-
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: 如果本篇文章对你有帮助的,记得点赞加+关注~ 谢谢!