EntityWrapper的in用法
1
2
3
4
|
EntityWrapper<UserLife> wrapper = new EntityWrapper<>(); wrapper.eq( "is_valid" , 1 ); wrapper.in( "life_name" , "ge,edu,career" ); List<UserLife> userLabelList = userLabelService.selectList(wrapper); |
in的第二个参数可以是字符串也可以是list但是注意字符串中","间不能有空格,不然会查出来的语句就是这样的
mybatis-plus EntityWrapper in
环境:
springBoot+mybatis
源码:
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
|
/** * <p> * IN 条件语句,目前适配mysql及oracle * </p> * * @param column 字段名称 * @param value 匹配值 集合 * @return this */ public Wrapper<T> in(String column, Collection<?> value) { return in( true , column, value); } /** * <p> * IN 条件语句,目前适配mysql及oracle * </p> * * @param condition 拼接的前置条件 * @param column 字段名称 * @param value 匹配值 集合 * @return this */ public Wrapper<T> in( boolean condition, String column, Collection<?> value) { if (condition && CollectionUtils.isNotEmpty(value)) { sql.WHERE(formatSql(inExpression(column, value, false ), value.toArray())); } return this ; } |
如果condition不传,等同于:condition: true;
如果传入的value不为空,相当于改 in 查询语句为拼接;
举个例子
1
2
3
4
5
6
7
8
9
10
11
|
//代码 @Override public List<User> selectByCaseIdSet(Set<String> idSet) { EntityWrapper<User> wrapper = new EntityWrapper<>(); wrapper.in(!CollectionUtils.isEmpty(idSet), "id" , idSet); return this .selectList(wrapper); } /** * 如果idSet 为空,sql: select * from user * 如果idSet 不为空, sql: select * from user where id in (idSet) ** / |
以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/luo_yu_1106/article/details/100270052