FreeMarker 自动生成代码 一 根据数据库表字段生成 java bean
第一步,创建模版
package ${packageName}.${beanName};
/**
**/
public class ${beanNameClass} {
/**
**/
<#list metaDataList as metaData>
private ${metaData.variableType} ${metaData.fieldName};
</#list>
/**
**/
public ${beanNameClass}(){
super();
}
/**
* setter and getter
**/
<#list metaDataList as metaData>
public ${metaData.variableType} get${metaData.fuFieldName}(){
return ${metaData.fieldName};
}
public void set${metaData.fuFieldName}(${metaData.variableType} ${metaData.fieldName}){
this.${metaData.fieldName}=${metaData.fieldName};
}
</#list>
}
package org.scf.codeGen.tabel2Bean;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
public class Table2BeanUtil {
private Configuration cfg = null;
private final String CODE_FILE_CHARSET = "GBK";
private String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8";
Connection con = null;
private static String targetPath = "src\\org\\scf\\codeGen\\tabel2Bean";
private static String tableStr = "studentscf";
public static void main(String[] args) throws Exception {
Table2BeanUtil maker = new Table2BeanUtil();
maker.init();
File targetFilepath = new File(targetPath+"\\"+tableStr.toLowerCase());
if(!targetFilepath.exists()){
targetFilepath.mkdirs();
}
File targetFile = new File(targetFilepath.getPath()+"\\"+firstUpper(tableStr)+".java");
if(!targetFile.exists()){
targetFile.createNewFile();
}
String templateFile = "Bean.ftl";
maker.process(maker.getAllColumn(tableStr), targetFile, templateFile);
}
//初始化工作
public void init() throws Exception {
cfg = new Configuration();
//设置模板文件位置
cfg.setDirectoryForTemplateLoading(new File("src\\org\\scf\\codeGen\\tabel2Bean\\template"));
// 加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(url , "root" , "pccw" ) ;
}
// 获得所有的数据列
public Map getAllColumn(String tableName){
Map rootMap = new HashMap();
List list = new ArrayList();
try {
DatabaseMetaData dmd = con.getMetaData();
ResultSet rs = dmd.getColumns("","test",tableName, null);
int i = 0;
while(rs.next()){
String fieldName = rs.getString("COLUMN_NAME");
String variableType = rs.getString("TYPE_NAME").equals("varchar") ? "String" :"";
Map rootMap1 = new HashMap();
rootMap1.put("fieldName", fieldName);
rootMap1.put("fuFieldName", firstUpper(fieldName));
rootMap1.put("variableType", variableType);
list.add(rootMap1);
}
rootMap.put("metaDataList", list);
rootMap.put("packageName","org.scf.codeGen.tabel2Bean.target");
rootMap.put("beanName", tableName);
rootMap.put("beanNameClass", firstUpper(tableName));
} catch (SQLException e) {
e.printStackTrace();
}
return rootMap;
}
//模板 + 数据模型 = 输出
public void process(Map rootMap,File targetFile,String templateFile) {
OutputStreamWriter osw = null;
try {
osw = new OutputStreamWriter(new FileOutputStream(targetFile), CODE_FILE_CHARSET);
Template template = cfg.getTemplate(templateFile);
template.process(rootMap, osw);
osw.flush();
osw.close();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(osw != null){
try {
osw.close();
} catch (IOException e) {
}
}
}
}
private static String firstUpper(String str){
String c = str.substring(0,1);
String upperC = c.toUpperCase();
return upperC+str.substring(1);
}
}
- 点赞
- 收藏
- 关注作者
评论(0)