溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

mybatisplus中返回Vo案例分析

發(fā)布時(shí)間:2023-03-14 14:11:34 來源:億速云 閱讀:111 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“mybatisplus中返回Vo案例分析”,在日常操作中,相信很多人在mybatisplus中返回Vo案例分析問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”mybatisplus中返回Vo案例分析”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!

注意: mybatisplus內(nèi)置的幾個(gè)方法使用泛型限制了方法的返回類型,
所以實(shí)現(xiàn)返回Vo還是得自定義方法, 這個(gè)方法名盡量不要和原有的名字類似
(以免出問題), 采用mybatisplus就是想借用它的wrapper的便利.

另外, 如果不采用vo, 而是直接ss.realname submitterName, ss.title submitterTitle, sa.realname appraiserName , sa.title appraiserTitle加入為entity中屬性(對(duì)這幾個(gè)屬性取消持久化 ), 然后直接修改page方法對(duì)應(yīng)的xml, 這么做雖然可以返回你要的結(jié)果, 但是, 前端傳入entity對(duì)象作為查詢參數(shù)時(shí), 假如這幾個(gè)屬性存在非空值, 那么mybatisplus會(huì)映射到sql中去, 這樣sql就報(bào)錯(cuò)了, 因?yàn)閿?shù)據(jù)庫沒這幾個(gè)字段
(除非你禁止這幾個(gè)參數(shù)的反序列化或者對(duì)mybatisplus wrapper 過濾掉這幾個(gè)屬性, 先不說fastjson的反序列化在這幾個(gè)屬性上用沒有bug, 這樣做會(huì)讓代碼不優(yōu)雅,耦合也高, swagger ui也沒寫清楚, 所以還是采用vo的好)

1.定義Vo (extends 實(shí)體類然后加幾個(gè)非持久化字段), 作為返回。不用map作為返回的原因是, 對(duì)swagger-api不友好

2.xml 如下(就用mybaisplus, 不想用mybatis)

 <select id="selectPageVo" resultType="com.DailyReportVo">
    select ss.realname submitterName, ss.title submitterTitle, sa.realname appraiserName , sa.title appraiserTitle, n.*
    from  nst_daily_report n
    LEFT JOIN sys_user sa
    on n.appraiser_id = sa.id
    LEFT JOIN sys_user ss
    on n.submitter_id = ss.id
    <where>
        ${ew.sqlSegment}
    </where>
</select>

3.service

public interface IDailyReportService extends IService<DailyReport> {
    IPage<DailyReportVo> selectPageVo(IPage<DailyReportVo> page, Wrapper<DailyReportVo> queryWrapper);
}
@Service
public class DailyReportServiceImpl extends ServiceImpl<DailyReportMapper, DailyReport> implements IDailyReportService {
    @Override
    public IPage<DailyReportVo> selectPageVo(IPage<DailyReportVo> page, Wrapper<DailyReportVo> queryWrapper) {
        return this.baseMapper.selectPageVo(page, queryWrapper);
    }
}

4.controller

@GetMapping(value = "/list")
	public Result<IPage<DailyReportVo>> queryPageList(DailyReport dailyReport,
									  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									  HttpServletRequest req){
		DailyReportVo dailyReportVo = new DailyReportVo();
				BeanUtils.copyProperties(dailyReportVo,dailyReport);
		Result<IPage<DailyReportVo>> result = new Result<>();
		QueryWrapper<DailyReportVo> queryWrapper = QueryGenerator.initQueryWrapper(dailyReportVo, req.getParameterMap());
		Page<DailyReportVo> page = new Page<>(pageNo, pageSize);
		IPage<DailyReportVo> pageList = dailyReportService.selectPageVo(page, queryWrapper);
		result.setSuccess(true);
		result.setResult(pageList);
		return result;
	}

到此,關(guān)于“mybatisplus中返回Vo案例分析”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI