Spring Boot + Mybatis-Plus 集成与使用(四)
前言:
上章节我们讲解了MyBatis-Plus自动SQL注入原理以及简单了解了什么是条件构造器。本章节就来对条件构造器的各个方法的使用进行讲解。
一、性能分析插件
在条件构造器使用之前,我们先讲解下MyBatis-Plus提供的一个扩展插件-性能分析插件。可以用于输出每条 SQL 语句及其执行时间。之所以先讲解它呢,是因为接下来,我们讲解条件构造器的各个方法,只查看和比较SQL的输出打印,不做执行后数据结果的查看。
1、配置
在前面章节介绍配置问题1时,其中一种解决方法是新建一个配置类。现在配置插件,我们可以在这个类中添加如下代码,在Spring Boot启动时实例化插件类对象,加载到容器中:
[code]@Bean public PerformanceInterceptor performanceInterceptor() { PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor(); performanceInterceptor.setFormat(true); return performanceInterceptor; }
参数说明:
- 参数:maxTime SQL 执行最大时长,超过自动停止运行,有助于发现问题。
- 参数:format SQL SQL是否格式化,默认false。这里设置为true,格式化打印转出。
- 该插件只用于开发环境,不建议生产环境使用。
二、条件构造器使用
官方警告:不支持以及不赞成在RPC调用中把 Wrapper 进行传输。wrapper 很重,传输 wrapper 可以类比为你的 controller 用 map 接收值(开发一时爽,维护火葬场)。正确的 RPC 调用姿势是写一个 DTO 进行传输,被调用方再根据 DTO 执行相应的操作 。
1、QueryWrapper
继承自 AbstractWrapper ,自身的内部属性 entity 也用于生成 where 条件。这里使用的是数据库字段,不是Java属性。经下面讲解提供条件方法
-
allEq 多个EQ或个别isNull
接口与默认方法:
[code] default <V> Children allEq(Map<R, V> params) { return this.allEq(params, true); } default <V> Children allEq(Map<R, V> params, boolean null2IsNull) { return this.allEq(true, params, null2IsNull); } <V> Children allEq(boolean condition, Map<R, V> params, boolean null2IsNull); default <V> Children allEq(BiPredicate<R, V> filter, Map<R, V> params) { return this.allEq(filter, params, true); } default <V> Children allEq(BiPredicate<R, V> filter, Map<R, V> params, boolean null2IsNull) { return this.allEq(true, filter, params, null2IsNull); } <V> Children allEq(boolean condition, BiPredicate<R, V> filter, Map<R, V> params, boolean null2IsNull);
参数说明:
- params:字段名与字段值的键值对,
key
为数据库字段名,value
为字段值 - condition:该条件是否加入生成的sql中
- null2IsNull: 为
true
则在map
的value
为null
时调用 isNull 方法,为false
时则忽略value
为null
的 - filter:过滤params中的键值加入生成的sql中
实例:
[code]@Test public void contextLoads() { QueryWrapper<SysLog> queryWrapper = new QueryWrapper(); Map<String, Object> map = new HashMap<>(); map.put("f_option_type", "1"); map.put("f_option_person", "admin"); queryWrapper.allEq(map); sysLogMapper.selectList(queryWrapper); }
[code] Time:53 ms - ID:com.banxun.demo.mapper.SysLogMapper.selectList Execute SQL: SELECT f_log_id AS logId, f_option_type AS optionType, f_option_person AS optionPerson, f_option_content AS optionContent, f_option_ip AS optionIp, f_option_type AS optionStatus, f_error_info AS errorInfo, f_option_time AS optionTime FROM t_sys_log WHERE f_option_person = 'admin' AND f_option_type = '1'
可以看到,在map中传入了两个值对。控台打印了执行时间以及执行的SQL,在where 条件后拼接了f_option_person = 'admin' AND f_option_type = '1'
我们给map中键f_option_person的value设为空,再看下结果
[code]@Test public void contextLoads() { QueryWrapper<SysLog> queryWrapper = new QueryWrapper(); Map<String, Object> map = new HashMap<>(); map.put("f_option_type", "1"); map.put("f_option_person", null); queryWrapper.allEq(map); sysLogMapper.selectList(queryWrapper); }
[code] Time:25 ms - ID:com.banxun.demo.mapper.SysLogMapper.selectList Execute SQL: SELECT f_log_id AS logId, f_option_type AS optionType, f_option_person AS optionPerson, f_option_content AS optionContent, f_option_ip AS optionIp, f_option_type AS optionStatus, f_error_info AS errorInfo, f_option_time AS optionTime FROM t_sys_log WHERE f_option_person IS NULL AND f_option_type = '1'
可以看到,当键f_option_person值为空,默认会在where 条件后拼接了f_option_person IS NULL ,可以看null2IsNull 默认是true,现在传false,看下执行结果
[code]@Test public void contextLoads() { QueryWrapper<SysLog> queryWrapper = new QueryWrapper(); Map<String, Object> map = new HashMap<>(); map.put("f_option_type", "1"); map.put("f_option_person", null); queryWrapper.allEq(map, false); sysLogMapper.selectList(queryWrapper); }
[code]Time:25 ms - ID:com.banxun.demo.mapper.SysLogMapper.selectList Execute SQL: SELECT f_log_id AS logId, f_option_type AS optionType, f_option_person AS optionPerson, f_option_content AS optionContent, f_option_ip AS optionIp, f_option_type AS optionStatus, f_error_info AS errorInfo, f_option_time AS optionTime FROM t_sys_log WHERE f_option_type = '1'
输出的SQL可以看到,传null2IsNull为false,where后不执行 is null。我们再来看filter参数的用法。filter是BiPredicate函数接口类型,返回Boolean。这里使用时需要先了解JDK8新特性-函数接口使用方式。这里不做深入讲解,我们使用null2IsNull默认true。如果没有传filter,应该两个条件都打印,现在来看下执行结果
[code]@Test public void contextLoads() { QueryWrapper<SysLog> queryWrapper = new QueryWrapper(); Map<String, Object> map = new HashMap<>(); map.put("f_option_type", "1"); map.put("f_option_person", "admin"); queryWrapper.allEq((k, v) -> v == "1", map); sysLogMapper.selectList(queryWrapper); }
[code] Time:29 ms - ID:com.banxun.demo.mapper.SysLogMapper.selectList Execute SQL: SELECT f_log_id AS logId, f_option_type AS optionType, f_option_person AS optionPerson, f_option_content AS optionContent, f_option_ip AS optionIp, f_option_type AS optionStatus, f_error_info AS errorInfo, f_option_time AS optionTime FROM t_sys_log WHERE f_option_type = '1'
输出SQL看到where只拼接了f_option_type = '1'这一个条件,我们来看eqAll源码
[code]public <V> Children allEq(boolean condition, BiPredicate<R, V> filter, Map<R, V> params, boolean null2IsNull) { if (condition && CollectionUtils.isNotEmpty(params)) { params.forEach((k, v) -> { if (filter.test(k, v)) { if (StringUtils.checkValNotNull(v)) { this.eq(k, v); } else if (null2IsNull) { this.isNull(k); } } }); } return this.typedThis; }
分析源码,可以看到方法中遍历map参数,使用函数接口传入具体实现来判断是否过滤此条件。不过过滤,则继续判断是否值为空,为空再判断是否使用is null。
condition参数就不试,意思上面也介绍了。就是判断条件是否加入生成sql,有兴趣的同伴可以自已动手试试看结果。
-
ne 等于 =
接口与默认方法:
[code]default Children eq(R column, Object val) { return this.eq(true, column, val); } Children eq(boolean condition, R column, Object val);
参数:
- condition:该条件是否加入生成的sql中
- column:数据库字段名
- val:字段值
实例:
[code]@Test public void contextLoads() { QueryWrapper<SysLog> queryWrapper = new QueryWrapper(); queryWrapper.eq("f_option_type", "1").eq("f_option_persion","admin"); sysLogMapper.selectList(queryWrapper); }
[code] Time:33 ms - ID:com.banxun.demo.mapper.SysLogMapper.selectList Execute SQL: SELECT f_log_id AS logId, f_option_type AS optionType, f_option_person AS optionPerson, f_option_content AS optionContent, f_option_ip AS optionIp, f_option_type AS optionStatus, f_error_info AS errorInfo, f_option_time AS optionTime FROM t_sys_log WHERE f_option_type = '1' AND f_option_person = 'admin'
QueryWrapper支持链式,调用多个eq,以下几个条件方法同eq,这里就不作多讲解
-
ne 不等于 <>
-
gt 大于 >
-
ge 大于等于 >=
-
lt 小于 <
-
le 小于等于 <=
-
between BETWEEN 值1 AND 值2
[code]default Children between(R column, Object val1, Object val2) { return this.between(true, column, val1, val2); } Children between(boolean condition, R column, Object val1, Object val2);
参数:
- condition:该条件是否加入生成的sql中
- column:数据库字段名
- val1:字段值1
- val2:字段值2
实例:
[code]@Test public void contextLoads() { QueryWrapper<SysLog> queryWrapper = new QueryWrapper(); queryWrapper.between("f_log_id", 10, 20); sysLogMapper.selectList(queryWrapper); }
[code] Time:22 ms - ID:com.banxun.demo.mapper.SysLogMapper.selectList Execute SQL: SELECT f_log_id AS logId, f_option_type AS optionType, f_option_person AS optionPerson, f_option_content AS optionContent, f_option_ip AS optionIp, f_option_type AS optionStatus, f_error_info AS errorInfo, f_option_time AS optionTime FROM t_sys_log WHERE f_log_id BETWEEN 10 AND 20
-
notBetween NOT BETWEEN 值1 AND 值2
[code]default Children notBetween(R column, Object val1, Object val2) { return this.between(true, column, val1, val2); } Children notBetween(boolean condition, R column, Object val1, Object val2);
参数:
- condition:该条件是否加入生成的sql中
- column:数据库字段名
- val1:字段值1
- val2:字段值2
实例:
[code]@Test public void contextLoads() { QueryWrapper<SysLog> queryWrapper = new QueryWrapper(); queryWrapper.notBetween("f_log_id", 10, 20); sysLogMapper.selectList(queryWrapper); }
[code] Time:31 ms - ID:com.banxun.demo.mapper.SysLogMapper.selectList Execute SQL: SELECT f_log_id AS logId, f_option_type AS optionType, f_option_person AS optionPerson, f_option_content AS optionContent, f_option_ip AS optionIp, f_option_type AS optionStatus, f_error_info AS errorInfo, f_option_time AS optionTime FROM t_sys_log WHERE f_log_id NOT BETWEEN 10 AND 20
-
like LIKE '%值%'
接口与默认方法:
[code]default Children like(R column, Object val) { return this.like(true, column, val); } Children like(boolean condition, R column, Object val);
参数:
- condition:该条件是否加入生成的sql中
- column:数据库字段名
- val:字段值
实例:
[code]@Test public void contextLoads() { QueryWrapper<SysLog> queryWrapper = new QueryWrapper(); queryWrapper.like("f_option_content", "异常"); sysLogMapper.selectList(queryWrapper); }
[code] Time:21 ms - ID:com.banxun.demo.mapper.SysLogMapper.selectList Execute SQL: SELECT f_log_id AS logId, f_option_type AS optionType, f_option_person AS optionPerson, f_option_content AS optionContent, f_option_ip AS optionIp, f_option_type AS optionStatus, f_error_info AS errorInfo, f_option_time AS optionTime FROM t_sys_log WHERE f_option_content LIKE '%异常%'
-
notLike NOT LIKE '%值%'
接口与默认方法:
[code]default Children notLike(R column, Object val) { return this.like(true, column, val); } Children notLike(boolean condition, R column, Object val);
参数:
- condition:该条件是否加入生成的sql中
- column:数据库字段名
- val:字段值
实例:
[code]@Test public void contextLoads() { QueryWrapper<SysLog> queryWrapper = new QueryWrapper(); queryWrapper.notLike("f_option_content", "异常"); sysLogMapper.selectList(queryWrapper); }
[code] Time:37 ms - ID:com.banxun.demo.mapper.SysLogMapper.selectList Execute SQL: SELECT f_log_id AS logId, f_option_type AS optionType, f_option_person AS optionPerson, f_option_content AS optionContent, f_option_ip AS optionIp, f_option_type AS optionStatus, f_error_info AS errorInfo, f_option_time AS optionTime FROM t_sys_log WHERE f_option_content NOT LIKE '%异常%'
-
likeLeft LIKE '%值'
接口与默认方法:
[code]default Children likeLeft(R column, Object val) { return this.like(true, column, val); } Children likeLeft(boolean condition, R column, Object val);
参数:
- condition:该条件是否加入生成的sql中
- column:数据库字段名
- val:字段值
实例:
[code]@Test public void contextLoads() { QueryWrapper<SysLog> queryWrapper = new QueryWrapper(); queryWrapper.likeLeft("f_option_content", "异常"); sysLogMapper.selectList(queryWrapper); }
[code] Time:22 ms - ID:com.banxun.demo.mapper.SysLogMapper.selectList Execute SQL: SELECT f_log_id AS logId, f_option_type AS optionType, f_option_person AS optionPerson, f_option_content AS optionContent, f_option_ip AS optionIp, f_option_type AS optionStatus, f_error_info AS errorInfo, f_option_time AS optionTime FROM t_sys_log WHERE f_option_content LIKE '%异常'
输出打印SQL可以看likeLeft,就是对字段值左边进行模糊查询,likeRight顾名思义就是对字段值右边进行模糊查询
-
likeRight LIKE '值%'
-
isNull 字段 IS NULL
接口与默认方法:
[code]default Children isNull(R column) { return this.isNull(true, column); } Children isNull(boolean condition, R column);
参数:
- condition:该条件是否加入生成的sql中
- column:数据库字段名
实例:
[code]@Test public void contextLoads() { QueryWrapper<SysLog> queryWrapper = new QueryWrapper(); queryWrapper.isNull("f_option_content"); sysLogMapper.selectList(queryWrapper); }
[code] Time:24 ms - ID:com.banxun.demo.mapper.SysLogMapper.selectList Execute SQL: SELECT f_log_id AS logId, f_option_type AS optionType, f_option_person AS optionPerson, f_option_content AS optionContent, f_option_ip AS optionIp, f_option_type AS optionStatus, f_error_info AS errorInfo, f_option_time AS optionTime FROM t_sys_log WHERE f_option_content IS NULL
输出打印SQL可以看到在where后拼接了f_option_content IS NULL,那isNotNull就是拼接了f_option_content IS NOT NULL
-
isNotNull 字段 IS NOT NULL
-
in IN (value.get(0), value.get(1), ...)
接口与默认方法:
[code]default Children in(R column, Collection<?> coll) { return this.in(true, column, coll); } Children in(boolean condition, R column, Collection<?> coll);
参数:
- condition:该条件是否加入生成的sql中
- column:数据库字段名
- coll:字段值集合或数组
实例:
[code]@Test public void contextLoads() { QueryWrapper<SysLog> queryWrapper = new QueryWrapper(); queryWrapper.in("f_log_id", new Integer[]{1,2,3}); sysLogMapper.selectList(queryWrapper); }
[code] Time:21 ms - ID:com.banxun.demo.mapper.SysLogMapper.selectList Execute SQL: SELECT f_log_id AS logId, f_option_type AS optionType, f_option_person AS optionPerson, f_option_content AS optionContent, f_option_ip AS optionIp, f_option_type AS optionStatus, f_error_info AS errorInfo, f_option_time AS optionTime FROM t_sys_log WHERE f_log_id IN ( 1,2,3 )
输出打印SQL可以看到在where后拼接了f_log_id IN (1,2,3),那isNotNull就是拼接了f_log_id NOT IN (1,2,3)
-
notIn 字段 NOT IN (value.get(0), value.get(1), ...)
-
inSql 字段 IN ( sql语句 )
接口与默认方法:
[code]default Children inSql(R column, String inValue) { return this.inSql(true, column, inValue); } Children inSql(boolean condition, R column, String inValue);
参数:
- condition:该条件是否加入生成的sql中
- column:数据库字段名
- inValue:in内sql字符串
实例:
[code]@Test public void contextLoads() { QueryWrapper<SysLog> queryWrapper = new QueryWrapper(); queryWrapper.inSql("f_log_id", "select f_log_id from t_sys_log where f_log_id < 10"); sysLogMapper.selectList(queryWrapper); }
[code] Time:25 ms - ID:com.banxun.demo.mapper.SysLogMapper.selectList Execute SQL: SELECT f_log_id AS logId, f_option_type AS optionType, f_option_person AS optionPerson, f_option_content AS optionContent, f_option_ip AS optionIp, f_option_type AS optionStatus, f_error_info AS errorInfo, f_option_time AS optionTime FROM t_sys_log WHERE f_log_id IN ( select f_log_id from t_sys_log where f_log_id < 10 )
输出打印SQL可以看到在where后拼接了f_log_id IN (select f_log_id from t_sys_log where f_log_id < 10),那isNotNull就是拼接了f_log_id not IN (select f_log_id from t_sys_log where f_log_id < 10)
-
notInSql 字段 NOT IN ( sql语句 )
-
groupBy 分组:GROUP BY 字段, ...
接口与默认方法:
[code]default Children groupBy(R column) { return this.groupBy(true, column); } default Children groupBy(R... columns) { return this.groupBy(true, columns); } Children groupBy(boolean condition, R... columns);
参数:
- condition:该条件是否加入生成的sql中
- column:数据库字段名
- columns:多个数据库字段名
实例:
[code]@Test public void contextLoads() { QueryWrapper<SysLog> queryWrapper = new QueryWrapper(); queryWrapper.groupBy("f_option_type", "f_option_person"); sysLogMapper.selectList(queryWrapper); }
[code] Time:26 ms - ID:com.banxun.demo.mapper.SysLogMapper.selectList Execute SQL: SELECT f_log_id AS logId, f_option_type AS optionType, f_option_person AS optionPerson, f_option_content AS optionContent, f_option_ip AS optionIp, f_option_type AS optionStatus, f_error_info AS errorInfo, f_option_time AS optionTime FROM t_sys_log GROUP BY f_option_type, f_option_person
-
orderByAsc 排序:ORDER BY 字段, ... DESC
接口与默认方法:
[code]default Children orderByDesc(R column) { return this.orderByDesc(true, column); } default Children orderByDesc(R... columns) { return this.orderByDesc(true, columns); } default Children orderByDesc(boolean condition, R... columns) { return this.orderBy(condition, false, columns); }
参数:
- condition:该条件是否加入生成的sql中
- column:数据库字段名
- columns:多个数据库字段名
实例:
[code]@Test public void contextLoads() { QueryWrapper<SysLog> queryWrapper = new QueryWrapper(); queryWrapper.orderByDesc("f_option_type", "f_option_person"); sysLogMapper.selectList(queryWrapper); }
[code] Time:30 ms - ID:com.banxun.demo.mapper.SysLogMapper.selectList Execute SQL: SELECT f_log_id AS logId, f_option_type AS optionType, f_option_person AS optionPerson, f_option_content AS optionContent, f_option_ip AS optionIp, f_option_type AS optionStatus, f_error_info AS errorInfo, f_option_time AS optionTime FROM t_sys_log ORDER BY f_option_type DESC , f_option_person DESC
-
orderByAsc 排序:ORDER BY 字段, ... DESC
-
orderBy 排序:ORDER BY 字段, ...
接口与默认方法:
[code]Children orderBy(boolean condition, boolean isAsc, R... columns);
参数:
- condition:该条件是否加入生成的sql中
- isAsc:数据库字段名
- columns:多个数据库字段名
实例:
[code]@Test public void contextLoads() { QueryWrapper<SysLog> queryWrapper = new QueryWrapper(); queryWrapper.orderBy(true,false,"f_option_type", "f_option_person"); sysLogMapper.selectList(queryWrapper); }
[code] Time:37 ms - ID:com.banxun.demo.mapper.SysLogMapper.selectList Execute SQL: SELECT f_log_id AS logId, f_option_type AS optionType, f_option_person AS optionPerson, f_option_content AS optionContent, f_option_ip AS optionIp, f_option_type AS optionStatus, f_error_info AS errorInfo, f_option_time AS optionTime FROM t_sys_log ORDER BY f_option_type DESC , f_option_person DESC
如果想一个字段ASC,一个DESC,可以调用一个orderByAsc,再调用orderByDesc
-
having HAVING ( sql语句 )
接口与默认方法:
[code]default Children having(String sqlHaving, Object... params) { return this.having(true, sqlHaving, params); } Children having(boolean condition, String sqlHaving, Object... params);
参数:
- condition:该条件是否加入生成的sql中
- isAsc:数据库字段名
- columns:多个数据库字段名
实例:
[code]@Test public void contextLoads() { QueryWrapper<SysLog> queryWrapper = new QueryWrapper(); queryWrapper.having(true,"count(1) > {0}", 10); sysLogMapper.selectCount(queryWrapper); }
[code]Time:19 ms - ID:com.banxun.demo.mapper.SysLogMapper.selectCount Execute SQL: SELECT COUNT( 1 ) FROM t_sys_log having count(1) > 10
-
or 拼接 OR
-
and 拼接 AND
-
nested 正常嵌套 不带 AND 或者 OR
接口与默认方法:
[code]default Children nested(Function<Param, Param> func) { return this.nested(true, func); } Children nested(boolean condition, Function<Param, Param> func);
参数:
- condition:该条件是否加入生成的sql中
- func:函数接口
实例:
[code]@Test public void contextLoads() { QueryWrapper<SysLog> queryWrapper = new QueryWrapper(); queryWrapper.nested(t-> t.eq("f_option_person", "admin").eq("f_option_type", "1")); sysLogMapper.selectCount(queryWrapper); }
[code] Time:32 ms - ID:com.banxun.demo.mapper.SysLogMapper.selectCount Execute SQL: SELECT COUNT( 1 ) FROM t_sys_log WHERE ( f_option_person = 'admin' AND f_option_type = '1' )
-
apply 拼接 sql
该方法可用于数据库函数 动态入参的
params对应前面applySql内部的{index}部分.这样是不会有sql注入风险的,反之会有!
接口与默认方法:
[code]default Children apply(String applySql, Object... value) { return this.apply(true, applySql, value); } Children apply(boolean condition, String applySql, Object... value);
参数:
- condition:该条件是否加入生成的sql中
- applySql:sql字符串
- value: 变量值
实例:
[code]@Test public void contextLoads() { QueryWrapper<SysLog> queryWrapper = new QueryWrapper(); queryWrapper.apply("date_format(f_option_time,'%Y-%m-%d') = '2019-08-26'"); sysLogMapper.selectCount(queryWrapper); }
[code] Time:21 ms - ID:com.banxun.demo.mapper.SysLogMapper.selectCount Execute SQL: SELECT COUNT( 1 ) FROM t_sys_log WHERE date_format(f_option_time,'%Y-%m-%d') = '2019-08-26'
-
last 无视优化规则直接拼接到 sql 的最后
接口与默认方法:
[code]default Children last(String lastSql) { return this.last(true, lastSql); } Children last(boolean condition, String lastSql);
参数:
- condition:该条件是否加入生成的sql中
- lastSql:sql字符串
实例:
[code]@Test public void contextLoads() { QueryWrapper<SysLog> queryWrapper = new QueryWrapper(); queryWrapper.last("limit 10"); sysLogMapper.selectCount(queryWrapper); }
[code] Time:28 ms - ID:com.banxun.demo.mapper.SysLogMapper.selectCount Execute SQL: SELECT COUNT( 1 ) FROM t_sys_log limit 10
-
exists 拼接 EXISTS ( sql语句 )
接口与默认方法:
[code]default Children exists(String existsSql) { return this.exists(true, existsSql); } Children exists(boolean condition, String existsSql);
参数:
- condition:该条件是否加入生成的sql中
- lastSql:sql字符串
实例:
[code]@Test public void contextLoads() { QueryWrapper<SysLog> queryWrapper = new QueryWrapper(); queryWrapper.exists("select f_log_id from t_sys_log where f_option_type = 1"); sysLogMapper.selectCount(queryWrapper); }
[code] Time:38 ms - ID:com.banxun.demo.mapper.SysLogMapper.selectCount Execute SQL: SELECT COUNT( 1 ) FROM t_sys_log WHERE EXISTS ( select f_log_id from t_sys_log where f_option_type = 1 )
-
notExists 拼接 NOT EXISTS ( sql语句 )
-
select 设置查询字段
接口与默认方法:
[code]Children select(R... columns); Children select(Predicate<TableFieldInfo> predicate); Children select(Class<T> entityClass, Predicate<TableFieldInfo> predicate);
参数:
- columns:多个字段名
- predicate:过滤字段
- entityClass:实体类
实例:
[code]@Test public void contextLoads() { QueryWrapper<SysLog> queryWrapper = new QueryWrapper(); queryWrapper.select("f_log_id","f_option_person"); sysLogMapper.selectList(queryWrapper); }
[code] Time:19 ms - ID:com.banxun.demo.mapper.SysLogMapper.selectList Execute SQL: SELECT f_log_id, f_option_person FROM t_sys_log
-
lambda 获取
LambdaWrapper
在
QueryWrapper中是获取
LambdaQueryWrapper
在
UpdateWrapper中是获取
LambdaUpdateWrapper
2、LambdaQueryWrapper
LambdaQueryWrapper支持使用JDK8的方法引用来使用对象属性构造条件,同时也支持上面所有方法。我们来看一个实例。
[code]@Test public void contextLoads() { QueryWrapper<SysLog> queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().select(SysLog::getLogId,SysLog::getOptionContent).like(SysLog::getOptionContent, "plus"); List<SysLog> sysLogs = sysLogMapper.selectList(queryWrapper); for (SysLog sysLog : sysLogs) { System.out.println(sysLog.getOptionContent()); } }
[code]Time:16 ms - ID:com.banxun.demo.mapper.SysLogMapper.selectList Execute SQL: SELECT f_log_id AS logId, f_option_content AS optionContent FROM t_sys_log WHERE f_option_content LIKE '%plus%'
可看到lambda()方法返回
LambdaQueryWrapper对象,这样可以很方便的使用方法引用来做查询。
三
、小结
本章节主要讲解条件构造器的名个方法的使用。下一章节,我们将介绍使用Mybatis-Plus的分页查询以及定义Mapper接口方法映射至xml的手动sql的CRUD操作与相关配置。
扫描下方二维码,关注微信公众号,掌握最新动态。与关注的同伴们一起学习,一起编程!
- 点赞
- 收藏
- 分享
- 文章举报
- Spring Boot + Mybatis-Plus 集成与使用(六)
- Spring Boot + Mybatis-Plus 集成与使用(五)
- Spring Boot + Mybatis-Plus 集成与使用(七)
- 使用SpringBoot搭建小型项目,集成mybatis,redis,swagger2,并部署在外部容器中。
- 架构实战项目心得(七):使用SpringBoot+Dubbo+Mybatisplus+Oracle搭建后台项目框架(一)
- 在springboot项目中使用mybatis 集成 Sharding-JDBC
- SpringBoot之集成mybatis:使用mybatis xml
- springboot+mybatisplus(使用总结,配置总结!!!干货...)
- spring boot 集成 mybatis 使用redis做二级缓存
- spring-boot-mybatis-plus使用心得
- spring-boot集成mybatis使用Druid监控
- (二十一)SpringBoot之集成mybatis:使用mybatis xml
- Spring Boot 使用Oracle集成Mybatis,驼峰映射(下划线)问题
- SpringBoot实践:集成Mybatis-Plus
- Mybatis-plus集成springboot
- Springboot-2.0.6.RELEASE版本集成Mybatis-plus及Mysql
- springboot2.0.5集成mybatis(PageHelper分页插件、generator插件使用)
- SpringBoot和mybatisplus的入门使用
- (二十)SpringBoot之集成mybatis:使用mybatis注解
- springboot 零xml集成mybatis-plus