(1),模板po_.ftl文件
package com.dto;
import java.io.Serializable;
/**
* @date: 2022/11/30 17:39
* @desc: <#list mapDataTableComment as listItmCom > ${listItmCom.columnComment} </#list>实体
*/
public class <#list mapDataTableComment as listItmCom > ${listItmCom.upperColumnName} </#list>Dto extends BaseDto implements Serializable {
<#list mapDataList as listItm >
private String ${listItm.columnName};
</#list>
}
(2),生成实体类
public class TestCls {
public static void main(String[] args) throws Exception {
TestCls testCls = new TestCls();
String testT00_po = "testT00_user";
String generFileName = testT00_po + "Dto.java";
testCls.outTemplate(testT00_po, "用户数据"
, generFileName, "po_.ftl" );
System.out.println("执行成功!");
}
// 输出到模板
private void outTemplate(String entryInfoPoName, String tableComment
, String templateFileName , String templFileName ) throws Exception {
Configuration configuration = new Configuration();
String templateDir = "F:\\xbl\\myResource\\temp_test_";
configuration.setDirectoryForTemplateLoading(new File(templateDir));
Template template = configuration.getTemplate(templFileName);
Map<String, List<EntryInfoPo>> dataMap = new HashMap<String, List<EntryInfoPo>>();
List<EntryInfoPo> listEntryInfo = new ArrayList<EntryInfoPo>();
listEntryInfo = testMetaAddComment();
dataMap.put("mapDataList", listEntryInfo );
// 表注释
dataMap.put("mapDataTableComment", testTableComment(entryInfoPoName, tableComment ));
String generFileDir = "F:\\xbl\\myResource\\temp_test_\\gener_file_\\";
File generFile = new File(generFileDir + templateFileName);
if(generFile.exists()) {
generFile.delete();
}
generFile.createNewFile();
Writer outputWriter = new OutputStreamWriter(new FileOutputStream(generFile ) );
// Writer outputWriter = new OutputStreamWriter(System.out );
template.process(dataMap, outputWriter);
System.out.println("输出完成!");
outputWriter.close();
}
// 表注释
private List<EntryInfoPo> testTableComment(String entryInfoPoName, String tableComment) throws Exception {
List<EntryInfoPo> listTableComment = new ArrayList<EntryInfoPo>();
EntryInfoPo entryInfoPo = new EntryInfoPo();
entryInfoPo.setColumnName(entryInfoPoName);
entryInfoPo.setColumnComment(tableComment);
entryInfoPo.setUpperColumnName(firTextUpper( entryInfoPoName ) );
listTableComment.add(entryInfoPo);
return listTableComment;
}
// 首字母大写
private String firTextUpper(String scontext ) {
int textLength = scontext.length();
String sufText = scontext.substring(1, textLength);
String firText = scontext.substring(0, 1);
firText = firText.toUpperCase();
String finText = firText + sufText;
return finText;
}
private List<EntryInfoPo> testMetaAddComment() throws Exception {
List<EntryInfoPo> listEntryInfoPo = new ArrayList<EntryInfoPo>();
Connection connection = getDataBaseConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("show full columns from t00_user ");
while (resultSet.next()) {
String columnComment = resultSet.getString("Comment");
String columnName = resultSet.getString("Field");
columnName = columnName.toLowerCase();
EntryInfoPo entryInfoPo = new EntryInfoPo();
entryInfoPo.setColumnName(columnName);
entryInfoPo.setColumnComment(columnComment);
listEntryInfoPo.add(entryInfoPo);
}
return listEntryInfoPo;
}
private Connection getDataBaseConnection() throws Exception {
// 连接数据库
Class.forName("com.mysql.jdbc.Driver");
String databaseUrl = "jdbc:mysql://127.0.0.1:3306/grcdb05?useUnicode=true&characterEncoding=utf8";
Connection connection = DriverManager.getConnection(databaseUrl, "root", "root");
return connection;
}
}