= пре 8 година
родитељ
комит
e70c45ddbf

+ 1 - 1
mybatis-plus/pom.xml

@@ -3,7 +3,7 @@
 	<modelVersion>4.0.0</modelVersion>
 	<groupId>com.baomidou</groupId>
 	<artifactId>mybatis-plus</artifactId>
-	<version>1.4.8</version>
+	<version>1.4.9</version>
 	<packaging>jar</packaging>
 
 	<name>mybatis-plus</name>

+ 68 - 3
mybatis-plus/src/main/java/com/baomidou/mybatisplus/generator/AutoGenerator.java

@@ -258,7 +258,7 @@ public class AutoGenerator {
 					buildMapper(beanName, mapperName);
 				}
 				if (valideFile(PATH_XML, mapperXMLName, XML_SUFFIX)) {
-					buildMapperXml(columns, types, comments, idMap, mapperName, mapperXMLName);
+					buildMapperXml(beanName, columns, types, comments, idMap, mapperName, mapperXMLName);
 				}
 				if (valideFile(PATH_SERVICE, serviceName, JAVA_SUFFIX)) {
 					buildService(beanName, serviceName);
@@ -757,12 +757,13 @@ public class AutoGenerator {
 	/**
 	 * 构建实体类映射XML文件
 	 *
+	 * @param beanName
 	 * @param columns
 	 * @param types
 	 * @param comments
 	 * @throws IOException
 	 */
-	protected void buildMapperXml(List<String> columns, List<String> types, List<String> comments,
+	protected void buildMapperXml(String beanName, List<String> columns, List<String> types, List<String> comments,
 			Map<String, IdInfo> idMap, String mapperName,String mapperXMLName) throws IOException {
 		File mapperXmlFile = new File(PATH_XML, mapperXMLName + ".xml");
 		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(mapperXmlFile)));
@@ -778,7 +779,11 @@ public class AutoGenerator {
 		/*
 		 * 下面开始写SqlMapper中的方法
 		 */
-		buildSQL(bw, idMap, columns);
+		if (config.isResultMap()) {
+			buildResultMap(bw, beanName, idMap, columns);
+		} else {
+			buildSQL(bw, idMap, columns);
+		}
 
 		bw.write("</mapper>");
 		bw.flush();
@@ -958,6 +963,66 @@ public class AutoGenerator {
 		bw.newLine();
 		bw.newLine();
 	}
+	
+	/**
+	 * 通用 ResultMap 返回参数
+	 *
+	 * @param bw
+	 * @param beanName
+	 * @param idMap
+	 * @param columns
+	 * @throws IOException
+	 */
+	protected void buildResultMap(BufferedWriter bw, String beanName ,Map<String, IdInfo> idMap, List<String> columns) throws IOException {
+		int size = columns.size();
+		bw.write("\t<!-- 通用查询结果列-->");
+		bw.newLine();
+		bw.write("\t<resultMap id=\"" + beanName + "ResultMap\" type=\"" + beanName + "\">");
+		bw.newLine();
+		
+		/*
+		 * 数据库类型
+		 */
+		DBType dbType = DBType.ORACLE;
+		if (config.getConfigDataSource() == ConfigDataSource.MYSQL) {
+			dbType = DBType.MYSQL;
+		}
+		
+		/*
+		 * 公共字段
+		 */
+		if (null != config.getConfigBaseEntity()) {
+			for (String column : config.getConfigBaseEntity().getColumns()) {
+				bw.write("\t\t<result column=\"" + SqlReservedWords.convert(dbType, column) + "\" property=\""
+						+ processField(column) + "\" />");
+				bw.newLine();
+			}
+		}
+		/**
+		 * 个性字段
+		 */
+		for (int i = 0; i < size; i++) {
+			String column = columns.get(i);
+			IdInfo idInfo = idMap.get(column);
+			if (idInfo != null) {
+				bw.write("\t\t<id column=\"" + SqlReservedWords.convert(dbType, column) + "\" property=\""
+						+ processField(idInfo.getValue()) + "\" />");
+			} else {
+				if (null == config.getConfigBaseEntity()) {
+					bw.write(" ");
+				}
+				bw.write("\t\t<result column=\"" + SqlReservedWords.convert(dbType, column) + "\" property=\""
+						+ processField(column) + "\" />");
+			}
+			if (i != size - 1) {
+				bw.newLine();
+			}
+		}
+		bw.newLine();
+		bw.write("\t</resultMap>");
+		bw.newLine();
+		bw.newLine();
+	}
 
 	/**
 	 * 获取所有的数据库表注释

+ 30 - 40
mybatis-plus/src/main/java/com/baomidou/mybatisplus/generator/ConfigGenerator.java

@@ -18,36 +18,19 @@ package com.baomidou.mybatisplus.generator;
 import com.baomidou.mybatisplus.annotations.IdType;
 import com.baomidou.mybatisplus.toolkit.StringUtils;
 
-
 /**
  * <p>
- * 生成器配置类
- * ********************************* 使用前必读 *********************
- * saveDir 文件生成目录
- * entity_package 		entity 包路径
- * mapper_package 		mapper 包路径
- * xmlPackage 			xx_mapper.xml 包路径,默认为mapper/xml
- * servicePackage 		service 包路径
- * controllerPackage 	controller 包路径
- * serviceImplPackage 	serviceImpl包路径,默认为service/impl
- * superService 		service 父类包路径名称
- * superServiceImpl 	service 实现父类包路径名称
- * mapperName			自定义 mapper 名称
- * mapperXMLName		自定义 xml 名称
- * serviceName			自定义 service 名称
- * serviceImplName		自定义 serviceImp 名称
- * tableNames   		要生成的表名称,如为空就直接指定所有表.格式为逗号分割
- * columnConstant  	   【实体】是否生成字段常量(默认 false)
- * buliderModel	   	   【实体】是否为构建者模型(默认 false)
- * fileOverride 		是否覆盖当前已有文件
- * -------------------------------------
- * 以下数据库相关配置:
- * -------------------------------------
- * db_include_prefix 表是否包含前缀,例如: tb_xxx 其中 tb_ 为前缀
- * db_driverName 驱动
- * db_user 用户名
- * db_password 密码
- * db_url 连接地址
+ * 生成器配置类 ********************************* 使用前必读 ********************* saveDir
+ * 文件生成目录 entity_package entity 包路径 mapper_package mapper 包路径 xmlPackage
+ * xx_mapper.xml 包路径,默认为mapper/xml servicePackage service 包路径 controllerPackage
+ * controller 包路径 serviceImplPackage serviceImpl包路径,默认为service/impl superService
+ * service 父类包路径名称 superServiceImpl service 实现父类包路径名称 mapperName 自定义 mapper 名称
+ * mapperXMLName 自定义 xml 名称 serviceName 自定义 service 名称 serviceImplName 自定义
+ * serviceImp 名称 tableNames 要生成的表名称,如为空就直接指定所有表.格式为逗号分割 columnConstant
+ * 【实体】是否生成字段常量(默认 false) buliderModel 【实体】是否为构建者模型(默认 false) fileOverride
+ * 是否覆盖当前已有文件 ------------------------------------- 以下数据库相关配置:
+ * ------------------------------------- db_include_prefix 表是否包含前缀,例如: tb_xxx 其中
+ * tb_ 为前缀 db_driverName 驱动 db_user 用户名 db_password 密码 db_url 连接地址
  **************************************************************
  * </p>
  * 
@@ -91,29 +74,29 @@ public class ConfigGenerator {
 	 * 指定生成表名
 	 */
 	protected String[] tableNames = null;
-	
+
 	/*
-	 * 【实体】是否生成字段常量(默认 false)<br>
-	 * -----------------------------------<br>
-	 * public static final String ID = "test_id";
+	 * 【实体】是否生成字段常量(默认 false)<br> -----------------------------------<br> public
+	 * static final String ID = "test_id";
 	 */
 	protected boolean columnConstant = false;
 
 	/*
-	 * 【实体】是否为构建者模型(默认 false)<br>
-	 * -----------------------------------<br>
-	 * 	public User setName(String name) {
-	 * 		this.name = name;
-	 * 		return this;
-	 * 	}
+	 * 【实体】是否为构建者模型(默认 false)<br> -----------------------------------<br> public
+	 * User setName(String name) { this.name = name; return this; }
 	 */
 	protected boolean buliderModel = false;
-	
+
 	/*
 	 * 是否覆盖当前路径下已有文件(默认 true)
 	 */
 	protected boolean fileOverride = true;
 
+	/*
+	 * true 生成 resultMap , false 生成通用 Base_Column_List
+	 */
+	protected boolean resultMap = false;
+
 	/* db_config */
 	protected boolean dbPrefix = false;
 
@@ -138,7 +121,6 @@ public class ConfigGenerator {
 
 	protected ConfigBaseEntity configBaseEntity = null;
 
-
 	public String getSaveDir() {
 		return saveDir;
 	}
@@ -368,6 +350,14 @@ public class ConfigGenerator {
 		this.fileOverride = fileOverride;
 	}
 
+	public boolean isResultMap() {
+		return resultMap;
+	}
+
+	public void setResultMap(boolean resultMap) {
+		this.resultMap = resultMap;
+	}
+
 	public String getServiceImplPackage() {
 		if (StringUtils.isEmpty(serviceImplPackage)) {
 			serviceImplPackage = servicePackage + ".impl";

+ 5 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/ConfigGeneratorTest.java

@@ -76,6 +76,11 @@ public class ConfigGeneratorTest {
 		 */
 		cg.setFileOverride(true);
 
+		/*
+		 * true 生成 resultMap , false 生成通用 Base_Column_List
+		 */
+		cg.setResultMap(false);
+
 		/*
 		 * 自定义类名,需要包含 %s 格式化会填充实体beanName
 		 */