Java之利⽤Freemarker模板引擎实现代码⽣成器,提⾼效率
开⼼⼀笑
【1.你以为我会眼睁睁的看着你去送死?我会闭着眼睛。2.给你讲个故事,从前有个笨蛋,他⾮常笨,别⼈问他问题他只会回答“没有”,这个故事你听过吗?】
视频教程
⼤家好,我录制的视频《Java之优雅编程之道》已经在CSDN学院发布了,有兴趣的同学可以购买观看,相信⼤家⼀定会收获到很多知识的。谢谢⼤家的⽀持……
视频地址:
提出问题
如何利⽤ Java + Freemarker 实现代码⽣成器
解决问题
* 前⾔ *
做业务开发的时候,经常要根据建⽴好的数据库表,⽣成相关的 Model , DTO , Service, Controller , DAO 等等。包括基本的增删改查。⽽这些细活往往⽐较简单且没有挑战性,纯粹苦⼒活。因此,根据公司的框架,开发⼀个代码⽣成器是很有必要的。
这⾥:
假如你有⼀定的java基础;
假如你熟悉freemarker模板引擎;
假如你熟悉MVC框架;
假如你熟悉Spring Data 框架
* 技术选型 *
由于代码⽣成器是要⽣成很多⽂件的,包 Test.java , TestDTO.java ,TestController.java , TestServiceImpl.java , ITestService.java , TestDAO.java 等等这些⽂件。所有考虑⽤ freemarker 强⼤的模板引擎,制作相关的模板。
* 实现思路 *
⾸先,假如在数据库中有⼀张表 ay_test.
CREATE TABLE "public"."ay_test" (
"id" varchar(32) COLLATE "default" NOT NULL,
"name" varchar(10) COLLATE "default",
"birth_date" timestamp(6),
"remark" text COLLATE "default",
CONSTRAINT "ay_test_pkey" PRIMARY KEY ("id")
)
我们⾸先要获取数据库的连接,这⾥我只贴出相关的代码。
private final String URL = "jdbc:postgresql://192.168.3.160:10655/cibpm";
private final String USER = "postgres";
private final String PASSWORD = "888888";
private final String DRIVER = "org.postgresql.Driver";
public Connection getConnection() throws Exception{
Class.forName(DRIVER);
Connection connection= Connection(URL, USER, PASSWORD); return connection;
}
获取数据库表的元数据
private final String changeTableName = replaceUnderLineAndUpperCase(tableName);
Connection connection = getConnection();
DatabaseMetaData databaseMetaData = MetaData();
ResultSet resultSet = Columns(null,"%", tableName,"%");
最后根据元数据获取表字段,注释等等,⽣成相关的⽂件
代码实现
Java代码实现
package com.evada.de.generate.util;
import com.evada.del.ColumnClass;
plate.Template;
import org.apachemons.lang3.StringUtils;
import java.io.*;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 描述:代码⽣成器
* Created by Ay on 2017/5/1.
*/
public class CodeGenerateUtils {
private final String AUTHOR = "Ay";
private final String CURRENT_DATE = "2017/05/03";
private final String tableName = "tm_project_quality_problem";
private final String packageName = "com.evada.pm.process.manage";
private final String tableAnnotation = "质量问题";
private final String URL = "jdbc:postgresql://192.168.3.160:10655/cibpm";
private final String USER = "postgres";
private final String PASSWORD = "888888";
private final String DRIVER = "org.postgresql.Driver";
private final String DRIVER = "org.postgresql.Driver";
private final String diskPath = "D://";
private final String changeTableName = replaceUnderLineAndUpperCase(tableName);
public Connection getConnection() throws Exception{
Class.forName(DRIVER);
Connection connection= Connection(URL, USER, PASSWORD);
return connection;
}
public static void main(String[] args) throws Exception{
CodeGenerateUtils codeGenerateUtils = new CodeGenerateUtils();
}
public void generate() throws Exception{
try {
Connection connection = getConnection();
DatabaseMetaData databaseMetaData = MetaData();
ResultSet resultSet = Columns(null,"%", tableName,"%");
//⽣成Mapper⽂件
generateMapperFile(resultSet);免费模板生成器
//⽣成Dao⽂件
generateDaoFile(resultSet);
//⽣成Repository⽂件
generateRepositoryFile(resultSet);
//⽣成服务层接⼝⽂件
generateServiceInterfaceFile(resultSet);
//⽣成服务实现层⽂件
generateServiceImplFile(resultSet);
/
/⽣成Controller层⽂件
generateControllerFile(resultSet);
//⽣成DTO⽂件
generateDTOFile(resultSet);
//⽣成Model⽂件
generateModelFile(resultSet);
} catch (Exception e) {
throw new RuntimeException(e);
}finally{
}
}
private void generateModelFile(ResultSet resultSet) throws Exception{
final String suffix = ".java";
final String path = diskPath + changeTableName + suffix;
final String templateName = "Model.ftl";
File mapperFile = new File(path);
List<ColumnClass> columnClassList = new ArrayList<>();
ColumnClass columnClass = null;
()){
//id字段略过
String("COLUMN_NAME").equals("id")) continue;
columnClass = new ColumnClass();
/
/获取字段名称
columnClass.String("COLUMN_NAME"));
//获取字段类型
columnClass.String("TYPE_NAME"));
//转换字段名称,如 sys_name 变成 SysName
columnClass.setChangeColumnName(String("COLUMN_NAME"))); //字段在数据库的注释
columnClass.String("REMARKS"));
columnClassList.add(columnClass);
}
Map<String,Object> dataMap = new HashMap<>();
Map<String,Object> dataMap = new HashMap<>();
dataMap.put("model_column",columnClassList);
generateFileByTemplate(templateName,mapperFile,dataMap);
}
private void generateDTOFile(ResultSet resultSet) throws Exception{
final String suffix = "DTO.java";
final String path = "D://" + changeTableName + suffix;
final String templateName = "DTO.ftl";
File mapperFile = new File(path);
Map<String,Object> dataMap = new HashMap<>();
generateFileByTemplate(templateName,mapperFile,dataMap);
}
private void generateControllerFile(ResultSet resultSet) throws Exception{
final String suffix = "Controller.java";
final String path = diskPath + changeTableName + suffix;
final String templateName = "Controller.ftl";
File mapperFile = new File(path);
Map<String,Object> dataMap = new HashMap<>();
generateFileByTemplate(templateName,mapperFile,dataMap);
}
private void generateServiceImplFile(ResultSet resultSet) throws Exception{
final String suffix = "ServiceImpl.java";
final String path = diskPath + changeTableName + suffix;
final String templateName = "ServiceImpl.ftl";
File mapperFile = new File(path);
Map<String,Object> dataMap = new HashMap<>();
generateFileByTemplate(templateName,mapperFile,dataMap);
}
private void generateServiceInterfaceFile(ResultSet resultSet) throws Exception{ final String prefix = "I";
final String suffix = "Service.java";
final String path = diskPath + prefix + changeTableName + suffix;
final String templateName = "ServiceInterface.ftl";
File mapperFile = new File(path);
Map<String,Object> dataMap = new HashMap<>();
generateFileByTemplate(templateName,mapperFile,dataMap);
}
private void generateRepositoryFile(ResultSet resultSet) throws Exception{
final String suffix = "Repository.java";
final String path = diskPath + changeTableName + suffix;
final String templateName = "Repository.ftl";
File mapperFile = new File(path);
Map<String,Object> dataMap = new HashMap<>();
generateFileByTemplate(templateName,mapperFile,dataMap);
}
private void generateDaoFile(ResultSet resultSet) throws Exception{
final String suffix = "DAO.java";
final String path = diskPath + changeTableName + suffix;
final String templateName = "DAO.ftl";
File mapperFile = new File(path);
Map<String,Object> dataMap = new HashMap<>();
generateFileByTemplate(templateName,mapperFile,dataMap);
}
private void generateMapperFile(ResultSet resultSet) throws Exception{
final String suffix = "l";
final String path = diskPath + changeTableName + suffix;
final String templateName = "Mapper.ftl";
final String templateName = "Mapper.ftl";
File mapperFile = new File(path);
Map<String,Object> dataMap = new HashMap<>();
generateFileByTemplate(templateName,mapperFile,dataMap);
}
private void generateFileByTemplate(final String templateName,File file,Map<String,Object> dataMap) throws Exception{ Template template = Template(templateName);
FileOutputStream fos = new FileOutputStream(file);
dataMap.put("table_name_small",tableName);
dataMap.put("table_name",changeTableName);
dataMap.put("author",AUTHOR);
dataMap.put("date",CURRENT_DATE);
dataMap.put("package_name",packageName);
dataMap.put("table_annotation",tableAnnotation);
Writer out = new BufferedWriter(new OutputStreamWriter(fos, "utf-8"),10240);
template.process(dataMap,out);
}
public String replaceUnderLineAndUpperCase(String str){
StringBuffer sb = new StringBuffer();
sb.append(str);
int count = sb.indexOf("_");
while(count!=0){
int num = sb.indexOf("_",count);
count = num + 1;
if(num != -1){
char ss = sb.charAt(count);
char ia = (char) (ss - 32);
}
}
String result = sb.toString().replaceAll("_","");
return StringUtils.capitalize(result);
}
}
FreeMarkerTemplateUtils⼯具类
FreeMarkerTemplateUtils⼯具类⽤来配置模板所在的路径
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论