mybatis plusにおけるEntityWrapperソースコード解読


mybatis plusには多くのCRUDが内蔵されており、その中でEntityWrapperというクラスがあります.このクラスはmybatis plusが私たちに書いてくれた多くのインタフェースで、dao層で書く方法がxmlで実現されているようにしています.では、この友好的なクラスは私たちにどのような方法を実現したのでしょうか.今日はソースコードを見て、具体的に話しましょう.
/**
 * Copyright (c) 2011-2014, hubin ([email protected]).
 * 

* Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at *

* http://www.apache.org/licenses/LICENSE-2.0 *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */

package com.baomidou.mybatisplus.mapper; import java.io.Serializable; import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; import com.baomidou.mybatisplus.enums.SqlLike; import com.baomidou.mybatisplus.exceptions.MybatisPlusException; import com.baomidou.mybatisplus.toolkit.ArrayUtils; import com.baomidou.mybatisplus.toolkit.CollectionUtils; import com.baomidou.mybatisplus.toolkit.MapUtils; import com.baomidou.mybatisplus.toolkit.SqlUtils; import com.baomidou.mybatisplus.toolkit.StringUtils; /** *

* , T-SQL *

* * @author hubin , yanghu , Dyang , Caratacus * @Date 2016-11-7 */
@SuppressWarnings("serial") public abstract class Wrapper<T> implements Serializable { /** * */ private static final String PLACE_HOLDER = "{%s}"; private static final String MYBATIS_PLUS_TOKEN = "#{%s.paramNameValuePairs.%s}"; private static final String MP_GENERAL_PARAMNAME = "MPGENVAL"; private static final String DEFAULT_PARAM_ALIAS = "ew"; protected String paramAlias = null; /** * SQL , :id,name,age */ protected String sqlSelect = null; /** * TSQL SQL */ protected SqlPlus sql = new SqlPlus(); /** * sql WHERE OR AND OR OR */ protected Boolean isWhere; /** * WHERE AND OR */ protected String AND_OR = "AND"; private Map<String, Object> paramNameValuePairs = new HashMap<>(4); private AtomicInteger paramNameSeq = new AtomicInteger(0); /** * EntityWrapper * * @return */ public T getEntity() { return null; } public String getSqlSelect() { if (StringUtils.isEmpty(sqlSelect)) { return null; } return stripSqlInjection(sqlSelect); } public Wrapper<T> setSqlSelect(String sqlSelect) { if (StringUtils.isNotEmpty(sqlSelect)) { this.sqlSelect = sqlSelect; } return this; } /** * SQL ( ) */ public abstract String getSqlSegment(); public String toString() { String sqlSegment = getSqlSegment(); if (StringUtils.isNotEmpty(sqlSegment)) { sqlSegment = sqlSegment.replaceAll("#\\{" + getParamAlias() + ".paramNameValuePairs.MPGENVAL[0-9]+}", "\\?"); } return sqlSegment; } /** *

* SQL WHERE *

*

* eg: ew.where("name='zhangsan'").where("id={0}","123"); *

* : WHERE (NAME='zhangsan' AND id=123) *

* * @param sqlWhere where * @param params * @return this */
public Wrapper<T> where(String sqlWhere, Object... params) { sql.WHERE(formatSql(sqlWhere, params)); return this; } /** *

* SQL "field=value" *

* * @param column * @param params * @return */
public Wrapper<T> eq(String column, Object params) { sql.WHERE(formatSql(String.format("%s = {0}", column), params)); return this; } /** *

* SQL "field <> value" *

* * @param column * @param params * @return */
public Wrapper<T> ne(String column, Object params) { sql.WHERE(formatSql(String.format("%s <> {0}", column), params)); return this; } /** *

* SQL "field=value" *

* * @param params * @return */
@SuppressWarnings({"rawtypes", "unchecked"}) public Wrapper<T> allEq(Map<String, Object> params) { if (MapUtils.isNotEmpty(params)) { Iterator iterator = params.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry<String, Object> entry = (Map.Entry<String, Object>) iterator.next(); Object value = entry.getValue(); if (StringUtils.checkValNotNull(value)) { sql.WHERE(formatSql(String.format("%s = {0}", entry.getKey()), entry.getValue())); } } } return this; } /** *

* SQL "field>value" *

* * @param column * @param params * @return */
public Wrapper<T> gt(String column, Object params) { sql.WHERE(formatSql(String.format("%s > {0}", column), params)); return this; } /** *

* SQL "field>=value" *

* * @param column * @param params * @return */
public Wrapper<T> ge(String column, Object params) { sql.WHERE(formatSql(String.format("%s >= {0}", column), params)); return this; } /** *

* SQL "field * * @param column * @param params * @return */

public Wrapper<T> lt(String column, Object params) { sql.WHERE(formatSql(String.format("%s < {0}", column), params)); return this; } /** *

* SQL "field<=value" *

* * @param column * @param params * @return */
public Wrapper<T> le(String column, Object params) { sql.WHERE(formatSql(String.format("%s <= {0}", column), params)); return this; } /** *

* AND *

* * @param sqlAnd and * @param params * @return this */
public Wrapper<T> and(String sqlAnd, Object... params) { sql.AND().WHERE(formatSql(sqlAnd, params)); return this; } /** *

* AND *

*

* eg: ew.where("name='zhangsan'").and("id=11").andNew("statu=1"); : WHERE * (name='zhangsan' AND id=11) AND (statu=1) *

* * @param sqlAnd AND * @param params * @return this */
public Wrapper<T> andNew(String sqlAnd, Object... params) { sql.AND_NEW().WHERE(formatSql(sqlAnd, params)); return this; } /** *

* AND *

*

* * @return this */

public Wrapper<T> and() { sql.AND_NEW(); return this; } /** *

* OR *

* * @return this */
public Wrapper<T> or() { sql.OR_NEW(); return this; } /** *

* OR *

* * @param sqlOr or * @param params * @return this */
public Wrapper<T> or(String sqlOr, Object... params) { if (StringUtils.isEmpty(sql.toString())) { AND_OR = "OR"; } sql.OR().WHERE(formatSql(sqlOr, params)); return this; } /** *

* OR , () *

*

* eg: ew.where("name='zhangsan'").and("id=11").orNew("statu=1"); : WHERE * (name='zhangsan' AND id=11) OR (statu=1) *

* * @param sqlOr AND * @param params * @return this */
public Wrapper<T> orNew(String sqlOr, Object... params) { if (StringUtils.isEmpty(sql.toString())) { AND_OR = "OR"; } sql.OR_NEW().WHERE(formatSql(sqlOr, params)); return this; } /** *

* SQL groupBy *

*

* eg: ew.where("name='zhangsan'").groupBy("id,name") *

* * @param columns SQL Group by , Group By * @return this */
public Wrapper<T> groupBy(String columns) { sql.GROUP_BY(columns); return this; } /** *

* SQL having *

*

* eg: ew.groupBy("id,name").having("id={0}",22).and("password is not null") *

* * @param sqlHaving having * @param params * @return EntityWrapper */
public Wrapper<T> having(String sqlHaving, Object... params) { sql.HAVING(formatSql(sqlHaving, params)); return this; } /** *

* SQL orderby *

*

* eg: ew.groupBy("id,name").having("id={0}",22).and("password is not null" * ).orderBy("id,name") *

* * @param columns SQL order by , Order By * @return this */
public Wrapper<T> orderBy(String columns) { sql.ORDER_BY(columns); return this; } /** *

* SQL orderby , *

* * @param columns SQL order by , Order By * @param isAsc * @return this */
public Wrapper<T> orderBy(String columns, boolean isAsc) { if (StringUtils.isNotEmpty(columns)) { sql.ORDER_BY(columns + (isAsc ? " ASC" : " DESC")); } return this; } /** * LIKE ,value % * * @param column * @param value * @return this */ public Wrapper<T> like(String column, String value) { handerLike(column, value, SqlLike.DEFAULT, false); return this; } /** * NOT LIKE ,value % * * @param column * @param value * @return this */ public Wrapper<T> notLike(String column, String value) { handerLike(column, value, SqlLike.DEFAULT, true); return this; } /** * LIKE * * @param column * @param value like * @param isNot NOT LIKE */ private void handerLike(String column, String value, SqlLike type, boolean isNot) { if (StringUtils.isNotEmpty(column) && StringUtils.isNotEmpty(value)) { StringBuilder inSql = new StringBuilder(); inSql.append(column); if (isNot) { inSql.append(" NOT"); } inSql.append(" LIKE {0}"); sql.WHERE(formatSql(inSql.toString(), SqlUtils.concatLike(value, type))); } } /** * LIKE ,value % * * @param column * @param value * @param type * @return this */ public Wrapper<T> like(String column, String value, SqlLike type) { handerLike(column, value, type, false); return this; } /** * NOT LIKE ,value % * * @param column * @param value * @param type * @return this */ public Wrapper<T> notLike(String column, String value, SqlLike type) { handerLike(column, value, type, true); return this; } /** * is not null * * @param columns 。 。 * @return this */ public Wrapper<T> isNotNull(String columns) { sql.IS_NOT_NULL(columns); return this; } /** * is not null * * @param columns 。 。 * @return this */ public Wrapper<T> isNull(String columns) { sql.IS_NULL(columns); return this; } /** * EXISTS , mysql oracle * * @param value * @return this */ public Wrapper<T> exists(String value) { sql.EXISTS(value); return this; } /** * NOT EXISTS * * @param value * @return this */ public Wrapper<T> notExists(String value) { sql.NOT_EXISTS(value); return this; } /** * IN , mysql oracle * * @param column * @param value * @return this */ public Wrapper<T> in(String column, String value) { if (StringUtils.isNotEmpty(value)) { in(column, StringUtils.splitWorker(value, ",", -1, false)); } return this; } /** * NOT IN * * @param column * @param value * @return this */ public Wrapper<T> notIn(String column, String value) { if (StringUtils.isNotEmpty(value)) { notIn(column, StringUtils.splitWorker(value, ",", -1, false)); } return this; } /** * IN , mysql oracle * * @param column * @param value List * @return this */ public Wrapper<T> in(String column, Collection<?> value) { if (CollectionUtils.isNotEmpty(value)) sql.WHERE(formatSql(inExpression(column, value, false), value.toArray())); return this; } /** * NOT IN , mysql oracle * * @param column * @param value List * @return this */ public Wrapper<T> notIn(String column, Collection<?> value) { if (CollectionUtils.isNotEmpty(value)) sql.WHERE(formatSql(inExpression(column, value, true), value.toArray())); return this; } /** * IN , mysql oracle * * @param column * @param value object * @return this */ public Wrapper<T> in(String column, Object[] value) { if (ArrayUtils.isNotEmpty(value)) sql.WHERE(formatSql(inExpression(column, Arrays.asList(value), false), value)); return this; } /** * NOT IN , mysql oracle * * @param column * @param value object * @return this */ public Wrapper<T> notIn(String column, Object... value) { if (ArrayUtils.isNotEmpty(value)) sql.WHERE(formatSql(inExpression(column, Arrays.asList(value), true), value)); return this; } /** * in * * @param column * @param value List * @param isNot NOT IN */ private String inExpression(String column, Collection<?> value, boolean isNot) { if (StringUtils.isNotEmpty(column) && CollectionUtils.isNotEmpty(value)) { StringBuilder inSql = new StringBuilder(); inSql.append(column); if (isNot) { inSql.append(" NOT"); } inSql.append(" IN "); inSql.append("("); int size = value.size(); for (int i = 0; i < size; i++) { inSql.append(String.format(PLACE_HOLDER, i)); if (i + 1 < size) { inSql.append(","); } } inSql.append(")"); return inSql.toString(); } return null; } /** * betwwee * * @param column * @param val1 * @param val2 * @return this */ public Wrapper<T> between(String column, Object val1, Object val2) { sql.WHERE(formatSql(String.format("%s BETWEEN {0} AND {1}", column), val1, val2)); return this; } /** * NOT betwwee * * @param column * @param val1 * @param val2 * @return this */ public Wrapper<T> notBetween(String column, Object val1, Object val2) { sql.WHERE(formatSql(String.format("%s NOT BETWEEN {0} AND {1}", column), val1, val2)); return this; } /** * , where() and() * * @param sqlWhere where sql * @param params * @return this */ public Wrapper<T> addFilter(String sqlWhere, Object... params) { return and(sqlWhere, params); } /** *

* andIf() *

*

* eg: ew.filterIfNeed(false,"name='zhangsan'").where("name='zhangsan'") * .filterIfNeed(true,"id={0}",22) *

* : WHERE (name='zhangsan' AND id=22) *

* * @param need * @param sqlWhere * @param params * @return this */
public Wrapper<T> addFilterIfNeed(boolean need, String sqlWhere, Object... params) { return need ? where(sqlWhere, params) : this; } /** *

* SQL *

* * @param value * @return this */
protected String stripSqlInjection(String value) { return value.replaceAll("('.+--)|(--)|(\\|)|(%7C)", ""); } /** *

* SQL *

* * @param sqlStr SQL * @param params * @return this */
protected String formatSql(String sqlStr, Object... params) { return formatSqlIfNeed(true, sqlStr, params); } /** *

* SQL
*
* Format SQL for methods: EntityWrapper.where/and/or...("name={0}", value); * ALL the {i} will be replaced with #{MPGENVALi}
*
* ew.where("sample_name={0}", "haha").and("sample_age >{0} * and sample_age<{1}", 18, 30) TO * sample_name=#{MPGENVAL1} and sample_age>#{MPGENVAL2} and * sample_age<#{MPGENVAL3}
*

* * @param need * @param sqlStr SQL * @param params * @return this */
protected String formatSqlIfNeed(boolean need, String sqlStr, Object... params) { if (!need || StringUtils.isEmpty(sqlStr)) { return null; } // #200 if (ArrayUtils.isNotEmpty(params)) { for (int i = 0; i < params.length; ++i) { String genParamName = MP_GENERAL_PARAMNAME + paramNameSeq.incrementAndGet(); sqlStr = sqlStr.replace(String.format(PLACE_HOLDER, i), String.format(MYBATIS_PLUS_TOKEN, getParamAlias(), genParamName)); paramNameValuePairs.put(genParamName, params[i]); } } return sqlStr; } /** *

* sql `WHERE` OR `AND` OR `OR` *

* * @param bool * @return this */
public Wrapper<T> isWhere(Boolean bool) { this.isWhere = bool; return this; } /** *

* SQL LIMIT *

* * @param begin * @param end * @return this */
public Wrapper<T> limit(int begin, int end) { sql.LIMIT(begin, end); return this; } /** * Fix issue 200. * * @return * @since 2.0.3 */ public Map<String, Object> getParamNameValuePairs() { return paramNameValuePairs; } public String getParamAlias() { return StringUtils.isEmpty(paramAlias) ? DEFAULT_PARAM_ALIAS : paramAlias; } /** *

* *

* * @param paramAlias * @return */
public Wrapper<T> setParamAlias(String paramAlias) { if (StringUtils.isNotEmpty(getSqlSegment())) { throw new MybatisPlusException("Error: Please call this method when initializing!"); } if (StringUtils.isNotEmpty(this.paramAlias)) { throw new MybatisPlusException("Error: Please do not call the method repeatedly!"); } this.paramAlias = paramAlias; return this; } }

最後に、ソースを読むと、多くの収穫が得られ、他の人の処理方法を見ると、あなたは大きく進歩します.ははは、今日あなたは学びましたか