Browse Source

生成器移植OK

jobob 8 years ago
parent
commit
9c488d54b8

+ 7 - 4
mybatis-plus/src/main/java/com/baomidou/mybatisplus/generator/config/DataSourceConfig.java

@@ -19,6 +19,7 @@ import java.sql.Connection;
 import java.sql.DriverManager;
 import java.sql.SQLException;
 
+import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
 import com.baomidou.mybatisplus.generator.config.rules.DbType;
 
 /**
@@ -61,11 +62,17 @@ public class DataSourceConfig {
 				dbType = DbType.MYSQL;
 			} else if (driverName.contains("oracle")) {
 				dbType = DbType.ORACLE;
+			} else {
+				throw new MybatisPlusException("Unknown type of database!");
 			}
 		}
 		return dbType;
 	}
 
+	public void setDbType(DbType dbType) {
+		this.dbType = dbType;
+	}
+
 	/**
 	 * 创建数据库连接对象
 	 *
@@ -116,8 +123,4 @@ public class DataSourceConfig {
 		this.password = password;
 	}
 
-	public void setDbType(DbType dbType) {
-		this.dbType = dbType;
-	}
-
 }

+ 10 - 9
mybatis-plus/src/main/java/com/baomidou/mybatisplus/generator/config/PackageConfig.java

@@ -28,45 +28,46 @@ public class PackageConfig {
 	/**
 	 * 父包名。如果为空,将下面子包名必须写全部, 否则就只需写子包名
 	 */
-	private String parent;
+	private String parent = "com.baomidou";
 
 	/**
 	 * 父包模块名。
 	 */
-	private String moduleName;
+	private String moduleName = null;
 
 	/**
 	 * Entity包名
 	 */
-	private String entity;
+	private String entity = "entity";
 
 	/**
 	 * Service包名
 	 */
-	private String service;
+	private String service = "service";
 
 	/**
 	 * Service Impl包名
 	 */
-	private String serviceImpl;
+	private String serviceImpl = "service.impl";
 	/**
 	 * Mapper包名
 	 */
-	private String mapper;
+	private String mapper = "mapper";
 
 	/**
 	 * Mapper XML包名
 	 */
-	private String xml;
+	private String xml = "mapper.xml";
 
 	/**
 	 * Controller包名
 	 */
-	private String controller;
+	private String controller = "web";
 
 	public String getParent() {
-		if (moduleName != null && !"".equals(moduleName.trim()))
+		if (StringUtils.isNotEmpty(moduleName)) {
 			return parent + "." + moduleName;
+		}
 		return parent;
 	}
 

+ 5 - 5
mybatis-plus/src/main/java/com/baomidou/mybatisplus/generator/config/StrategyConfig.java

@@ -29,7 +29,7 @@ public class StrategyConfig {
 	/**
 	 * 数据库表映射到实体的命名策略
 	 */
-	private NamingStrategy naming;
+	private NamingStrategy naming = NamingStrategy.nochange;
 
 	private NamingStrategy fieldNaming;
 
@@ -41,7 +41,7 @@ public class StrategyConfig {
 	/**
 	 * Entity 中的ID生成类型
 	 */
-	private IdStrategy idGenType;
+	private IdStrategy idGenType = IdStrategy.id_worker;
 
 	/**
 	 * 自定义继承的Entity类全称,带包名
@@ -51,17 +51,17 @@ public class StrategyConfig {
 	/**
 	 * 自定义继承的Mapper类全称,带包名
 	 */
-	private String superMapperClass;
+	private String superMapperClass = ConstVal.SUPERD_MAPPER_CLASS;
 
 	/**
 	 * 自定义继承的Service类全称,带包名
 	 */
-	private String superServiceClass;
+	private String superServiceClass = ConstVal.SUPERD_SERVICE_CLASS;
 
 	/**
 	 * 自定义继承的ServiceImpl类全称,带包名
 	 */
-	private String superServiceImplClass;
+	private String superServiceImplClass = ConstVal.SUPERD_SERVICEIMPL_CLASS;
 
 	/**
 	 * 自定义继承的Controller类全称,带包名

+ 6 - 6
mybatis-plus/src/main/java/com/baomidou/mybatisplus/generator/config/TemplateConfig.java

@@ -25,17 +25,17 @@ package com.baomidou.mybatisplus.generator.config;
  */
 public class TemplateConfig {
 
-	private String entity;
+	private String entity = ConstVal.TEMPLATE_ENTITY;
 
-	private String service;
+	private String service = ConstVal.TEMPLATE_SERVICE;
 
-	private String serviceImpl;
+	private String serviceImpl = ConstVal.TEMPLATE_SERVICEIMPL;
 
-	private String mapper;
+	private String mapper = ConstVal.TEMPLATE_MAPPER;
 
-	private String xml;
+	private String xml = ConstVal.TEMPLATE_XML;
 
-	private String controller;
+	private String controller = ConstVal.TEMPLATE_CONTROLLER;
 
 	public String getEntity() {
 		return entity;

+ 20 - 4
mybatis-plus/src/main/java/com/baomidou/mybatisplus/generator/config/builder/ConfigBuilder.java

@@ -101,10 +101,25 @@ public class ConfigBuilder {
 	 */
 	public ConfigBuilder(PackageConfig packageConfig, DataSourceConfig dataSourceConfig, StrategyConfig strategyConfig,
 			TemplateConfig template, String outputDir) {
-		handlerPackage(outputDir, packageConfig);
+		// 包配置
+		if (null == packageConfig) {
+			handlerPackage(outputDir, new PackageConfig());
+		} else {
+			handlerPackage(outputDir, packageConfig);
+		}
 		handlerDataSource(dataSourceConfig);
-		handlerStrategy(strategyConfig);
-		this.template = template;
+		// 策略配置
+		if (null == strategyConfig) {
+			handlerStrategy(new StrategyConfig());
+		} else {
+			handlerStrategy(strategyConfig);
+		}
+		// 模板配置
+		if (null == template) {
+			this.template = new TemplateConfig();
+		} else {
+			this.template = template;
+		}
 	}
 
 	// ************************ 曝露方法 BEGIN*****************************
@@ -277,7 +292,8 @@ public class ConfigBuilder {
 	 */
 	private List<TableInfo> processTable(List<TableInfo> tableList, NamingStrategy strategy, String tablePrefix) {
 		for (TableInfo tableInfo : tableList) {
-			tableInfo.setEntityName(NamingStrategy.capitalFirst(processName(tableInfo.getName(), strategy, tablePrefix)));
+			tableInfo.setEntityName(
+					NamingStrategy.capitalFirst(processName(tableInfo.getName(), strategy, tablePrefix)));
 			tableInfo.setMapperName(tableInfo.getEntityName() + ConstVal.MAPPER);
 			tableInfo.setXmlName(tableInfo.getMapperName());
 			tableInfo.setServiceName("I" + tableInfo.getEntityName() + ConstVal.SERIVCE);

+ 1 - 0
mybatis-plus/src/main/java/com/baomidou/mybatisplus/generator/config/rules/DbType.java

@@ -34,4 +34,5 @@ public enum DbType {
 	public String getValue() {
 		return value;
 	}
+
 }

+ 2 - 1
mybatis-plus/src/main/java/com/baomidou/mybatisplus/mapper/DBType.java

@@ -84,7 +84,8 @@ public enum DBType {
 	 * @return
 	 */
 	public static DBType getDBType(String dbType) {
-		for (DBType dt : DBType.values()) {
+		DBType[] dts = DBType.values();
+		for (DBType dt : dts) {
 			if (dt.getDb().equals(dbType)) {
 				return dt;
 			}

+ 71 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/generator/MysqlGenerator.java

@@ -0,0 +1,71 @@
+/**
+ * Copyright (c) 2011-2016, hubin (jobob@qq.com).
+ *
+ * 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.test.generator;
+
+import com.baomidou.mybatisplus.generator.MybatisPlusGenerator;
+import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
+import com.baomidou.mybatisplus.generator.config.PackageConfig;
+import com.baomidou.mybatisplus.generator.config.StrategyConfig;
+import com.baomidou.mybatisplus.generator.config.rules.DbType;
+import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
+
+/**
+ * <p>
+ * 代码生成器演示
+ * </p>
+ * @author hubin
+ * @date 2016-12-01
+ */
+public class MysqlGenerator {
+
+	/**
+	 * <p>
+	 * MySQL 生成演示
+	 * </p>
+	 */
+	public static void main(String[] args) {
+		MybatisPlusGenerator mpg = new MybatisPlusGenerator();
+		mpg.setOutputDir("D://");
+		mpg.setFileOverride(true);
+		mpg.setActiveRecord(true);
+		mpg.setEnableCache(false);
+		mpg.setAuthor("Yanghu");
+
+		// 数据源配置
+		DataSourceConfig dsc = new DataSourceConfig();
+		dsc.setDbType(DbType.MYSQL);
+		dsc.setDriverName("com.mysql.jdbc.Driver");
+		dsc.setUsername("root");
+		dsc.setPassword("521");
+		dsc.setUrl("jdbc:mysql://127.0.0.1:3306/mybatis-plus?characterEncoding=utf8");
+		mpg.setDataSource(dsc);
+
+		// 策略配置
+		StrategyConfig strategy = new StrategyConfig();
+		strategy.setTablePrefix("bmd_");
+		strategy.setNaming(NamingStrategy.remove_prefix_and_camel);
+		mpg.setStrategy(strategy);
+
+		// 包配置
+		PackageConfig pc = new PackageConfig();
+		pc.setModuleName("test");
+		mpg.setPackageInfo(pc);
+
+		// 执行生成
+		mpg.execute();
+	}
+
+}