Pārlūkot izejas kodu

支持 WHERE 实体条件自定义运算规则

= 7 gadi atpakaļ
vecāks
revīzija
1ef4f40851

+ 17 - 18
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/mapper/LogicSqlInjector.java

@@ -57,7 +57,7 @@ public class LogicSqlInjector extends AutoSqlInjector {
                 idStr = ids.toString();
             }
             String sql = String.format(sqlMethod.getSql(), table.getTableName(), sqlLogicSet(table),
-                    table.getKeyColumn(), idStr);
+                table.getKeyColumn(), idStr);
             sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
             this.addUpdateMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource);
         } else {
@@ -75,7 +75,7 @@ public class LogicSqlInjector extends AutoSqlInjector {
             // 逻辑删除注入
             SqlMethod sqlMethod = SqlMethod.LOGIC_DELETE;
             String sql = String.format(sqlMethod.getSql(), table.getTableName(), sqlLogicSet(table),
-                    sqlWhereEntityWrapper(table));
+                sqlWhereEntityWrapper(table));
             SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
             this.addUpdateMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource);
         } else {
@@ -93,7 +93,7 @@ public class LogicSqlInjector extends AutoSqlInjector {
             // 逻辑删除注入
             SqlMethod sqlMethod = SqlMethod.LOGIC_DELETE_BY_MAP;
             String sql = String.format(sqlMethod.getSql(), table.getTableName(), sqlLogicSet(table),
-                    sqlWhereByMap(table));
+                sqlWhereByMap(table));
             SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, Map.class);
             this.addUpdateMappedStatement(mapperClass, Map.class, sqlMethod.getMethod(), sqlSource);
         } else {
@@ -124,10 +124,10 @@ public class LogicSqlInjector extends AutoSqlInjector {
                 ids.append("#{item}");
                 ids.append("\n</foreach>");
                 sqlSource = languageDriver.createSqlSource(configuration, String.format(sqlMethod.getSql(), sqlSelectColumns(table, false),
-                        table.getTableName(), table.getKeyColumn(), ids.toString(), getLogicDeleteSql(table)), modelClass);
+                    table.getTableName(), table.getKeyColumn(), ids.toString(), getLogicDeleteSql(table)), modelClass);
             } else {
                 sqlSource = new RawSqlSource(configuration, String.format(sqlMethod.getSql(), sqlSelectColumns(table, false), table.getTableName(),
-                        table.getKeyColumn(), table.getKeyProperty(), getLogicDeleteSql(table)), Object.class);
+                    table.getKeyColumn(), table.getKeyProperty(), getLogicDeleteSql(table)), Object.class);
             }
             this.addSelectMappedStatement(mapperClass, sqlMethod.getMethod(), sqlSource, modelClass, table);
         } else {
@@ -150,14 +150,14 @@ public class LogicSqlInjector extends AutoSqlInjector {
         if (table.isLogicDelete()) {
             SqlMethod sqlMethod = selective ? SqlMethod.LOGIC_UPDATE_BY_ID : SqlMethod.LOGIC_UPDATE_ALL_COLUMN_BY_ID;
             String sql = String.format(sqlMethod.getSql(), table.getTableName(), sqlSet(selective, table, "et."),
-                    table.getKeyColumn(),
-                    "et." + table.getKeyProperty(),
-                    "<if test=\"et instanceof java.util.Map\">" +
-                            "<if test=\"et.MP_OPTLOCK_VERSION_ORIGINAL!=null\">"
-                            + "and ${et.MP_OPTLOCK_VERSION_COLUMN}=#{et.MP_OPTLOCK_VERSION_ORIGINAL}"
-                            + "</if>"
-                            + "</if>" +
-                            getLogicDeleteSql(table)
+                table.getKeyColumn(),
+                "et." + table.getKeyProperty(),
+                "<if test=\"et instanceof java.util.Map\">" +
+                    "<if test=\"et.MP_OPTLOCK_VERSION_ORIGINAL!=null\">"
+                    + "and ${et.MP_OPTLOCK_VERSION_COLUMN}=#{et.MP_OPTLOCK_VERSION_ORIGINAL}"
+                    + "</if>"
+                    + "</if>" +
+                    getLogicDeleteSql(table)
             );
             SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
             this.addUpdateMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource);
@@ -235,8 +235,8 @@ public class LogicSqlInjector extends AutoSqlInjector {
             }
             for (TableFieldInfo fieldInfo : fieldList) {
                 where.append(convertIfTag(fieldInfo, "ew.", false));
-                where.append(" AND ").append(fieldInfo.getColumn()).append("=#{ew.");
-                where.append(fieldInfo.getEl()).append("}");
+                where.append(" AND ").append(this.sqlCondition(fieldInfo.getCondition(),
+                    fieldInfo.getColumn(), "ew." + fieldInfo.getEl()));
                 where.append(convertIfTag(fieldInfo, true));
             }
             // 过滤逻辑
@@ -264,8 +264,8 @@ public class LogicSqlInjector extends AutoSqlInjector {
             List<TableFieldInfo> fieldList = table.getFieldList();
             for (TableFieldInfo fieldInfo : fieldList) {
                 where.append(convertIfTag(fieldInfo, "ew.entity.", false));
-                where.append(" AND ").append(fieldInfo.getColumn()).append("=#{ew.entity.");
-                where.append(fieldInfo.getEl()).append("}");
+                where.append(" AND ").append(this.sqlCondition(fieldInfo.getCondition(),
+                    fieldInfo.getColumn(), "ew.entity." + fieldInfo.getEl()));
                 where.append(convertIfTag(fieldInfo, true));
             }
             where.append("\n</if>");
@@ -279,7 +279,6 @@ public class LogicSqlInjector extends AutoSqlInjector {
         return super.sqlWhereEntityWrapper(table);
     }
 
-
     @Override
     protected String sqlWhereByMap(TableInfo table) {
         if (table.isLogicDelete()) {

+ 11 - 0
mybatis-plus-support/src/main/java/com/baomidou/mybatisplus/annotations/TableField.java

@@ -22,6 +22,7 @@ import java.lang.annotation.Target;
 
 import com.baomidou.mybatisplus.enums.FieldFill;
 import com.baomidou.mybatisplus.enums.FieldStrategy;
+import com.baomidou.mybatisplus.mapper.SqlCondition;
 
 /**
  * <p>
@@ -63,6 +64,16 @@ public @interface TableField {
      */
     boolean exist() default true;
 
+    /**
+     * <p>
+     * 字段 WHERE 实体查询比较条件
+     * </p>
+     * <p>
+     * 默认 `=` 等值
+     * </p>
+     */
+    String condition() default SqlCondition.EQUALS;
+
     /**
      * <p>
      * 字段验证策略

+ 15 - 0
mybatis-plus-support/src/main/java/com/baomidou/mybatisplus/entity/TableFieldInfo.java

@@ -21,6 +21,7 @@ import com.baomidou.mybatisplus.annotations.TableField;
 import com.baomidou.mybatisplus.annotations.TableLogic;
 import com.baomidou.mybatisplus.enums.FieldFill;
 import com.baomidou.mybatisplus.enums.FieldStrategy;
+import com.baomidou.mybatisplus.mapper.SqlCondition;
 import com.baomidou.mybatisplus.toolkit.SqlReservedWords;
 import com.baomidou.mybatisplus.toolkit.StringUtils;
 
@@ -76,6 +77,11 @@ public class TableFieldInfo {
      */
     private String logicNotDeleteValue;
 
+    /**
+     * 字段比较条件
+     */
+    private String condition = SqlCondition.EQUALS;
+
     /**
      * 字段填充策略
      */
@@ -117,6 +123,7 @@ public class TableFieldInfo {
             this.fieldStrategy = globalConfig.getFieldStrategy();
         }
         tableInfo.setLogicDelete(this.initLogicDelete(globalConfig, field));
+        this.condition = tableField.condition();
         /*
          * 保存当前字段的填充策略
 		 */
@@ -245,6 +252,14 @@ public class TableFieldInfo {
         this.logicNotDeleteValue = logicNotDeleteValue;
     }
 
+    public String getCondition() {
+        return condition;
+    }
+
+    public void setCondition(String condition) {
+        this.condition = condition;
+    }
+
     public FieldFill getFieldFill() {
         return fieldFill;
     }

+ 13 - 2
mybatis-plus-support/src/main/java/com/baomidou/mybatisplus/mapper/AutoSqlInjector.java

@@ -492,6 +492,15 @@ public class AutoSqlInjector implements ISqlInjector {
         this.addSelectMappedStatement(mapperClass, sqlMethod.getMethod(), sqlSource, Integer.class, null);
     }
 
+    /**
+     * <p>
+     * Sql 运算条件
+     * </p>
+     */
+    protected String sqlCondition(String condition, String column, String property) {
+        return String.format(condition, column, property);
+    }
+
     /**
      * <p>
      * EntityWrapper方式获取select where
@@ -513,7 +522,8 @@ public class AutoSqlInjector implements ISqlInjector {
         List<TableFieldInfo> fieldList = table.getFieldList();
         for (TableFieldInfo fieldInfo : fieldList) {
             where.append(convertIfTag(fieldInfo, "ew.entity.", false));
-            where.append(" AND ").append(fieldInfo.getColumn()).append("=#{ew.entity.").append(fieldInfo.getEl()).append("}");
+            where.append(" AND ").append(this.sqlCondition(fieldInfo.getCondition(),
+                fieldInfo.getColumn(), "ew.entity." + fieldInfo.getEl()));
             where.append(convertIfTag(fieldInfo, true));
         }
         where.append("\n</if>");
@@ -716,7 +726,8 @@ public class AutoSqlInjector implements ISqlInjector {
         List<TableFieldInfo> fieldList = table.getFieldList();
         for (TableFieldInfo fieldInfo : fieldList) {
             where.append(convertIfTag(fieldInfo, "ew.", false));
-            where.append(" AND ").append(fieldInfo.getColumn()).append("=#{ew.").append(fieldInfo.getEl()).append("}");
+            where.append(" AND ").append(this.sqlCondition(fieldInfo.getCondition(),
+                fieldInfo.getColumn(), "ew." + fieldInfo.getEl()));
             where.append(convertIfTag(fieldInfo, true));
         }
         where.append("\n</where>");

+ 46 - 0
mybatis-plus-support/src/main/java/com/baomidou/mybatisplus/mapper/SqlCondition.java

@@ -0,0 +1,46 @@
+/**
+ * Copyright (c) 2011-2014, hubin (jobob@qq.com).
+ * <p>
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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;
+
+/**
+ * <p>
+ * SQL 比较条件枚举类
+ * </p>
+ *
+ * @author hubin
+ * @Date 2018-01-05
+ */
+public class SqlCondition {
+
+    /**
+     * 等值
+     */
+    public static final String EQUALS = "%s=#{%s}";
+    /**
+     * % 两边 %
+     */
+    public static final String LIKE = "%s LIKE CONCAT('%',#{%s},'%')";
+    /**
+     * % 左
+     */
+    public static final String LIKE_LEFT = "%s LIKE CONCAT('%',#{%s}";
+    /**
+     * 右 %
+     */
+    public static final String LIKE_RIGHT = "%s LIKE CONCAT(#{%s},'%')";
+
+
+}