关于mybatis-plus的经验共享
@Select和Wrapper在同一个方法上共用
如下:
@Select("select * from selection " + "right join course on selection_courseId=course_id " + "and selection_studentId=#{studentId} \n") public List<SelectionResp> listSelectionsBysStudentIdUnion(@Param("studentId")Long studentId, Wrapper<Course> wrapper);
事实上,以上代码执行时并不会按照所想的,将Wrapper当做条件语句.经查阅资料,如下:
@Select("select * from selection " + "right join course on selection_courseId=course_id " + "and selection_studentId=#{studentId} ${ew.customSqlSegment}\n") public List<SelectionResp> listSelectionsBysStudentIdUnion(@Param("studentId")Long studentId, @Param(Constants.WRAPPER) Wrapper<Course> wrapper);
在wrapper添加注解@Param(Constants.WRAPPER) ,并在@select后追加 ${ew.customSqlSegment},就会达到想要的效果.(其实常量值Constants.WRAPPER就是’ew’);另外,一定要注意.Wrapper中要使用column_name的字符串,不要使用实体类,否则也不会奏效.
条件语句查询
@ApiOperation("查看课程;分页,可筛选")
@GetMapping("list/{current}/{size}")
public R getCoursesByStudentId(@PathVariable int current, @PathVariable int size, @RequestHeader("token") String token, CourseVo courseVo) {
final Long studentId = Long.valueOf(JwtUtils.getUserId(token));
final Page<SelectionResp> page = new Page<>(current, size);
final QueryWrapper<Course> wrapper = new QueryWrapper<>();
// --------------------------下面是查看的重点--------------------------------------------------
wrapper.lambda()
.like(Course::getCourseName, courseVo.getCourseName())
.like(Course::getCourseTime, courseVo.getCourseTime())
.like(Course::getCourseLocation, courseVo.getCourseLocation());
final List<SelectionResp> list = selectionService.listSelectionsBysStudentIdUnion(studentId, wrapper, page);
return R.ok().data("list", list);
}
利用以上内容,不使用筛选时,会得到下面的语句.
==> Preparing: select * from selection right join course on selection_courseId=course_id and selection_studentId=? WHERE (course_name LIKE ? AND course_time LIKE ? AND course_location LIKE ?)
==> Parameters: 2019215180(Long), %null%(String), %null%(String), %null%(String)
解决办法有两种
-
在前端传送数据的时候传入相应的courseName,courseTime,courseLocation,并赋值为""(注意,此时不要赋值为null,因为axios中params为空的时候不会传入服务器)
-
在后端解决.
wrapper .like(!ObjectUtils.isEmpty(courseVo.getCourseName()),"course_name", courseVo.getCourseName()) .like(!ObjectUtils.isEmpty(courseVo.getCourseTime()),"course_time", courseVo.getCourseTime()) .like(!ObjectUtils.isEmpty(courseVo.getCourseName()),"courseName", courseVo.getCourseLocation());
可以看一下类似的源码
public Children like(boolean condition, R column, Object val) { return likeValue(condition, LIKE, column, val, SqlLike.DEFAULT); }
对于每一个方法如like,eq等,都有其重载方法,即根据doncition判断是否要加入到条件语句中.
分页插件不奏效
-
查看是否启用插件
@Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); return interceptor; } //官网新版内容
-
//传递参数 Page 即自动分页,必须放在第一位(官网原话) @Select("select * from selection " + "right join course on selection_courseId=course_id " + "and selection_studentId=#{studentId} ${ew.customSqlSegment}\n") public List<SelectionResp> listSelectionsBysStudentIdUnion(IPage page, @Param("studentId")Long studentId, @Param(Constants.WRAPPER) Wrapper<Course> wrapper);