mybatis mapper.xml 区间查询条件
对 null 和 ” 分开进行判断
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
< select id = "selectByExample" resultMap = "BaseResultMap" parameterType = "com.test.entity.TestQuery" > SELECT * FROM test s WHERE 1=1 <!-- 创建时间 --> < if test = "createTime!=null and createTime!=''" > AND s.create_time = #{createTime,jdbcType=TIMESTAMP} </ if > <!-- 开始创建时间,结束创建时间都不为空 --> < if test = "beginCreateTime!=null and beginCreateTime!='' and endCreateTime!=null and endCreateTime!=''" > AND s.create_time >= #{beginCreateTime,jdbcType=TIMESTAMP} AND s.create_time <= #{endCreateTime,jdbcType=TIMESTAMP} </ if > <!-- 开始不为空,结束为空 null --> < if test = "beginCreateTime!=null and beginCreateTime!='' and endCreateTime==null" > AND s.create_time >= #{beginCreateTime,jdbcType=TIMESTAMP} </ if > <!-- 开始不为空,结束为空 '' --> < if test = "beginCreateTime!=null and beginCreateTime!='' and endCreateTime==''" > AND s.create_time >= #{beginCreateTime,jdbcType=TIMESTAMP} </ if > <!-- 开始为空 null,结束不为空 --> < if test = "beginCreateTime==null and endCreateTime!=null and endCreateTime!=''" > AND s.create_time <= #{endCreateTime,jdbcType=TIMESTAMP} </ if > <!-- 开始为空 '',结束不为空 --> < if test = "beginCreateTime=='' and endCreateTime!=null and endCreateTime!=''" > AND s.create_time <= #{endCreateTime,jdbcType=TIMESTAMP} </ if > </ select > |
tk mybatis通用mapper,复杂and or条件查询
需求:where查询,需要支持(a or b or c) and d
也就是a、b、c三个条件是或的关系,然后再与d相与。
尝试后,可以通过以下方式处理:
方式1:Weekend语法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
Weekend<User> weekend = new Weekend<>(User. class ); //关键字查询部分 String keyword = pageReq.getKeyword(); WeekendCriteria<User, Object> keywordCriteria = weekend.weekendCriteria(); if (StringUtils.isNotEmpty(keyword)) { keywordCriteria.orLike(User::getUserName, keyword).orLike(User::getPoliceNo, keyword).orLike(User::getRealName, keyword); //此处不需要再用下面这一句了,不然上面这个条件组合会重复一次 //weekend.and(keywordCriteria) } //部门查询部分 Example example = new Example(User. class ); Example.Criteria criteria = example.createCriteria(); criteria.andEqualTo( "departmentId" , departmentId); weekend.and(criteria); PageHelper.startPage(pageReq.getPageIndex(), pageReq.getPageSize()); List<User> users = userMapper.selectByExample(weekend); |
ps:上面,其中Weekend是高版本的通用mapper版本才有,而且需要java8语法支持。
方式2:通用example语法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
Example e = new Example(User. class ); Example.Criteria c = e.createCriteria(); //关键字查询部分 String keyword = pageReq.getKeyword(); if (StringUtils.isNotEmpty(keyword)) { c.orEqualTo( "userName" , keyword).orEqualTo( "policeNo" ,keyword).orEqualTo( "realName" ,keyword); } //部门查询部门 Example.Criteria criteria = e.createCriteria(); criteria.andEqualTo( "departmentId" , departmentId); e.and(criteria); PageHelper.startPage(pageReq.getPageIndex(), pageReq.getPageSize()); List<User> users = userMapper.selectByExample(e); |
执行的sql为:
1
2
3
4
5
6
|
WHERE ( user_name = ? OR police_no = ? OR real_name = ? ) AND (department_id = ?) |
总结下来,就是,
每个条件组合(a/b/c) (d)各自创建自己的cirteria,再用and或者or方法去连接
以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/tengdazhang770960436/article/details/56840666