mybatisplus中返回Vo案例分析

发布时间:2023-03-14 14:11:34 作者:iii
来源:亿速云 阅读:111

这篇文章主要介绍“mybatisplus中返回Vo案例分析”,在日常操作中,相信很多人在mybatisplus中返回Vo案例分析问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”mybatisplus中返回Vo案例分析”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

注意: mybatisplus内置的几个方法使用泛型限制了方法的返回类型,
所以实现返回Vo还是得自定义方法, 这个方法名尽量不要和原有的名字类似
(以免出问题), 采用mybatisplus就是想借用它的wrapper的便利.

另外, 如果不采用vo, 而是直接ss.realname submitterName, ss.title submitterTitle, sa.realname appraiserName , sa.title appraiserTitle加入为entity中属性(对这几个属性取消持久化 ), 然后直接修改page方法对应的xml, 这么做虽然可以返回你要的结果, 但是, 前端传入entity对象作为查询参数时, 假如这几个属性存在非空值, 那么mybatisplus会映射到sql中去, 这样sql就报错了, 因为数据库没这几个字段
(除非你禁止这几个参数的反序列化或者对mybatisplus wrapper 过滤掉这几个属性, 先不说fastjson的反序列化在这几个属性上用没有bug, 这样做会让代码不优雅,耦合也高, swagger ui也没写清楚, 所以还是采用vo的好)

1.定义Vo (extends 实体类然后加几个非持久化字段), 作为返回。不用map作为返回的原因是, 对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;
	}

到此,关于“mybatisplus中返回Vo案例分析”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. SpringBoot整合MybatisPlus配置多数据源增强插件的方法是什么
  2. springboot+mybatisplus+druid如何实现多数据源+分布式事务

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mybatisplus vo

上一篇:unicloud如何获取首页列表数据

下一篇:Go interface接口如何声明

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》