MyBatis-Plus(QueryWrapper使用lambda表达式拼接一些常用条件的写法)

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')
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值