mybatis-plus条件构造器EntityWrapper

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis-plus条件构造器EntityWrapper相关的知识,希望对你有一定的参考价值。

参考技术A 使用mybatis-plus查询时,当查询的条件在model中时,model放入括号内 new EntityWrapper<>(model)

使用mybatis-plus查询时,当查询的条件不在model中时,需要自定义时候,model放入尖括号内 new EntityWrapper<AuthBatchSubject>().in("batchid", ids)

使用mybatis-plus查询时,也可以都不传,那就是查询所有结果了

mybatis-plus条件构造器以及QueryWrapper用法学习

QueryWrapper的基本用法

话不多说,直接上代码

更多使用方法传送门

public void mybatisPlusTest(Long id, String startTime, String endTime) 
		// eqWrapper.eq("id",id)  --->>  where id = id;
		// SELECT id,pre_amount,opt_type,amount,src_userinfo,withdrawal_amount,opt_biz_id,target_uid,logtime,src_uid FROM return_commission_log WHERE (id = ?)
		QueryWrapper<ReturnCommissionLog> eqWrapper = new QueryWrapper<>();
		List<ReturnCommissionLog> eqList = returnCommissionLogMapper.selectList(eqWrapper.eq("id",id));

		// leWrapper.le("id",id)  --->>  where id <= id;
		// SELECT id,pre_amount,opt_type,amount,src_userinfo,withdrawal_amount,opt_biz_id,target_uid,logtime,src_uid FROM return_commission_log WHERE (id <= ?)
		QueryWrapper<ReturnCommissionLog> leWrapper = new QueryWrapper<>();
		List<ReturnCommissionLog> leList = returnCommissionLogMapper.selectList(leWrapper.le("id",id));

		// geWrapper.ge("id",id)  --->>  where id >= id;
		// SELECT id,pre_amount,opt_type,amount,src_userinfo,withdrawal_amount,opt_biz_id,target_uid,logtime,src_uid FROM return_commission_log WHERE (id >= ?)
		QueryWrapper<ReturnCommissionLog> geWrapper = new QueryWrapper<>();
		List<ReturnCommissionLog> geList = returnCommissionLogMapper.selectList(geWrapper.ge("id",id));

		// betweenWrapper.between("logtime",startTime,endTime)  --->>  where logtime startTime between endTime;
		// SELECT id,pre_amount,opt_type,amount,src_userinfo,withdrawal_amount,opt_biz_id,target_uid,logtime,src_uid FROM return_commission_log WHERE (logtime BETWEEN ? AND ?)
		QueryWrapper<ReturnCommissionLog> betweenWrapper = new QueryWrapper<>();
		List<ReturnCommissionLog> betweenList = returnCommissionLogMapper.selectList(betweenWrapper.between("logtime",startTime,endTime));

		// groupByWrapper.groupBy("id")  --->>  group by id;
		// SELECT id,pre_amount,opt_type,amount,src_userinfo,withdrawal_amount,opt_biz_id,target_uid,logtime,src_uid FROM return_commission_log GROUP BY id
		QueryWrapper<ReturnCommissionLog> groupByWrapper = new QueryWrapper<>();
		List<ReturnCommissionLog> groupByList = returnCommissionLogMapper.selectList(groupByWrapper.groupBy("id"));

		// orderByWrapper.orderByDesc("id")  --->>  order by id desc;
		// SELECT id,pre_amount,opt_type,amount,src_userinfo,withdrawal_amount,opt_biz_id,target_uid,logtime,src_uid FROM return_commission_log ORDER BY id DESC
		QueryWrapper<ReturnCommissionLog> orderByWrapper = new QueryWrapper<>();
		List<ReturnCommissionLog> orderByList = returnCommissionLogMapper.selectList(orderByWrapper.orderByDesc("id"));

		// orWrapper.like("id",id).or().between("logtime",startTime,endTime)  --->>  ;
		// SELECT id,pre_amount,opt_type,amount,src_userinfo,withdrawal_amount,opt_biz_id,target_uid,logtime,src_uid FROM return_commission_log WHERE (id LIKE ? OR logtime BETWEEN ? AND ?)
		QueryWrapper<ReturnCommissionLog> orWrapper = new QueryWrapper<>();
		List<ReturnCommissionLog> orList = returnCommissionLogMapper.selectList(orWrapper.like("id",id).or().between("logtime",startTime,endTime));

		// andWrapper.and(i ->i.like("id",id).between("logtime",startTime,endTime))  --->>  ;
		// SSELECT id,pre_amount,opt_type,amount,src_userinfo,withdrawal_amount,opt_biz_id,target_uid,logtime,src_uid FROM return_commission_log WHERE ( (id LIKE ? AND logtime BETWEEN ? AND ?) )
		QueryWrapper<ReturnCommissionLog> andWrapper = new QueryWrapper<>();
		List<ReturnCommissionLog> andList = returnCommissionLogMapper.selectList(andWrapper.and(i ->i.like("id",id).between("logtime",startTime,endTime)));

		// lastWrapper.last(" order by id desc limit 10")  --->>  order by id desc limit 10;
		// SELECT id,pre_amount,opt_type,amount,src_userinfo,withdrawal_amount,opt_biz_id,target_uid,logtime,src_uid FROM return_commission_log order by id desc limit 10
		// last 只能使用一次,有sql注入风险,谨慎使用
		QueryWrapper<ReturnCommissionLog> lastWrapper = new QueryWrapper<>();
		List<ReturnCommissionLog> lastList = returnCommissionLogMapper.selectList(lastWrapper.last(" order by id desc limit 10"));
	

以上是关于mybatis-plus条件构造器EntityWrapper的主要内容,如果未能解决你的问题,请参考以下文章

Mybatis-Plus 条件构造器

mybatis-plus条件构造器

mybatis-plus条件构造器

MyBatis-Plus02_条件构造器QueryWrapperUpdateWrapperconditionLambdaQuery

mybatis-plus条件构造器QueryWrapper

mybatis-plus条件构造器以及QueryWrapper用法学习