瀏覽代碼

优化 yanghu pull request 的代码生成类

青苗 9 年之前
父節點
當前提交
82768a8d33

+ 21 - 19
mybatis-plus/src/main/java/com/baomidou/mybatisplus/generator/AutoGenerator.java

@@ -15,20 +15,32 @@
  */
 package com.baomidou.mybatisplus.generator;
 
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
 import com.baomidou.mybatisplus.annotations.IdType;
 import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
 
-import java.io.*;
-import java.sql.*;
-import java.util.*;
-
 /**
  * <p>
  * 映射文件自动生成类
  * </p>
  *
- * @author hubin
- * @Date 2016-01-23
+ * @author hubin yanghu
+ * @Date 2016-06-12
  */
 public class AutoGenerator {
 
@@ -79,15 +91,6 @@ public class AutoGenerator {
             gf.mkdirs();
         }
 
-        /**
-         * 目录初始化
-         */
-//        PATH_ENTITY = getFilePath(gf.getPath(), "entity");
-//        PATH_MAPPER = getFilePath(gf.getPath(), "mapper");
-//        PATH_XML = getFilePath(gf.getPath(), "xml");
-//        PATH_SERVICE = getFilePath(gf.getPath(), "service");
-//        PATH_SERVICE_IMPL = getFilePath(gf.getPath(), "serviceImpl");
-
         /**
          * 修改设置最终目录的逻辑
          */
@@ -292,13 +295,13 @@ public class AutoGenerator {
             tables.add(results.getString(1));
         }
 
-        String tableNames = config.getTableNames();
-        if (null == tableNames || "".equals(tableNames.trim())) {
+        String[] tableNames = config.getTableNames();
+		if (null == tableNames || tableNames.length == 0) {
             return tables;
         }
 
         // 循环判断是否配置的所有表都在当前库中存在
-        List<String> custTables = Arrays.asList(tableNames.trim().split(","));
+        List<String> custTables = Arrays.asList(tableNames);
         List<String> notExistTables = new ArrayList<String>();
         for (String tb : custTables) {
             if (!tables.contains(tb)) {
@@ -424,7 +427,6 @@ public class AutoGenerator {
      * @return
      */
     private boolean isDecimal(List<String> types) {
-        int size = types.size();
         for (String type : types) {
             if (type.contains("decimal")) {
                 return true;

+ 14 - 9
mybatis-plus/src/main/java/com/baomidou/mybatisplus/generator/ConfigGenerator.java

@@ -60,10 +60,15 @@ public class ConfigGenerator {
 
     private String superServiceImpl;
 
-    private String tableNames;
-
-    private boolean fileOverride = false;
+	/*
+	 * 指定生成表名
+	 */
+	private String[] tableNames = null;
 
+	/*
+	 * 是否覆盖当前路径下已有文件(默认 true)
+	 */
+    private boolean fileOverride = true;
 
     /* db_config */
     private boolean dbPrefix = false;
@@ -245,13 +250,13 @@ public class ConfigGenerator {
         this.xmlPackage = xmlPackage;
     }
 
-    public String getTableNames() {
-        return tableNames;
-    }
+	public String[] getTableNames() {
+		return tableNames;
+	}
 
-    public void setTableNames(String tableNames) {
-        this.tableNames = tableNames;
-    }
+	public void setTableNames( String[] tableNames ) {
+		this.tableNames = tableNames;
+	}
 
     public boolean isFileOverride() {
         return fileOverride;

+ 7 - 10
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/ConfigGeneratorTest.java

@@ -9,16 +9,8 @@ public class ConfigGeneratorTest {
         cg.setEntityPackage("com.baomidou.entity");//entity 实体包路径
         cg.setMapperPackage("com.baomidou.mapper");//mapper 映射文件路径
         cg.setServicePackage("com.baomidou.service");//service 层路径
-        cg.setXmlPackage("com.baomidou.mapper.xml");  // xml层路径(可以不写)
-        cg.setServiceImplPackage("com.baomidou.service.impl");  // serviceimpl层路径(可以不写)
-        cg.setFileOverride(false);  // 默认值为false , 是否覆盖当前路径下已有文件
-
-        /**
-         *  如果配置了就只生成指定表名的文件,如果不配置就全部表
-         *  此处会对表名做校验,如果表名不存在 ,则不会生成任何文件。
-         *  控制台会提示不存在的表名称
-         * */
-        cg.setTableNames("user,roles");
+        cg.setXmlPackage("com.baomidou.mapper.xml");//xml层路径(可以不写)
+        cg.setServiceImplPackage("com.baomidou.service.impl");//serviceimpl层路径(可以不写)
 
 		/* 此处可以配置 SuperServiceImpl 子类路径,默认如下 */
         //cg.setSuperServiceImpl("com.baomidou.framework.service.impl.SuperServiceImpl");
@@ -39,6 +31,11 @@ public class ConfigGeneratorTest {
 		 * </p>
 		 */
         cg.setDbPrefix(false);
+        /*
+         * 默认值为true , 是否覆盖当前路径下已有文件
+         */
+        cg.setFileOverride(true);
         return cg;
     }
+
 }

+ 2 - 3
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/AutoGeneratorUser.java

@@ -35,7 +35,6 @@ public class AutoGeneratorUser extends ConfigGeneratorTest {
 	 * <p>
 	 * 配置方法查看 {@link ConfigGenerator}
 	 * </p>
-	 * 
 	 */
 	public static void main( String[] args ) {
 		ConfigGenerator cg = getConfigGenerator();
@@ -43,8 +42,8 @@ public class AutoGeneratorUser extends ConfigGeneratorTest {
 		/* mysql 数据库相关配置 */
 		cg.setDbDriverName("com.mysql.jdbc.Driver");
 		cg.setDbUser("root");
-		cg.setDbPassword("123456");
-		cg.setDbUrl("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8");
+		cg.setDbPassword("");
+		cg.setDbUrl("jdbc:mysql://127.0.0.1:3306/mybatis-plus?characterEncoding=utf8");
 
 		/*
 		 * 表主键 ID 生成类型, 自增该设置无效。

+ 2 - 2
mybatis-plus/src/test/resources/mysql-config.xml

@@ -72,9 +72,9 @@
 			</transactionManager>
 			<dataSource type="UNPOOLED">
 				<property name="driver" value="com.mysql.jdbc.Driver" />
-				<property name="url" value="jdbc:mysql://localhost:3306/test" />
+				<property name="url" value="jdbc:mysql://localhost:3306/mybatis-plus" />
 				<property name="username" value="root" />
-				<property name="password" value="123456" />
+				<property name="password" value="" />
 			<!-- 
 				<property name="driver" value="${jdbc.driver}" />
 				<property name="url" value="${jdbc.url}" />