jobob 8 anni fa
parent
commit
8def426fd0

+ 3 - 3
pom.xml

@@ -3,7 +3,7 @@
     <modelVersion>4.0.0</modelVersion>
     <groupId>com.baomidou</groupId>
     <artifactId>mybatis-plus</artifactId>
-    <version>2.0.8</version>
+    <version>2.0.9</version>
     <packaging>jar</packaging>
 
     <name>mybatis-plus</name>
@@ -240,8 +240,8 @@
         </dependency>
         <dependency>
             <groupId>org.projectlombok</groupId>
-            <artifactId>${lombok.version}</artifactId>
-            <version>1.16.16</version>
+            <artifactId>lombok</artifactId>
+            <version>${lombok.version}</version>
             <scope>test</scope>
         </dependency>
         <!-- test end -->

+ 8 - 18
src/main/java/com/baomidou/mybatisplus/entity/GlobalConfiguration.java

@@ -48,8 +48,8 @@ public class GlobalConfiguration implements Serializable {
     private String logicDeleteValue = null;
     // 逻辑未删除全局值
     private String logicNotDeleteValue = null;
-    // 数据库类型(默认 MySql)
-    private DBType dbType = DBType.MYSQL;
+    // 数据库类型
+    private DBType dbType;
     // 主键类型(默认 ID_WORKER)
     private IdType idType = IdType.ID_WORKER;
     // 表名、字段名、是否使用下划线命名(默认 false)
@@ -64,8 +64,6 @@ public class GlobalConfiguration implements Serializable {
     private FieldStrategy fieldStrategy = FieldStrategy.NOT_NULL;
     // 是否刷新mapper
     private boolean isRefresh = false;
-    // 是否自动获取DBType
-    private boolean isAutoSetDbType = true;
     // 是否大写命名
     private boolean isCapitalMode = false;
     // 标识符
@@ -115,12 +113,12 @@ public class GlobalConfiguration implements Serializable {
         return dbType;
     }
 
-    public void setDbType(String dbType) {
-        this.dbType = DBType.getDBType(dbType);
-        this.isAutoSetDbType = false;
-    }
-
-    public void setDbTypeByJdbcUrl(String jdbcUrl) {
+    /**
+     * 根据jdbcUrl设置数据库类型
+     *
+     * @param jdbcUrl
+     */
+    public void setDbType(String jdbcUrl) {
         this.dbType = JdbcUtils.getDbType(jdbcUrl);
     }
 
@@ -172,14 +170,6 @@ public class GlobalConfiguration implements Serializable {
         this.isRefresh = refresh;
     }
 
-    public boolean isAutoSetDbType() {
-        return isAutoSetDbType;
-    }
-
-    public void setAutoSetDbType(boolean autoSetDbType) {
-        this.isAutoSetDbType = autoSetDbType;
-    }
-
     public Set<String> getMapperRegistryCache() {
         return mapperRegistryCache;
     }

+ 3 - 1
src/main/java/com/baomidou/mybatisplus/enums/DBType.java

@@ -15,6 +15,8 @@
  */
 package com.baomidou.mybatisplus.enums;
 
+import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
+
 /**
  * <p>
  * MybatisPlus 数据库类型
@@ -92,7 +94,7 @@ public enum DBType {
                 return dt;
             }
         }
-        return MYSQL;
+        throw new MybatisPlusException("Error: Unknown database type, or do not support changing database!\n");
     }
 
     public String getDb() {

+ 12 - 0
src/main/java/com/baomidou/mybatisplus/generator/config/po/TableInfo.java

@@ -200,6 +200,18 @@ public class TableInfo {
         importPackages.add(pkg);
     }
 
+    /**
+     * 逻辑删除
+     */
+    public boolean isLogicDelete(String logicDeletePropertyName) {
+        for (TableField tableField : fields) {
+            if (tableField.getPropertyName().equals(logicDeletePropertyName)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
     /**
      * 转换filed实体为xmlmapper中的basecolumn字符串信息
      *

+ 4 - 1
src/main/java/com/baomidou/mybatisplus/plugins/pagination/dialects/SQLServer2005Dialect.java

@@ -65,7 +65,10 @@ public class SQLServer2005Dialect implements IDialect {
         StringBuilder sql = new StringBuilder();
         sql.append("WITH query AS (SELECT ").append(distinctStr).append("TOP 100 PERCENT ")
                 .append(" ROW_NUMBER() OVER (").append(orderby).append(") as __row_number__, ").append(pagingBuilder)
-                .append(") SELECT * FROM query WHERE __row_number__ BETWEEN ").append(offset).append(" AND ")
+                .append(") SELECT * FROM query WHERE __row_number__ BETWEEN ")
+                //FIX#299:原因:mysql中limit 10(offset,size) 是从第10开始(不包含10),;而这里用的BETWEEN是两边都包含,所以改为offset+1
+                .append(offset + 1)
+                .append(" AND ")
                 .append(offset + limit).append(" ORDER BY __row_number__");
         return sql.toString();
     }

+ 17 - 15
src/main/java/com/baomidou/mybatisplus/spring/MybatisMapperRefresh.java

@@ -151,24 +151,26 @@ public class MybatisMapperRefresh implements Runnable {
                 public void run() {
                     if (fileSet == null) {
                         fileSet = new HashSet<>();
-                        for (Resource mapperLocation : mapperLocations) {
-                            try {
-                                if (ResourceUtils.isJarURL(mapperLocation.getURL())) {
-                                    String key = new UrlResource(ResourceUtils.extractJarFileURL(mapperLocation.getURL()))
-                                            .getFile().getPath();
-                                    fileSet.add(key);
-                                    if (jarMapper.get(key) != null) {
-                                        jarMapper.get(key).add(mapperLocation);
+                        if (mapperLocations != null) {
+                            for (Resource mapperLocation : mapperLocations) {
+                                try {
+                                    if (ResourceUtils.isJarURL(mapperLocation.getURL())) {
+                                        String key = new UrlResource(ResourceUtils.extractJarFileURL(mapperLocation.getURL()))
+                                                .getFile().getPath();
+                                        fileSet.add(key);
+                                        if (jarMapper.get(key) != null) {
+                                            jarMapper.get(key).add(mapperLocation);
+                                        } else {
+                                            List<Resource> resourcesList = new ArrayList<>();
+                                            resourcesList.add(mapperLocation);
+                                            jarMapper.put(key, resourcesList);
+                                        }
                                     } else {
-                                        List<Resource> resourcesList = new ArrayList<>();
-                                        resourcesList.add(mapperLocation);
-                                        jarMapper.put(key, resourcesList);
+                                        fileSet.add(mapperLocation.getFile().getPath());
                                     }
-                                } else {
-                                    fileSet.add(mapperLocation.getFile().getPath());
+                                } catch (IOException ioException) {
+                                    ioException.printStackTrace();
                                 }
-                            } catch (IOException ioException) {
-                                ioException.printStackTrace();
                             }
                         }
                     }

+ 4 - 10
src/main/java/com/baomidou/mybatisplus/toolkit/GlobalConfigUtils.java

@@ -189,20 +189,14 @@ public class GlobalConfigUtils {
      * @param globalConfig 全局配置
      */
     public static void setMetaData(DataSource dataSource, GlobalConfiguration globalConfig) {
-        Connection connection = null;
-        try {
-            connection = dataSource.getConnection();
+        try (Connection connection = dataSource.getConnection()) {
             String jdbcUrl = connection.getMetaData().getURL();
             // 设置全局关键字
             globalConfig.setSqlKeywords(connection.getMetaData().getSQLKeywords());
             // 自动设置数据库类型
-            if (globalConfig.isAutoSetDbType()) {
-                globalConfig.setDbTypeByJdbcUrl(jdbcUrl);
-            }
-        } catch (SQLException e) {
-            logger.warn("Warn: GlobalConfiguration setMetaData Fail !  Cause:" + e);
-        } finally {
-            IOUtils.closeQuietly(connection);
+            globalConfig.setDbType(jdbcUrl);
+        } catch (Exception e) {
+            throw new MybatisPlusException("Error: GlobalConfigUtils setMetaData Fail !  Cause:" + e);
         }
     }
 

+ 3 - 0
src/main/resources/templates/entity.java.vm

@@ -50,6 +50,9 @@ public class ${entity} implements Serializable {
 #end
 #elseif(${field.convert})
 	@TableField("${field.name}")
+#end
+#if(${logicDeletePropertyName}==${field.propertyName})
+    @TableLogic
 #end
 	private ${field.propertyType} ${field.propertyName};
 #end

+ 1 - 1
src/test/java/com/baomidou/mybatisplus/test/GlobalConfigurationTest.java

@@ -50,7 +50,7 @@ public class GlobalConfigurationTest {
     @SuppressWarnings("unchecked")
     public static void main(String[] args) {
         GlobalConfiguration global = GlobalConfigUtils.defaults();
-        global.setAutoSetDbType(true);
+        // global.setAutoSetDbType(true);
         // 设置全局校验机制为FieldStrategy.Empty
         global.setFieldStrategy(2);
         BasicDataSource dataSource = new BasicDataSource();