EntityWrapper自定义SQL
在mybatis plus中根据条件构造器,构建SQL查询是很方便的,使用条件构造器可以替代我们写SQL。下面我们看几个例子。
第一种
使用T selectOne(Wrapper<T> wrapper);我们只需要传递入我们创建一个EntityWrapper()并将条件拼接好就可以。
例如new EntityWrapper().eq("id","1")就是查询id等于1的这条数据。这里不仅仅支持eq(),还有like,not like ,group by等,差不多在SQL中需要的这里都有。
第二种
在单表查询的时候我们需要自定义列的时候则使用setSqlSelect方法,具体如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
/**此格式的意思为requireParam传参了才创建 where 条件,请按照此格式撰写代码 *Object为你的单表实体类 *requireParam为入参实体类 *EntityWrapper<Object> ew = new EntityWrapper<Object>(requireParam);是为了让构造 where 条件 *我在前面给requireParam的state属性设置了值为1,所以这里会自动拼接上 where 条件,如果不需要的话则可不传requireParam * * **/ EntityWrapper<Object> ew = new EntityWrapper<Object>(requireParam); /**设置需要查询的列名**/ ew.setSqlSelect( "id,resource_id,user_id,username,content,createtime,parent_id," + " path,type,like_count" ) /**排序方式**/ .orderBy( false , "path" , false ); |
同第一种情况一样,后面可以继续拼接各种条件。
第三种
EntityWrapper()拼接where条件,这个其实在第二种情况中已经讲了,就是根据我们创建EntityWrapper的时候有没有入参来做。
到目前我们只用到了这些,以后遇到了我会继续更新。
MybatisPlus自定义sql查询
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public void reconciliationForConfirmOne(Integer Id, Integer payoffType) { List<FinanceReconciliationEntity> frList = financeReconciliationService.selectList( new EntityWrapper<FinanceReconciliationEntity>() .eq( "payoff_id" , Id) .eq( "payoff_type" , payoffType) .where( "(reconciliation_status <> 1 OR reconciliation_status IS NULL)" ) ); for (FinanceReconciliationEntity fr : frList) { fr.setReconciliationStatus( 1 ); } if (frList != null && frList.size() > 0 ) { financeReconciliationService.updateBatchById(frList); } } |
以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/zhouseawater/article/details/81166420