瀏覽代碼

优化代码生成器

青苗 9 年之前
父節點
當前提交
53622fd041

+ 33 - 36
mybatis-plus/src/main/java/com/baomidou/mybatisplus/generator/AutoGenerator.java

@@ -361,25 +361,26 @@ public class AutoGenerator {
 	 * @return
 	 */
 	protected String mysqlProcessType(String type) {
-		if (type.contains("char")) {
+		String t = type.toLowerCase();
+		if (t.contains("char")) {
 			return "String";
-		} else if (type.contains("bigint")) {
+		} else if (t.contains("bigint")) {
 			return "Long";
-		} else if (type.contains("int")) {
+		} else if (t.contains("int")) {
 			return "Integer";
-		} else if (type.contains("date") || type.contains("timestamp")) {
+		} else if (t.contains("date") || t.contains("timestamp")) {
 			return "Date";
-		} else if (type.contains("text")) {
+		} else if (t.contains("text")) {
 			return "String";
-		} else if (type.contains("bit")) {
+		} else if (t.contains("bit")) {
 			return "Boolean";
-		} else if (type.contains("decimal")) {
+		} else if (t.contains("decimal")) {
 			return "BigDecimal";
-		} else if (type.contains("blob")) {
+		} else if (t.contains("blob")) {
 			return "byte[]";
-		} else if (type.contains("float")) {
+		} else if (t.contains("float")) {
 			return "Float";
-		} else if (type.contains("double")) {
+		} else if (t.contains("double")) {
 			return "Double";
 		}
 		return null;
@@ -393,17 +394,18 @@ public class AutoGenerator {
 	 * @return
 	 */
 	protected String oracleProcessType(String type) {
-		if (type.contains("CHAR")) {
+		String t = type.toUpperCase();
+		if (t.contains("CHAR")) {
 			return "String";
-		} else if (type.contains("DATE") || type.contains("TIMESTAMP")) {
+		} else if (t.contains("DATE") || t.contains("TIMESTAMP")) {
 			return "Date";
-		} else if (type.contains("NUMBER")) {
+		} else if (t.contains("NUMBER")) {
 			return "Double";
-		} else if (type.contains("FLOAT")) {
+		} else if (t.contains("FLOAT")) {
 			return "Float";
-		} else if (type.contains("BLOB")) {
+		} else if (t.contains("BLOB")) {
 			return "Object";
-		} else if (type.contains("RAW")) {
+		} else if (t.contains("RAW")) {
 			return "byte[]";
 		}
 		return null;
@@ -418,7 +420,8 @@ public class AutoGenerator {
 	 */
 	protected boolean isDate(List<String> types) {
 		for (String type : types) {
-			if (type.contains("date") || type.contains("timestamp")) {
+			String t = type.toLowerCase();
+			if (t.contains("date") || t.contains("timestamp")) {
 				return true;
 			}
 		}
@@ -434,25 +437,25 @@ public class AutoGenerator {
 	 */
 	protected boolean isDecimal(List<String> types) {
 		for (String type : types) {
-			if (type.contains("decimal")) {
+			if (type.toLowerCase().contains("decimal")) {
 				return true;
 			}
 		}
 		return false;
 	}
 
+	/**
+	 * 字段处理
+	 * 
+	 * @param field
+	 *            表字段
+	 * @return
+	 */
 	protected String processField(String field) {
-		/*
-		 * 驼峰命名直接返回
-		 */
-		if (null != field && !field.contains("_")) {
-			return field;
-		}
-
 		/*
 		 * 处理下划线分割命名字段
 		 */
-		StringBuilder sb = new StringBuilder(field.length());
+		StringBuilder sb = new StringBuilder();
 		String[] fields = field.split("_");
 		sb.append(fields[0].toLowerCase());
 		for (int i = 1; i < fields.length; i++) {
@@ -708,20 +711,14 @@ public class AutoGenerator {
 		bw.newLine();
 		bw.write("import " + config.getEntityPackage() + "." + beanName + ";");
 		bw.newLine();
-		if (config.getConfigIdType() == ConfigIdType.STRING) {
-			bw.write("import com.baomidou.framework.service.ICommonService;");
-		} else {
-			bw.write("import com.baomidou.framework.service.ISuperService;");
-		}
+		String superService = config.getSuperService();
+		bw.write("import " + superService);
 		bw.newLine();
 
 		bw = buildClassComment(bw, beanName + " 表数据服务层接口");
 		bw.newLine();
-		if (config.getConfigIdType() == ConfigIdType.STRING) {
-			bw.write("public interface " + serviceName + " extends ICommonService<" + beanName + "> {");
-		} else {
-			bw.write("public interface " + serviceName + " extends ISuperService<" + beanName + "> {");
-		}
+		superService = superService.substring(superService.lastIndexOf(".") + 1);
+		bw.write("public interface " + serviceName + " extends " + superService + "<" + beanName + "> {");
 		bw.newLine();
 		bw.newLine();
 

+ 25 - 3
mybatis-plus/src/main/java/com/baomidou/mybatisplus/generator/ConfigGenerator.java

@@ -17,6 +17,7 @@ package com.baomidou.mybatisplus.generator;
 
 import com.baomidou.mybatisplus.annotations.IdType;
 
+
 /**
  * <p>
  * 生成器配置类
@@ -27,9 +28,13 @@ import com.baomidou.mybatisplus.annotations.IdType;
  * xmlPackage xx_mapper.xml 包路径,默认为mapper/xml
  * servicePackage service 包路径
  * serviceImplPackage serviceImpl包路径,默认为service/impl
- * superServiceImpl service 父类包路径名称
- * tableNames   要生成的表名称,如为空就直接指定所有表.格式为逗号分割
- * fileOverride 是否覆盖当前已有文件
+ * superService 	service 父类包路径名称
+ * superServiceImpl service 实现父类包路径名称
+ * mapperName		自定义 mapper 名称
+ * serviceName		自定义 service 名称
+ * serviceImplName	自定义 serviceImp 名称
+ * tableNames   	要生成的表名称,如为空就直接指定所有表.格式为逗号分割
+ * fileOverride 	是否覆盖当前已有文件
  * -------------------------------------
  * 以下数据库相关配置:
  * -------------------------------------
@@ -58,6 +63,8 @@ public class ConfigGenerator {
 
 	protected String serviceImplPackage;
 
+	protected String superService;
+
 	protected String superServiceImpl;
 
 	/*
@@ -133,6 +140,21 @@ public class ConfigGenerator {
 		this.servicePackage = servicePackage;
 	}
 
+	public String getSuperService() {
+		if (superService == null || "".equals(superService)) {
+			if (this.getConfigIdType() == ConfigIdType.STRING) {
+				return "com.baomidou.framework.service.ICommonService";
+			} else {
+				return "com.baomidou.framework.service.ISuperService";
+			}
+		}
+		return superService;
+	}
+
+	public void setSuperService(String superService) {
+		this.superService = superService;
+	}
+
 	public String getSuperServiceImpl() {
 		if (superServiceImpl == null || "".equals(superServiceImpl)) {
 			if (this.getConfigIdType() == ConfigIdType.STRING) {

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

@@ -12,8 +12,9 @@ public class ConfigGeneratorTest {
 		cg.setXmlPackage("com.baomidou.mapper.xml");// xml层路径(可以不写)
 		cg.setServiceImplPackage("com.baomidou.service.impl");// serviceimpl层路径(可以不写)
 
-		/* 此处可以配置 SuperServiceImpl 子类路径,默认如下 */
-		// cg.setSuperServiceImpl("com.baomidou.framework.service.impl.SuperServiceImpl");
+		/* 此处可以配置,自定义 service 及 serviceImpl 子类路径 */
+		//cg.setSuperService("com.xxx.service.IBaseService");
+		//cg.setSuperServiceImpl("com.xxx.service.impl.BaseServiceImpl");
 
 		/* 此处设置 String 类型数据库ID,默认Long类型 */
 		// cg.setConfigIdType(ConfigIdType.STRING);