MyBatis Example And与Or混合使用的实例
作者:看!蜗牛在漂移 时间:2021-11-11 01:53:13
MyBatis Example And与Or混合使用
(条件1 and 条件2) or ( 条件3 and 条件4)
MemberExample example = new MemberExample();
MemberExample.Criteria c1 = example.createCriteria();
c1.andOne(A).andTwo(B);
MemberExample.Criteria c2 = example.createCriteria();
c2.andThree(C).andFour(D);
example.or(c2);
条件1 and (条件2 or 条件3)
思路 : 分拆 : A and ( B or C ) ==> ( A and B ) or ( A and C )
MemberExample example = new MemberExample();
MemberExample.Criteria c1 = example.createCriteria();
c1.andOne(A).andTwo(B);
MemberExample.Criteria c2 = example.createCriteria();
c2.andOne(A).andThree(C);
example.or(c2);
MyBatis Example 处理And、Or关系方法
1.( xx and xx) or ( xx and xx)
实例代码:
BaUserExample baUserExample = new BaUserExample();
Criteria criteria1 = baUserExample.createCriteria();
criteria1.andOrgIdEqualTo("1");
criteria1.andDeptIdEqualTo("1");
Criteria criteria2 = baUserExample.createCriteria();
criteria2.andUserNameEqualTo("name");
criteria2.andEmailLike("%test@%");
baUserExample.or(criteria2);
userMapper.countByExample(baUserExample);
执行的sql语句:
==> Preparing: select count(*) from ba_user WHERE ( org_id = ? and dept_id = ? ) or( user_name = ? and email like ? )
2.xx and ( xx or xx)
暂时没找到直接的sql语句构造方法,但是经过转换还是可以实现的
根据逻辑表达式可以知道 a and ( b or c ) = ( a and b) or ( a and c )
所以就转变成第一种方法
举个例子,假如想要实现
select count(*) from ba_user WHERE userName like ? and ( dept_id is null or dept_id <>? )
可以转化为
select count(*) from ba_user WHERE (userName like ? and dept_id is null ) or ( userName like ? and dept_id <>? )
实例代码:
BaUserExample baUserExample = new BaUserExample();
Criteria criteria1 = baUserExample.createCriteria();
criteria1.andUserNameLike("%name%");
criteria1.andDeptIdIsNull();
Criteria criteria2 = baUserExample.createCriteria();
criteria2.andUserNameLike("%name%");
criteria2.andDeptIdNotEqualTo("1");
baUserExample.or(criteria2);
userMapper.countByExample(baUserExample);
执行的sql语句:
==> Preparing: select count(*) from ba_user WHERE ( user_name like ? and dept_id is null ) or( user_name like ? and dept_id <> ? )
这算是一种取巧的方法吧,对于这样的问题可以自己编写mapper.xml文件,或者在代码里面过滤,还有一种思路就是修改Criteria的代码实现and和or功能调换(还没尝试过)。
来源:https://blog.csdn.net/gunicoin/article/details/116461360
标签:MyBatis,Example,And,Or
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java文件上传与文件下载实现方法详解
2023-11-20 12:28:45
![](https://img.aspxhome.com/file/2023/5/59405_0s.png)
完美解决SpringCloud-OpenFeign使用okhttp替换不生效问题
2023-07-03 11:27:25
![](https://img.aspxhome.com/file/2023/3/83443_0s.jpg)
springboot+mybatis+枚举处理器的实现
2023-11-06 00:48:04
Java中的泛型详解
2023-11-02 17:51:56
C#中Span相关的性能优化建议
2021-07-05 15:42:26
![](https://img.aspxhome.com/file/2023/7/69817_0s.png)
spring mvc 实现获取后端传递的值操作示例
2023-08-10 12:55:52
Silverlight实现星星闪烁动画
2023-03-28 05:32:46
![](https://img.aspxhome.com/file/2023/1/85721_0s.gif)
Spring Boot 在启动时进行配置文件加解密的方法详解
2023-11-12 17:13:54
Android 自定义View实现抽屉效果
2022-08-03 01:27:00
C#导出数据到CSV文件的通用类实例
2023-05-22 09:23:16
Android控件之RatingBar自定义星级评分样式
2023-12-22 16:03:33
![](https://img.aspxhome.com/file/2023/3/93303_0s.png)
基于Beanutils.copyProperties()的用法及重写提高效率
2023-04-20 12:10:25
![](https://img.aspxhome.com/file/2023/8/85568_0s.jpg)
C#的内存回收代码
2023-06-20 06:38:47
Java多线程定时器Timer原理及实现
2022-03-03 09:53:51
Java实现的RSA加密解密算法示例
2022-05-03 09:20:38
java 中String.equals和==的比较
2023-03-09 08:45:16
C#实现IDisposable接口释放非托管资源
2022-12-01 06:59:53
C#仿密保卡功能的简单实现代码
2022-01-17 04:51:12
![](https://img.aspxhome.com/file/2023/2/94562_0s.png)
Unity的OnOpenAsset实用案例深入解析
2021-05-30 01:37:16
Java详解IO流创建读取与写入操作
2023-01-23 01:50:28