浏览代码

实体字符串类型默认为 like 查询

hubin 7 年之前
父节点
当前提交
eae2dabdb0

+ 28 - 16
mybatis-plus-annotation/src/main/java/com/baomidou/mybatisplus/annotation/DbType.java

@@ -15,7 +15,6 @@
  */
 package com.baomidou.mybatisplus.annotation;
 
-
 import lombok.Getter;
 
 /**
@@ -24,64 +23,77 @@ import lombok.Getter;
  * </p>
  *
  * @author hubin
- * @since 2018-06-09
+ * @since 2018-06-23
  */
 @Getter
 public enum DbType {
     /**
      * MYSQL
      */
-    MYSQL("mysql", "`%s`", "MySql数据库"),
+    MYSQL("mysql", "`%s`", "%s LIKE CONCAT('%%',#{%s},'%%')", "MySql数据库"),
     /**
      * MARIADB
      */
-    MARIADB("mariadb", "`%s`", "MariaDB数据库"),
+    MARIADB("mariadb", "`%s`", "%s LIKE CONCAT('%%',#{%s},'%%')", "MariaDB数据库"),
     /**
      * ORACLE
      */
-    ORACLE("oracle", null, "Oracle数据库"),
+    ORACLE("oracle", null, "%s LIKE CONCAT('%%',#{%s},'%%')", "Oracle数据库"),
     /**
      * DB2
      */
-    DB2("db2", null, "DB2数据库"),
+    DB2("db2", null, "%s LIKE CONCAT('%%',#{%s},'%%')", "DB2数据库"),
     /**
      * H2
      */
-    H2("h2", null, "H2数据库"),
+    H2("h2", null, "%s LIKE CONCAT('%%',#{%s},'%%')", "H2数据库"),
     /**
      * HSQL
      */
-    HSQL("hsql", null, "HSQL数据库"),
+    HSQL("hsql", null, "%s LIKE CONCAT('%%',#{%s},'%%')", "HSQL数据库"),
     /**
      * SQLITE
      */
-    SQLITE("sqlite", "`%s`", "SQLite数据库"),
+    SQLITE("sqlite", "`%s`", "%s LIKE CONCAT('%%',#{%s},'%%')", "SQLite数据库"),
     /**
      * POSTGRE
      */
-    POSTGRE_SQL("postgresql", "\"%s\"", "Postgre数据库"),
+    POSTGRE_SQL("postgresql", "\"%s\"", "%s LIKE CONCAT('%%',#{%s},'%%')", "Postgre数据库"),
     /**
      * SQLSERVER2005
      */
-    SQL_SERVER2005("sqlserver2005", null, "SQLServer2005数据库"),
+    SQL_SERVER2005("sqlserver2005", null, "%s LIKE '%%'+#{%s}+'%%'", "SQLServer2005数据库"),
     /**
      * SQLSERVER
      */
-    SQL_SERVER("sqlserver", null, "SQLServer数据库"),
+    SQL_SERVER("sqlserver", null, "%s LIKE '%%'+#{%s}+'%%'", "SQLServer数据库"),
     /**
      * UNKONWN DB
      */
-    OTHER("other", null, "其他数据库");
+    OTHER("other", null, null, "其他数据库");
 
+    /**
+     * 数据库名称
+     */
     private final String db;
-
+    /**
+     * 转移符
+     */
     private final String quote;
-
+    /**
+     * LIKE 拼接模式
+     */
+    private final String like;
+    /**
+     * 描述
+     */
     private final String desc;
 
-    DbType(final String db, final String quote, final String desc) {
+
+    DbType(String db, String quote, String like, String desc) {
         this.db = db;
         this.quote = quote;
+        this.like = like;
         this.desc = desc;
     }
 

+ 0 - 1
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/enums/SqlMethod.java

@@ -64,7 +64,6 @@ public enum SqlMethod {
     SELECT_BY_ID("selectById", "根据ID 查询一条数据", "SELECT %s FROM %s WHERE %s=#{%s}"),
     SELECT_BY_MAP("selectByMap", "根据columnMap 查询一条数据", "<script>SELECT %s FROM %s %s</script>"),
     SELECT_BATCH_BY_IDS("selectBatchIds", "根据ID集合,批量查询数据", "<script>SELECT %s FROM %s WHERE %s IN (%s)</script>"),
-    SELECT_ONE("selectOne", "查询满足条件一条数据", "<script>SELECT %s FROM %s %s</script>"),
     SELECT_COUNT("selectCount", "查询满足条件总记录数", "<script>SELECT COUNT(1) FROM %s %s</script>"),
     SELECT_LIST("selectList", "查询满足条件所有数据", "<script>SELECT %s FROM %s %s</script>"),
     SELECT_PAGE("selectPage", "查询满足条件所有数据(并翻页)", "<script>SELECT %s FROM %s %s</script>"),

+ 2 - 24
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/AbstractMethod.java

@@ -284,28 +284,6 @@ public abstract class AbstractMethod {
         return columns.toString();
     }
 
-    /**
-     * <p>
-     * SQL 查询条件
-     * </p>
-     */
-    protected String sqlWhere(TableInfo table) {
-        StringBuilder where = new StringBuilder("<where>");
-        if (StringUtils.isNotEmpty(table.getKeyProperty())) {
-            where.append("<if test=\"ew.").append(table.getKeyProperty()).append("!=null\">")
-                .append(table.getKeyColumn()).append("=#{ew.").append(table.getKeyProperty()).append("}")
-                .append("</if>");
-        }
-        List<TableFieldInfo> fieldList = table.getFieldList();
-        for (TableFieldInfo fieldInfo : fieldList) {
-            where.append(convertIfTag(fieldInfo, "ew.", false))
-                .append(" AND ").append(sqlCondition(fieldInfo.getCondition(),
-                fieldInfo.getColumn(), "ew." + fieldInfo.getEl()))
-                .append(convertIfTag(fieldInfo, true));
-        }
-        return where.append("</where>").toString();
-    }
-
     /**
      * <p>
      * SQL map 查询条件
@@ -413,8 +391,8 @@ public abstract class AbstractMethod {
         List<TableFieldInfo> fieldList = table.getFieldList();
         for (TableFieldInfo fieldInfo : fieldList) {
             where.append(convertIfTag(fieldInfo, "ew.entity.", false));
-            where.append(" AND ").append(sqlCondition(fieldInfo.getCondition(),
-                fieldInfo.getColumn(), "ew.entity." + fieldInfo.getEl()));
+            where.append(" AND ").append(sqlCondition(fieldInfo.getCondition(), fieldInfo.getColumn(),
+                "ew.entity." + fieldInfo.getEl()));
             where.append(convertIfTag(fieldInfo, true));
         }
         where.append("</if>");

+ 0 - 1
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/DefaultSqlInjector.java

@@ -34,7 +34,6 @@ import com.baomidou.mybatisplus.core.injector.methods.SelectList;
 import com.baomidou.mybatisplus.core.injector.methods.SelectMaps;
 import com.baomidou.mybatisplus.core.injector.methods.SelectMapsPage;
 import com.baomidou.mybatisplus.core.injector.methods.SelectObjs;
-import com.baomidou.mybatisplus.core.injector.methods.SelectOne;
 import com.baomidou.mybatisplus.core.injector.methods.SelectPage;
 import com.baomidou.mybatisplus.core.injector.methods.Update;
 import com.baomidou.mybatisplus.core.injector.methods.UpdateById;

+ 1 - 1
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectObjs.java

@@ -35,7 +35,7 @@ public class SelectObjs extends AbstractMethod {
     @Override
     public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
         SqlMethod sqlMethod = SqlMethod.SELECT_OBJS;
-        String sql = String.format(sqlMethod.getSql(), sqlSelectColumns(tableInfo, true),
+        String sql = String.format(sqlMethod.getSql(), sqlSelectObjsColumns(tableInfo),
             tableInfo.getTableName(), this.sqlWhereEntityWrapper(tableInfo));
         SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
         return this.addSelectMappedStatement(mapperClass, sqlMethod.getMethod(), sqlSource, modelClass, tableInfo);

+ 0 - 43
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectOne.java

@@ -1,43 +0,0 @@
-/*
- * Copyright (c) 2011-2020, 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.core.injector.methods;
-
-import org.apache.ibatis.mapping.MappedStatement;
-import org.apache.ibatis.mapping.SqlSource;
-
-import com.baomidou.mybatisplus.core.enums.SqlMethod;
-import com.baomidou.mybatisplus.core.injector.AbstractMethod;
-import com.baomidou.mybatisplus.core.metadata.TableInfo;
-
-/**
- * <p>
- * 根据 ID 删除
- * </p>
- *
- * @author hubin
- * @since 2018-04-06
- */
-public class SelectOne extends AbstractMethod {
-
-    @Override
-    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
-        SqlMethod sqlMethod = SqlMethod.SELECT_ONE;
-        String sql = String.format(sqlMethod.getSql(), this.sqlSelectColumns(tableInfo, false),
-            tableInfo.getTableName(), this.sqlWhere(tableInfo));
-        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
-        return this.addSelectMappedStatement(mapperClass, sqlMethod.getMethod(), sqlSource, modelClass, tableInfo);
-    }
-}

+ 0 - 9
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/mapper/BaseMapper.java

@@ -176,15 +176,6 @@ public interface BaseMapper<T> {
      */
     List<T> selectByMap(@Param("cm") Map<String, Object> columnMap);
 
-    /**
-     * <p>
-     * 根据 entity 条件,查询一条记录
-     * </p>
-     *
-     * @param entity 实体对象
-     */
-    T selectOne(@Param("ew") T entity);
-
     /**
      * <p>
      * 根据 Wrapper 条件,查询总记录数

+ 9 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/TableFieldInfo.java

@@ -17,6 +17,7 @@ package com.baomidou.mybatisplus.core.metadata;
 
 import java.lang.reflect.Field;
 
+import com.baomidou.mybatisplus.annotation.DbType;
 import com.baomidou.mybatisplus.annotation.FieldFill;
 import com.baomidou.mybatisplus.annotation.FieldStrategy;
 import com.baomidou.mybatisplus.annotation.SqlCondition;
@@ -131,6 +132,7 @@ public class TableFieldInfo {
         tableInfo.setLogicDelete(this.initLogicDelete(dbConfig, field));
         this.update = tableField.update();
         this.condition = tableField.condition();
+        this.setCondition(dbConfig.getDbType());
         /*
          * 保存当前字段的填充策略
          */
@@ -152,6 +154,7 @@ public class TableFieldInfo {
         this.el = field.getName();
         this.fieldStrategy = dbConfig.getFieldStrategy();
         this.propertyType = field.getType();
+        this.setCondition(dbConfig.getDbType());
         tableInfo.setLogicDelete(this.initLogicDelete(dbConfig, field));
     }
 
@@ -274,6 +277,12 @@ public class TableFieldInfo {
         return condition;
     }
 
+    public void setCondition(DbType dbType) {
+        if (null == this.condition && StringUtils.isCharSequence(this.propertyType)) {
+            this.condition = dbType.getLike();
+        }
+    }
+
     public void setCondition(String condition) {
         this.condition = condition;
     }

+ 0 - 28
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/injector/LogicAbstractMethod.java

@@ -86,34 +86,6 @@ public abstract class LogicAbstractMethod extends AbstractMethod {
 
     // ------------ 处理逻辑删除条件过滤 ------------
 
-    @Override
-    protected String sqlWhere(TableInfo table) {
-        if (table.isLogicDelete()) {
-            StringBuilder where = new StringBuilder("<where>");
-            // 过滤逻辑
-            List<TableFieldInfo> fieldList = table.getFieldList();
-            // EW 逻辑
-            if (StringUtils.isNotEmpty(table.getKeyProperty())) {
-                where.append("<if test=\"ew.").append(table.getKeyProperty()).append("!=null\">");
-                where.append(" AND ").append(table.getKeyColumn()).append("=#{ew.");
-                where.append(table.getKeyProperty()).append("}");
-                where.append("</if>");
-            }
-            for (TableFieldInfo fieldInfo : fieldList) {
-                where.append(convertIfTag(fieldInfo, "ew.", false));
-                where.append(" AND ").append(sqlCondition(fieldInfo.getCondition(),
-                    fieldInfo.getColumn(), "ew." + fieldInfo.getEl()));
-                where.append(convertIfTag(fieldInfo, true));
-            }
-            // 过滤逻辑
-            where.append(getLogicDeleteSql(table));
-            where.append("</where>");
-            return where.toString();
-        }
-        // 正常逻辑
-        return super.sqlWhere(table);
-    }
-
     @Override
     protected String sqlWhereEntityWrapper(TableInfo table) {
         if (table.isLogicDelete()) {

+ 0 - 1
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/injector/LogicSqlInjector.java

@@ -37,7 +37,6 @@ import com.baomidou.mybatisplus.extension.injector.methods.LogicSelectList;
 import com.baomidou.mybatisplus.extension.injector.methods.LogicSelectMaps;
 import com.baomidou.mybatisplus.extension.injector.methods.LogicSelectMapsPage;
 import com.baomidou.mybatisplus.extension.injector.methods.LogicSelectObjs;
-import com.baomidou.mybatisplus.extension.injector.methods.LogicSelectOne;
 import com.baomidou.mybatisplus.extension.injector.methods.LogicSelectPage;
 import com.baomidou.mybatisplus.extension.injector.methods.LogicUpdate;
 import com.baomidou.mybatisplus.extension.injector.methods.LogicUpdateById;

+ 1 - 1
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/injector/methods/LogicSelectObjs.java

@@ -35,7 +35,7 @@ public class LogicSelectObjs extends LogicAbstractMethod {
     @Override
     public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
         SqlMethod sqlMethod = SqlMethod.SELECT_OBJS;
-        String sql = String.format(sqlMethod.getSql(), sqlSelectColumns(tableInfo, true),
+        String sql = String.format(sqlMethod.getSql(), sqlSelectObjsColumns(tableInfo),
             tableInfo.getTableName(), sqlWhereEntityWrapper(tableInfo));
         SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
         return addSelectMappedStatement(mapperClass, sqlMethod.getMethod(), sqlSource, modelClass, tableInfo);

+ 0 - 43
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/injector/methods/LogicSelectOne.java

@@ -1,43 +0,0 @@
-/*
- * Copyright (c) 2011-2020, 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.extension.injector.methods;
-
-import org.apache.ibatis.mapping.MappedStatement;
-import org.apache.ibatis.mapping.SqlSource;
-
-import com.baomidou.mybatisplus.core.enums.SqlMethod;
-import com.baomidou.mybatisplus.core.metadata.TableInfo;
-import com.baomidou.mybatisplus.extension.injector.LogicAbstractMethod;
-
-/**
- * <p>
- * 根据 ID 删除
- * </p>
- *
- * @author hubin
- * @since 2018-06-13
- */
-public class LogicSelectOne extends LogicAbstractMethod {
-
-    @Override
-    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
-        SqlMethod sqlMethod = SqlMethod.SELECT_ONE;
-        String sql = String.format(sqlMethod.getSql(), sqlSelectColumns(tableInfo, false),
-            tableInfo.getTableName(), sqlWhere(tableInfo));
-        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
-        return addSelectMappedStatement(mapperClass, sqlMethod.getMethod(), sqlSource, modelClass, tableInfo);
-    }
-}