Browse Source

优化代码生成器

zhouchenglin 7 years ago
parent
commit
6b2b97ff32

+ 23 - 0
src/main/java/net/chenlin/dp/modules/generator/entity/TableEntity.java

@@ -52,6 +52,29 @@ public class TableEntity implements Serializable {
 	 */
 	private Timestamp createTime;
 
+	/**
+	 * 是否含有decimal类型数据
+	 */
+	private Boolean hasDecimal;
+
+	public TableEntity buildHasDecimal() {
+		for (ColumnEntity columnEntity : columns) {
+			if ("decimal".equals(columnEntity.getDataType().toLowerCase())) {
+				this.hasDecimal = true;
+				return this;
+			}
+		}
+		return this;
+	}
+
+	public Boolean getHasDecimal() {
+		return hasDecimal;
+	}
+
+	public void setHasDecimal(Boolean hasDecimal) {
+		this.hasDecimal = hasDecimal;
+	}
+
 	public TableEntity() {
 		super();
 	}

+ 1 - 2
src/main/java/net/chenlin/dp/modules/generator/utils/GenUtils.java

@@ -117,12 +117,11 @@ public class GenUtils {
 		map.put("viewPath", params.getViewPath());
 		map.put("authKey", urlToAuthKey(params.getRequestMapping()));
 		map.put("columns", table.getColumns());
+		map.put("hasDecimal", table.buildHasDecimal().getHasDecimal());
 		map.put("package", PropertiesUtils.getInstance("velocity/generator").get("package"));
 		map.put("module", params.getModule());
 		map.put("author", PropertiesUtils.getInstance("velocity/generator").get("author"));
 		map.put("email", PropertiesUtils.getInstance("velocity/generator").get("email"));
-		map.put("url", PropertiesUtils.getInstance("velocity/generator").get("url"));
-		map.put("datetime", DateUtils.format(new Date(), DateUtils.DATE_TIME_CHN_PATTERN));
 		VelocityContext context = new VelocityContext(map);
 
 		// 获取模板列表

+ 1 - 2
src/main/java/net/chenlin/dp/modules/generator/utils/JdbcGenUtils.java

@@ -123,12 +123,11 @@ public class JdbcGenUtils {
             map.put("viewPath", webModule + "/" + tableEntity.getClassName().toLowerCase());
             map.put("authKey", GenUtils.urlToAuthKey(tableEntity.getTableName().replace("_","/")));
             map.put("columns", tableEntity.getColumns());
+            map.put("hasDecimal", tableEntity.buildHasDecimal().getHasDecimal());
             map.put("package", PropertiesUtils.getInstance("velocity/generator").get("package"));
             map.put("module", javaModule);
             map.put("author", PropertiesUtils.getInstance("velocity/generator").get("author"));
             map.put("email", PropertiesUtils.getInstance("velocity/generator").get("email"));
-            map.put("url", PropertiesUtils.getInstance("velocity/generator").get("url"));
-            map.put("datetime", DateUtils.format(new Date(), DateUtils.DATE_PATTERN_SLASH));
             VelocityContext context = new VelocityContext(map);
 
             System.out.println("============ start table: " + tableEntity.getTableName() + " ================");

+ 0 - 2
src/main/resources/velocity/generator.properties

@@ -7,8 +7,6 @@ package=net.chenlin.dp
 author=ZhouChenglin
 #email
 email=yczclcn@163.com
-#url
-url=www.chenlintech.com
 
 #table prefix tag
 tablePrefix=tb_

+ 1 - 5
src/main/resources/velocity/template/Controller.java.vm

@@ -16,11 +16,7 @@ import ${package}.modules.${module}.service.${className}Service;
 
 /**
  * ${comments}
- *
- * @author ${author}
- * @email ${email}
- * @url ${url}
- * @date ${datetime}
+ * @author ${author}<${email}>
  */
 @RestController
 @RequestMapping("/${requestMapping}")

+ 10 - 7
src/main/resources/velocity/template/Entity.java.vm

@@ -3,18 +3,14 @@ package ${package}.modules.${module}.entity;
 import java.io.Serializable;
 import java.util.Date;
 
-#if(${hasBigDecimal})
+#if(${hasDecimal})
 import java.math.BigDecimal;
 #end
 
 
 /**
  * ${comments}
- *
- * @author ${author}
- * @email ${email}
- * @url ${url}
- * @date ${datetime}
+ * @author ${author}<${email}>
  */
 public class ${className}Entity implements Serializable {
 	
@@ -33,10 +29,17 @@ public class ${className}Entity implements Serializable {
 	}
 
 #foreach ($column in $columns)
+    /**
+     * setter for $column.fieldName
+     * @param $column.fieldName
+     */
 	public void set${column.methodName}($column.fieldType $column.fieldName) {
 		this.$column.fieldName = $column.fieldName;
 	}
-	
+
+    /**
+     * getter for $column.fieldName
+     */
 	public $column.fieldType get${column.methodName}() {
 		return $column.fieldName;
 	}

+ 31 - 9
src/main/resources/velocity/template/Manager.java.vm

@@ -8,22 +8,44 @@ import ${package}.modules.${module}.entity.${className}Entity;
 
 /**
  * ${comments}
- *
- * @author ${author}
- * @email ${email}
- * @url ${url}
- * @date ${datetime}
+ * @author ${author}<${email}>
  */
 public interface ${className}Manager {
 
+    /**
+     * 分页查询
+     * @param page
+     * @param search
+     * @return
+     */
 	List<${className}Entity> list${className}(Page<${className}Entity> page, Query search);
-	
+
+    /**
+     * 新增
+     * @param ${objName}
+     * @return
+     */
 	int save${className}(${className}Entity ${objName});
-	
+
+    /**
+     * 根据id查询
+     * @param id
+     * @return
+     */
 	${className}Entity get${className}ById(Long id);
-	
+
+    /**
+     * 修改
+     * @param ${objName}
+     * @return
+     */
 	int update${className}(${className}Entity ${objName});
-	
+
+    /**
+     * 删除
+     * @param id
+     * @return
+     */
 	int batchRemove(Long[] id);
 	
 }

+ 27 - 6
src/main/resources/velocity/template/ManagerImpl.java.vm

@@ -13,40 +13,61 @@ import ${package}.modules.${module}.manager.${className}Manager;
 
 /**
  * ${comments}
- *
- * @author ${author}
- * @email ${email}
- * @url ${url}
- * @date ${datetime}
+ * @author ${author}<${email}>
  */
 @Component("${objName}Manager")
 public class ${className}ManagerImpl implements ${className}Manager {
 
 	@Autowired
 	private ${className}Mapper ${objName}Mapper;
-	
 
+    /**
+     * 分页查询
+     * @param page
+     * @param search
+     * @return
+     */
 	@Override
 	public List<${className}Entity> list${className}(Page<${className}Entity> page, Query search) {
 		return ${objName}Mapper.listForPage(page, search);
 	}
 
+    /**
+     * 新增
+     * @param ${objName}
+     * @return
+     */
 	@Override
 	public int save${className}(${className}Entity ${objName}) {
 		return ${objName}Mapper.save(${objName});
 	}
 
+    /**
+     * 根据id查询
+     * @param id
+     * @return
+     */
 	@Override
 	public ${className}Entity get${className}ById(Long id) {
 		${className}Entity ${objName} = ${objName}Mapper.getObjectById(id);
 		return ${objName};
 	}
 
+    /**
+     * 修改
+     * @param ${objName}
+     * @return
+     */
 	@Override
 	public int update${className}(${className}Entity ${objName}) {
 		return ${objName}Mapper.update(${objName});
 	}
 
+    /**
+     * 删除
+     * @param id
+     * @return
+     */
 	@Override
 	public int batchRemove(Long[] id) {
 		int count = ${objName}Mapper.batchRemove(id);

+ 1 - 5
src/main/resources/velocity/template/Mapper.java.vm

@@ -7,11 +7,7 @@ import net.chenlin.dp.modules.sys.dao.BaseMapper;
 
 /**
  * ${comments}
- *
- * @author ${author}
- * @email ${email}
- * @url ${url}
- * @date ${datetime}
+ * @author ${author}<${email}>
  */
 @Mapper
 public interface ${className}Mapper extends BaseMapper<${className}Entity> {

+ 30 - 9
src/main/resources/velocity/template/Service.java.vm

@@ -8,22 +8,43 @@ import ${package}.modules.${module}.entity.${className}Entity;
 
 /**
  * ${comments}
- *
- * @author ${author}
- * @email ${email}
- * @url ${url}
- * @date ${datetime}
+ * @author ${author}<${email}>
  */
 public interface ${className}Service {
 
+    /**
+     * 分页查询
+     * @param params
+     * @return
+     */
 	Page<${className}Entity> list${className}(Map<String, Object> params);
-	
+
+    /**
+     * 新增
+     * @param ${objName}
+     * @return
+     */
 	R save${className}(${className}Entity ${objName});
-	
+
+    /**
+     * 根据id查询
+     * @param id
+     * @return
+     */
 	R get${className}ById(Long id);
-	
+
+    /**
+     * 修改
+     * @param ${objName}
+     * @return
+     */
 	R update${className}(${className}Entity ${objName});
-	
+
+    /**
+     * 删除
+     * @param id
+     * @return
+     */
 	R batchRemove(Long[] id);
 	
 }

+ 28 - 7
src/main/resources/velocity/template/ServiceImpl.java.vm

@@ -15,11 +15,7 @@ import ${package}.modules.${module}.service.${className}Service;
 
 /**
  * ${comments}
- *
- * @author ${author}
- * @email ${email}
- * @url ${url}
- * @date ${datetime}
+ * @author ${author}<${email}>
  */
 @Service("${objName}Service")
 public class ${className}ServiceImpl implements ${className}Service {
@@ -27,6 +23,11 @@ public class ${className}ServiceImpl implements ${className}Service {
 	@Autowired
 	private ${className}Manager ${objName}Manager;
 
+    /**
+     * 分页查询
+     * @param params
+     * @return
+     */
 	@Override
 	public Page<${className}Entity> list${className}(Map<String, Object> params) {
 		Query query = new Query(params);
@@ -35,24 +36,44 @@ public class ${className}ServiceImpl implements ${className}Service {
 		return page;
 	}
 
+    /**
+     * 新增
+     * @param ${objName}
+     * @return
+     */
 	@Override
-	public R save${className}(${className}Entity role) {
-		int count = ${objName}Manager.save${className}(role);
+	public R save${className}(${className}Entity ${objName}) {
+		int count = ${objName}Manager.save${className}(${objName});
 		return CommonUtils.msg(count);
 	}
 
+    /**
+     * 根据id查询
+     * @param id
+     * @return
+     */
 	@Override
 	public R get${className}ById(Long id) {
 		${className}Entity ${objName} = ${objName}Manager.get${className}ById(id);
 		return CommonUtils.msg(${objName});
 	}
 
+    /**
+     * 修改
+     * @param ${objName}
+     * @return
+     */
 	@Override
 	public R update${className}(${className}Entity ${objName}) {
 		int count = ${objName}Manager.update${className}(${objName});
 		return CommonUtils.msg(count);
 	}
 
+    /**
+     * 删除
+     * @param id
+     * @return
+     */
 	@Override
 	public R batchRemove(Long[] id) {
 		int count = ${objName}Manager.batchRemove(id);