MyBatis-Plus(QueryWrapper使用lambda表达式拼接一些常用条件的写法)
【1】=,like,!=,>=,排序等
super.lambdaQuery()
.eq(isNotNull(req.getId()), StudyDemo::getId, req.getId())
.like(isNotBlank(req.getName()), StudyDemo::getStudyName, req.getName())
.ne(true,StudyDemo::getStudyStatus,1)
.ge(true,StudyDemo::getTimes,1)
.orderByDesc(StudyDemo::getId)
.list();
sql:
select * from study_demo where id = 'id' and study_name like '%name%'
and study_status != 1 and times >= 0 order by id desc;
【2】between
super.lambdaQuery()
.between(isNotNull(req.getStartTime()) && isNotNull(req.getEndTime()), StudyDemo::getUpdateTime, req.getStartTime(), req.getEndTime())
.list();
sql:
select * from study_demo where update_time between 'startTime' and 'endTime'
【3】in
super.lambdaQuery()
.in(StudyDemo::getId, list).orderByAsc(StudyDemo::getId)
.list();
sql:
select * from study_demo where id in (...) order id asc;
【4】or
(1)
super.lambdaQuery()
.eq(true,StudyDemo::getStudyStatus,1)
.eq(isNotNull(req.getCourseUpdateId()),StudyDemo::getName, req.getName())
.or()
.eq(isNotNull(req.getCourseUpdateId()),StudyDemo::getOtherName, req.getName())
.list();
sql:
select * from study_demo where study_status =1 and name = 'xx' or other_name = 'xx'
(2)
super.lambdaQuery()
.eq(true,StudyDemo::getStudyStatus,1)
.or(obj1 -> obj1.eq(StudyDemo::getName, req.getName()))
.or(obj2 ->obj2.eq(StudyDemo::getOtherName, req.getName()))
.list();
sql:
select * from study_demo where study_status =1 and (name = 'xx' ) or (other_name = 'xx')
(3)
super.lambdaQuery()
.eq(true,StudyDemo::getStudyStatus,1)
.and(isNotNull(req.getCourseUpdateId()), wq -> wq
.eq(WoStudyChapter::getCourseUpdateId, req.getName())
.or()
.eq(WoStudyChapter::getCourseId, req.getName()))
.list();
sql:
select * from study_demo where study_status =1 and (name = 'xx' or other_name = 'xx')