SpringBoot整合Mybatis-plus 实现自定义的多表查询、分页条件查询

  • Mybatis-plus 实现自定义的多表查询、分页条件查询
    问题:总所周知,Mybatis-plus这个框架在日常开发中,减少了许多需要自己手写sql语句(单表)的情况,但是当一个业务需要查询多张表的时候,并分页条件查询时,Mybatis-plus并不能自动帮我们实现,需要自己动手写动态sql语句。
    废话不多说,直接开始!

  • 首先,实现分页查询需要配置分页插件,别忘了在配置类上加上@Configuration注解哦

@Configuration
public class MybatisPlusConfig {

    //分插件
    @Bean
    public MybatisPlusInterceptor paginationInnerInterceptor () {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
   	 }
   }
  • 然后,在mapper层定义查询的方法,注意这里需要加上@Param(Constants.WRAPPER) 注解,会自动给我们动态补全,
    需要在mapper接口上方加上@Mapper 注解 让spring自动管理,当然也可以配置扫描包
@Mapper
public interface EduCourseMapper extends BaseMapper<EduCourse> {
    //直接查询课程信息,用vo封装
    //@Param(Constants.WRAPPER) 加上这个注解
    public Page<CourseInfoList> getCourseInfoList (Page<CourseInfoList> courseListPage,
                                                   @Param(Constants.WRAPPER) QueryWrapper<CourseInfoList> queryWrapper);

}
  • 然后,需要在xml中写sql语句,注意这里需要在id后面加上${ew.customSqlSegment},这个相等于会自动在后面添加where
    ,这个和@Param(Constants.WRAPPER) 注解配套使用
<select id="getCourseInfoList" resultType="com.million.eduservice.entity.vo.course.CourseInfoList">
        select ec.id,
               ec.title,
               ec.price,
               ec.lesson_num lessonNum,
               ec.`status`,
               ec.view_count viewCount,
               ec.gmt_create gmtCreate,
               et.`name`     teacherName,
               ecd.description
        FROM guli.edu_course ec
                 LEFT JOIN guli.edu_course_description ecd ON ec.id = ecd.id
                 LEFT JOIN guli.edu_teacher et ON ec.teacher_id = et.id ${ew.customSqlSegment}
                 ORDER BY gmtCreate DESC
    </select>
  • 在service 定义你要查询方法,我这里有个条件查询的方法,如果没有条件查询的需要,添加直接查询 的方法即可
    //直接查询课程信息,用vo封装
    Page<CourseInfoList> getCourseInfoList (Page<CourseInfoList> courseListPage, QueryWrapper<CourseInfoList> queryWrapper);

    //条件分页查询课程信息
    Page<CourseInfoList>  pageQueryCourse (Page<CourseInfoList> coursePage, CourseQuery courseQuery);
  • 在service实现类中重写这个两个方法,实现业务需求
    @Override
    public Page<CourseInfoList> getCourseInfoList (Page<CourseInfoList> courseListPage, QueryWrapper<CourseInfoList> queryWrapper) {
        return null;
    }


    //条件分页查询课程信息
    @Override
    public Page<CourseInfoList> pageQueryCourse (Page<CourseInfoList> courseListPage, CourseQuery courseQuery) {
        QueryWrapper<CourseInfoList> queryWrapper = new QueryWrapper<>();
        //判断teacherQuery是否为空,也就是前端有无传过来参数,没有,就直接分页查询
        if (courseQuery == null) {
            Page<CourseInfoList> courseInfoList = baseMapper.getCourseInfoList(courseListPage, null);
            return courseInfoList;
        }
        String title = courseQuery.getName();
        String status = courseQuery.getStatus();
        Integer lessonNum = courseQuery.getLessonNum();
        BigDecimal price = courseQuery.getPrice();

        if (!StringUtils.isEmpty(title)) {
            queryWrapper.like("title", title);
        }
        if (!StringUtils.isEmpty(status)) {
            queryWrapper.eq("status", status);
        }
        if (!StringUtils.isEmpty(lessonNum)) {
            //课程数大于等于  这里的column是数据库的字段名
            queryWrapper.ge("lesson_num", lessonNum);
        }
        if (!StringUtils.isEmpty(price)) {
            //价格大于等于
            queryWrapper.ge("price", price);
        }
        //加了@Param(Constants.WRAPPER) 注解 和${ew.customSqlSegment},会将我们自己定义的queryWrapper自动补全到sql语句where后面
        return baseMapper.getCourseInfoList(courseListPage, queryWrapper);
    }

  • 在controller接口中调用
  @ApiOperation(value = "分页条件查询")
    @PostMapping("/pageQueryCourse/{page}/{limit}")
    public R pageQueryCourse (@ApiParam(name = "page", value = "当前页码", required = true)
                        @PathVariable("page") Long page,
                        @ApiParam(name = "limit", value = "每页记录数", required = true)
                        @PathVariable("limit") Long limit,
                        @RequestBody CourseQuery courseQuery) {
        Page<CourseInfoList> courseListPage = new Page<>(page, limit);
        Page<CourseInfoList> courseList = eduCourseService.pageQueryCourse(courseListPage, courseQuery);
        return R.ok().data("courseList", courseList);
    }
  • 使用Swagger接口测试
    在这里插入图片描述

在这里插入图片描述

  • 总结:1.需要先配置分页插件
    2.在mapper接口的方法上,加上 @Param(Constants.WRAPPER) QueryWrapper queryWrapper 注解
    3.在xml中加上${ew.customSqlSegment}
    4.实现业务 调用接口,进行测试,条件查询中的数据用@requestBody注解封装

ps: 如果本篇文章对你有帮助的,记得点赞加+关注~ 谢谢!

  • 5
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值