前言
距离上一篇文章已经过去一个多月了,最近忙着项目上线,连轴转了一个多月,头发都少了不少!今天分享的是MP入门使用教程的最后的部分,也是使用的比较多的地方,就是如何使用MP进行条件查询!
正文
本文是在前两篇文章的基础之上进行拓展,所以如果有不清楚的可以先参考前面的两篇文章:SpringBoot系列:MybatisPlus的入门级使用教程(上),以及SpringBoot系列:MybatisPlus的入门级使用教程(中),源码地址会放在最后。
先来看一个使用MP进行条件查询的例子:
//构造条件构造器 QueryWrapper<User> wrapper = new QueryWrapper<>(); //构造条件 wrapper.eq("User_ID",ID); //使用提供的selectList默认方法进行结果查询 List<EduChapter> userList = baseMapper.selectList(wrapper); 复制代码
上述代码中首先使用QuerWrapper构造一个条件构造器,类型是需要查询的实体类User,然后使用条件构造器组装查询条件,上面是userid等于id,然后使用mp提供的默认的查询结果的selectList方法进行查询,条件就是上面构造的条件。
看起来很简单,这里主要就是使用QuerWrapper条件构造器构造查询条件,然后作为参数传入查询方法中即可。
接下来看看QuerWrapper的源码,源码比较长,这里截取部分,有需要的可以自行查看:
public class QueryWrapper<T> extends AbstractWrapper<T, String, QueryWrapper<T>> { ..... } 复制代码
可以发现这里QueryWrapper继承了AbstractWrapper抽象类,点进去看看AbstractWrapper抽象类的源码:
public abstract class AbstractWrapper<T, R, This extends AbstractWrapper<T, R, This>> extends Wrapper<T> implements Compare<This, R>, Nested<This>, Join<This>, Func<This, R> { private static final String MP_GENERAL_PARAMNAME = "MPGENVAL"; private static final String DEFAULT_PARAM_ALIAS = "ew"; private static final String PLACE_HOLDER = "{%s}"; private static final String MYBATIS_PLUS_TOKEN = "#{%s.paramNameValuePairs.%s}"; protected final This typedThis = this; protected AtomicInteger paramNameSeq; protected Map<String, Object> paramNameValuePairs; protected String paramAlias = null; protected String lastSql = ""; protected T entity; protected MergeSegments expression; protected Class<T> entityClass; public AbstractWrapper() { } public T getEntity() { return this.entity; } public This setEntity(T entity) { this.entity = entity; this.initEntityClass(); return this.typedThis; } protected void initEntityClass() { if (this.entity != null) { this.entityClass = this.entity.getClass(); } } protected Class<T> getCheckEntityClass() { Assert.notNull(this.entityClass, "entityClass must not null,please set entity before use this method!"); return this.entityClass; } public <V> This allEq(boolean condition, Map<R, V> params, boolean null2IsNull) { if (condition && CollectionUtils.isNotEmpty(params)) { params.forEach((k, v) -> { if (StringUtils.checkValNotNull(v)) { this.eq(k, v); } else if (null2IsNull) { this.isNull(k); } }); } return this.typedThis; } public <V> This 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; } public This eq(boolean condition, R column, Object val) { return this.addCondition(condition, column, SqlKeyword.EQ, val); } public This ne(boolean condition, R column, Object val) { return this.addCondition(condition, column, SqlKeyword.NE, val); } public This gt(boolean condition, R column, Object val) { return this.addCondition(condition, column, SqlKeyword.GT, val); } public This ge(boolean condition, R column, Object val) { return this.addCondition(condition, column, SqlKeyword.GE, val); } public This lt(boolean condition, R column, Object val) { return this.addCondition(condition, column, SqlKeyword.LT, val); } public This le(boolean condition, R column, Object val) { return this.addCondition(condition, column, SqlKeyword.LE, val); } public This like(boolean condition, R column, Object val) { return this.doIt(condition, () -> { return this.columnToString(column); }, SqlKeyword.LIKE, () -> { return this.formatSql("{0}", "%" + val + "%"); }); } public This notLike(boolean condition, R column, Object val) { return this.not(condition).like(condition, column, val); } public This likeLeft(boolean condition, R column, Object val) { return this.doIt(condition, () -> { return this.columnToString(column); }, SqlKeyword.LIKE, () -> { return this.formatSql("{0}", "%" + val); }); } public This likeRight(boolean condition, R column, Object val) { return this.doIt(condition, () -> { return this.columnToString(column); }, SqlKeyword.LIKE, () -> { return this.formatSql("{0}", val + "%"); }); } public This between(boolean condition, R column, Object val1, Object val2) { return this.doIt(condition, () -> { return this.columnToString(column); }, SqlKeyword.BETWEEN, () -> { return this.formatSql("{0}", val1); }, SqlKeyword.AND, () -> { return this.formatSql("{0}", val2); }); } public This notBetween(boolean condition, R column, Object val1, Object val2) { return this.not(condition).between(condition, column, val1, val2); } public This and(boolean condition, Function<This, This> func) { return this.and(condition).addNestedCondition(condition, func); } public This or(boolean condition, Function<This, This> func) { return this.or(condition).addNestedCondition(condition, func); } public This nested(boolean condition, Function<This, This> func) { return this.addNestedCondition(condition, func); } public This or(boolean condition) { return this.doIt(condition, SqlKeyword.OR); } public This apply(boolean condition, String applySql, Object... value) { return this.doIt(condition, WrapperKeyword.APPLY, () -> { return this.formatSql(applySql, value); }); } public This last(boolean condition, String lastSql) { if (condition) { this.lastSql = " " + lastSql; } return this.typedThis; } public This exists(boolean condition, String existsSql) { return this.doIt(condition, SqlKeyword.EXISTS, () -> { return String.format("(%s)", existsSql); }); } public This notExists(boolean condition, String notExistsSql) { return this.not(condition).exists(condition, notExistsSql); } public This isNull(boolean condition, R column) { return this.doIt(condition, () -> { return this.columnToString(column); }, SqlKeyword.IS_NULL); } public This isNotNull(boolean condition, R column) { return this.doIt(condition, () -> { return this.columnToString(column); }, SqlKeyword.IS_NOT_NULL); } public This in(boolean condition, R column, Collection<?> value) { return CollectionUtils.isEmpty(value) ? this.typedThis : this.doIt(condition, () -> { return this.columnToString(column); }, SqlKeyword.IN, this.inExpression(value)); } public This notIn(boolean condition, R column, Collection<?> value) { return CollectionUtils.isEmpty(value) ? this.typedThis : this.not(condition).in(condition, column, value); } public This inSql(boolean condition, R column, String inValue) { return this.doIt(condition, () -> { return this.columnToString(column); }, SqlKeyword.IN, () -> { return String.format("(%s)", inValue); }); } public This notInSql(boolean condition, R column, String inValue) { return this.not(condition).inSql(condition, column, inValue); } public This groupBy(boolean condition, R... columns) { return ArrayUtils.isEmpty(columns) ? this.typedThis : this.doIt(condition, SqlKeyword.GROUP_BY, () -> { return this.columnsToString(columns); }); } public This orderBy(boolean condition, boolean isAsc, R... columns) { if (ArrayUtils.isEmpty(columns)) { return this.typedThis; } else { SqlKeyword mode = isAsc ? SqlKeyword.ASC : SqlKeyword.DESC; Object[] var5 = columns; int var6 = columns.length; for(int var7 = 0; var7 < var6; ++var7) { R column = var5[var7]; this.doIt(condition, SqlKeyword.ORDER_BY, () -> { return this.columnToString(column); }, mode); } return this.typedThis; } } public This having(boolean condition, String sqlHaving, Object... params) { return this.doIt(condition, SqlKeyword.HAVING, () -> { return this.formatSqlIfNeed(condition, sqlHaving, params); }); } protected This not(boolean condition) { return this.doIt(condition, SqlKeyword.NOT); } protected This and(boolean condition) { return this.doIt(condition, SqlKeyword.AND); } protected This addCondition(boolean condition, R column, SqlKeyword sqlKeyword, Object val) { return this.doIt(condition, () -> { return this.columnToString(column); }, sqlKeyword, () -> { return this.formatSql("{0}", val); }); } protected This addNestedCondition(boolean condition, Function<This, This> func) { return this.doIt(condition, WrapperKeyword.LEFT_BRACKET, (ISqlSegment)func.apply(this.instance(this.paramNameSeq, this.paramNameValuePairs)), WrapperKeyword.RIGHT_BRACKET); } protected abstract This instance(AtomicInteger var1, Map<String, Object> var2); protected final String formatSql(String sqlStr, Object... params) { return this.formatSqlIfNeed(true, sqlStr, params); } protected final String formatSqlIfNeed(boolean need, String sqlStr, Object... params) { if (need && !StringUtils.isEmpty(sqlStr)) { if (ArrayUtils.isNotEmpty(params)) { for(int i = 0; i < params.length; ++i) { String genParamName = "MPGENVAL" + this.paramNameSeq.incrementAndGet(); sqlStr = sqlStr.replace(String.format("{%s}", i), String.format("#{%s.paramNameValuePairs.%s}", this.getParamAlias(), genParamName)); this.paramNameValuePairs.put(genParamName, params[i]); } } return sqlStr; } else { return null; } } private ISqlSegment inExpression(Collection<?> value) { return () -> { return (String)value.stream().map((i) -> { return this.formatSql("{0}", i); }).collect(Collectors.joining(",", "(", ")")); }; } protected final void initNeed() { this.paramNameSeq = new AtomicInteger(0); this.paramNameValuePairs = new HashMap(16); this.expression = new MergeSegments(); } protected This doIt(boolean condition, ISqlSegment... sqlSegments) { if (condition) { this.expression.add(sqlSegments); } return this.typedThis; } public String getParamAlias() { return StringUtils.isEmpty(this.paramAlias) ? "ew" : this.paramAlias; } public String getSqlSegment() { String sqlSegment = this.expression.getSqlSegment(); if (StringUtils.isNotEmpty(sqlSegment)) { return sqlSegment + this.lastSql; } else { return StringUtils.isNotEmpty(this.lastSql) ? this.lastSql : null; } } public MergeSegments gt() { return this.expression; } public Map<String, Object> getParamNameValuePairs() { return this.paramNameValuePairs; } protected String columnsToString(R... columns) { return (String)Arrays.stream(columns).map(this::columnToString).collect(Collectors.joining(",")); } protected abstract String columnToString(R var1); public This clone() { return (AbstractWrapper)SerializationUtils.clone(this.typedThis); } } 复制代码
AbstractWrapper继承了Wrapper抽象类,并且AbstractWrapper中提供了eq、gt、ge等条件判断方法,以及getExpression()生成条件表达式方法。类以及接口之间的关系如下图:
总结如下:
Wrapper : 它是条件构造抽象类,也是最顶端的父类;
AbstractWrapper : 也是条件构造器抽象类,继承了Wrapper 抽象类,它主要用于查询条件封装,生成 sql 的 where 条件
QueryWrapper : 是条件构造器的实体类,是对各类的Entity对象封装的操作类;
可以发现在AbstractWrapper类中有各类的ge、gt、le、lt、isNull、isNotNull、eq、ne等方法,它们等同于,sql语句中的大于、等于、小于等条件,比如
wrapper.eq("User_ID",ID); 复制代码
这行代码就表示,条件是user_ID字段的值等于ID,源码如下:
public This eq(boolean condition, R column, Object val) { return this.addCondition(condition, column, SqlKeyword.EQ, val); } 复制代码
eq这类的方法的第一个参数是condition表示该条件是否加入生成的sql中,默认以及不写就是true,第二个参数表示的是字段名(注意需要和数据库中的字段名一样),第三个参数就是字段的值。
所以可以发现条件构造器最主要的就是eq、ge、gt这类方法的使用。它们大多数都是一样的,这里简单演示几种:
比如allEq,源码可以参考上面AbstractWrapper的源码:
QueryWrapper<User> queryWrapper = new QueryWrapper<>(); Map<String, Object> map = new HashMap<>(); map.put("id", 2); map.put("name", "Jack"); map.put("age", 20); queryWrapper.allEq(map); List<User> users = userMapper.selectList(queryWrapper); users.forEach(System.out::println); 复制代码
like的使用:
QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper .like("address","上海") .notLike("name", "e") .likeRight("email", "t"); List<Map<String, Object>> maps = userMapper.selectMaps(queryWrapper);//返回值是Map列表 maps.forEach(System.out::println); 复制代码
or、and的使用:如果不调用or则默认为使用 and
//修改值 User user = new User(); user.setAge(99); user.setName("Andy"); //修改条件 UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>(); userUpdateWrapper .like("name", "h") .or() .between("age", 20, 30); int result = userMapper.update(user, userUpdateWrapper); 复制代码
这里使用的是 UpdateWrapper, 它主要用于Update 条件封装,对Entity对象进行更新操作。
还可以使用select方法指定需要查询的字段,这里就是指定查询的id,name,age:
QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.select("id", "name", "age"); List<User> users = userMapper.selectList(queryWrapper); users.forEach(System.out::println); 复制代码
set、setSql:
//修改值 User user = new User(); user.setAge(99); //修改条件 UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>(); userUpdateWrapper .like("name", "h") .set("name", "老李头")//除了可以查询还可以使用set设置修改的字段 .setSql(" email = '123@qq.com'");//可以有子查询 int result = userMapper.update(user, userUpdateWrapper); 复制代码
最终的sql会合并 user.setAge(),以及 userUpdateWrapper.set() 和 setSql() 中 的字段。等同于:
UPDATE user SET age=99, name="老李头", email = '123@qq.com' WHERE deleted=0 AND name LIKE "h" 复制代码
总结
以上就是关于mybatisplus的条件构造器的使用,至此mybatisplus的介绍大结局了,要想掌握更多的mp的使用,还是要在日常的开发中多多练习。更多关于mybatisplus的内容可以参考以下内容:
mybatisplus源码地址: https://gitcode.net/mirrors/baomidou/mybatis-plus?utm_source=csdn_github_accelerator MP官网地址: https://baomidou.com/pages/24112f/ 复制代码