MyBatis-Plus+mysql5.7 动态拼接sql语句 分页查询 自定义sql 查询条件 分组 排序

在使用srpingboot2.x+mybatis-plus框架是遇到特殊需求时研究mybatis-plus的条件构造器动态拼接sql查询,这个查询接口涉及到了自定义sql动态拼接sql分页求和分组排序。

可以直接看业务实现方法


首先说一下接口的需求:

入参JSON:

 1 {
 2     "from": "2020-5-29",
 3     "limit": 10,
 4     "offset": 0,
 5     "order": "hitCount",
 6     "page": 1,
 7     "search": "",
 8     "sort": "desc",
 9     "sortDirection": "",
10     "to": "2020-6-10"
11 }

入参注释说明:

{
    "from": "开始查询时间",
    "limit": 条数,
    "offset": 0,
    "order": "需要排序的字段",
    "page": 页码,
    "search": "查询条件",
    "sort": "排序规则",
    "sortDirection": "",
    "to": "结束查询时间"
}

然后先写接口实现:

1     @PostMapping("categoryList")
2     public R getCategoryList(@Validated @RequestBody QaDetailSearchVo vo){
3         RobotPage pageHelper = new RobotPage(vo.getPage(),vo.getLimit());
4         return R.ok(iStatQaService.findQaStatCategory(pageHelper, vo));
5     }

解释:POST请求,R 是自定义的返回类型,RobotPage是为了实现一些特殊返回值 继承于mybatsi-plus分页的Ipage:

1 public class RobotPage<T> extends Page<T> implements Serializable {}

重点是业务实现类和mapper层:

对于一般的查询mybatis-plus的条件构造器已经可以满足了,例如:

1 SysUser sysUser = baseMapper.selectOne(Wrappers.<SysUser>lambdaQuery()
2                 .select(SysUser::getUserId, SysUser::getUsername, SysUser::getPhone, SysUser::getEmail, SysUser::getPassword, SysUser::getDeptId, SysUser::getJobId, SysUser::getAvatar)
3                 .eq(SysUser::getUsername, username));

但是对于一些需要复杂的或者多表的操作就需要自定义sql语句了,示例:

首先是mapper自定义分页查询接口:

1 @Select("SELECT s.category_id as id, s.category_name as category, SUM(s.hit_count) as hitCount, SUM(s.solve_count) as resolvedCount, SUM(s.not_solve_count) as unresolvedCount " +
2             "FROM core_stat_qa as s ${ew.customSqlSegment}")
3     IPage<QaDetailCategoryListVo> findCategoryListByParams(IPage<QaDetailCategoryListVo> page, @Param(Constants.WRAPPER) QueryWrapper<StatQa> wrappers);

这部分官网给的有示例:https://mybatis.plus/guide/wrapper.html#%E4%BD%BF%E7%94%A8-wrapper-%E8%87%AA%E5%AE%9A%E4%B9%89sql

业务接口不用看 直接是实现类:

 1 @Override
 2     public Grid findQaStatCategory(Page<QaDetailCategoryListVo> page, QaDetailSearchVo vo) {
 3         //声明返回
 4         Grid grid = new Grid();
 5         //组装查询语句
 6         QueryWrapper<StatQa> qaQueryWrapper = new QueryWrapper<>();
 7         //模糊查询
 8         qaQueryWrapper.like(StringUtils.isNotBlank(vo.getSearch()),"category_name",vo.getSearch());
 9         //where条件
10         qaQueryWrapper.ge("create_time", DateUtil.parse(vo.getFrom(),"yyyy-MM-dd"));
11         qaQueryWrapper.lt("create_time",DateUtil.parse(vo.getTo(), "yyyy-MM-dd"));
12         //分组
13         qaQueryWrapper.groupBy("category_id","category_name");
14         //排序
15         qaQueryWrapper.orderBy(true, !"desc".equals(vo.getSort()),vo.getOrder());
16         //调用自定义sql语句
17         IPage<QaDetailCategoryListVo> statQaIPage = baseMapper.findCategoryListByParams(page, qaQueryWrapper);
18         //抽取list
19         List<QaDetailCategoryListVo> statQaList = statQaIPage.getRecords();
20         grid.setList(statQaList);
21         grid.setTotal(statQaIPage.getTotal());
22         return grid;
23     }

需要解释的:

排序orderBy方法中的第一个true官网上也是给出解释的:表示该条件是否加入最后生成的sql中。

!"desc".equals(vo.getSort())  是排序规则 asc和desc

调用mapper自定义sql就是:
baseMapper.findCategoryListByParams(page, qaQueryWrapper);

接口完成。
posted @ 2020-05-30 15:06  {{unidentified}}  阅读(9199)  评论(0编辑  收藏  举报