代码生成器模板
package entity;
import java.util.Date;
import java.io.Serializable;
public class ${table.className} implements Serializable {
<#list table.columns as column>
private ${column.javaType} ${column.filedName};
</#list>
<#list table.columns as column>
public void set${column.upperFiledName}(${column.javaType} ${column.filedName}){
this.${column.filedName}=${column.filedName};
}
public ${column.javaType} get${column.upperFiledName}(){
return this.${column.filedName};
}
</#list>
}
package cn.code;
import freemarker.template.Configuration;
import freemarker.template.Template;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;
public class Generator {
private Configuration configuration;
//path 模板路径
public void init(String path) throws Exception{
configuration = new Configuration(Configuration.getVersion());
configuration.setDirectoryForTemplateLoading(new File(path));
}
//tempName模板文件的名称
// savePath 保存路径
public void process(String tempName,String savePath,Map<String,Object> param)throws Exception{
//获取模板对应实例
Template template=configuration.getTemplate(tempName);
//组装数据
//初始化保存路径
FileOutputStream fos=new FileOutputStream(savePath);
OutputStreamWriter outputStreamWriter=new OutputStreamWriter(fos);
//传参,生成数据
template.process(param,outputStreamWriter);
}
}
package cn.code;
import cn.code.Moudle.Column;
import cn.code.Moudle.Table;
import cn.code.utils.StringUtils;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/**
* Created by zezhong.shang on 17-6-5.
*/
public class TableUtils {
private static String DBDRIVER = PropertiesUtils.get("database.properties", "driver");
private static String DBURL = PropertiesUtils.get("database.properties", "url");
private static String DBUSER = PropertiesUtils.get("database.properties", "user");
private static String DBPASS = PropertiesUtils.get("database.properties", "password");
private static DatabaseMetaData dmd = null;
static{
Connection conn = null;
try {
Class.forName(DBDRIVER);
conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS);
dmd=conn.getMetaData();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static List<Table> getTable() {
List<Table> tableList = new ArrayList<Table>();
try {
ResultSet rs = dmd.getTables(null, null, null, new String[]{"TABLE"});
while (rs.next()) {
Table table=new Table();
String tableName = rs.getString("TABLE_NAME");
table.setTableName(tableName);
table.setClassName(StringUtils.captureName(StringUtils.putOffUnderline(tableName)));
table.setLowerclassNameNoPrifex(tableName.toLowerCase().replaceAll("itrip",""));
table.setColumns(getColumns(tableName));
tableList.add(table);
}
} catch (Exception e) {
e.printStackTrace();
}
return tableList;
}
public static List<Column> getColumns(String tableName) {
List<Column> columnList=new ArrayList<Column>();
try {
ResultSet rs=dmd.getColumns(null,"%",tableName,"%");
while(rs.next()){
Column column=new Column();
String columnName=rs.getString("COLUMN_NAME");
String columnType=rs.getString("TYPE_NAME");
String remarks=rs.getString("REMARKS");
column.setColumnName(columnName);
column.setColumnType(columnType);
column.setRemarks(remarks);
column.setFiledName(columnName);
column.setUpperFiledName(StringUtils.captureName(columnName));
column.setJavaType(StringUtils.switchType(columnType));
columnList.add(column);
}
} catch (SQLException e) {
e.printStackTrace();
}
return columnList;
}
}
package cn.code;
import java.io.InputStreamReader;
import java.util.Properties;
/**
* <p></p>
* search 项目专用的读取配置文件的工具类
* @author XX
* @version v1.0
* @since 2015/5/25
*/
public class PropertiesUtils {
private static Properties props;
/**
* 加载配置文件
*
* @param fileName
*/
private static void readProperties(String fileName) {
try {
props = new Properties();
InputStreamReader inputStream = new InputStreamReader(PropertiesUtils.class.getClassLoader().getResourceAsStream(fileName), "UTF-8");
props.load(inputStream);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 根据key读取对应的value
*
* @param key
* @return
*/
public static String get(String fileName,String key) {
readProperties(fileName);
return props.getProperty(key);
}
}
package cn.code.utils;
/**
* Created by zezhong.shang on 17-6-2.
*/
public class StringUtils {
/**
* ijλ����ĸ��д
*
* @param name
* @param indx
* @return
*/
public static String captureName(String name, int indx) {
name = name.substring(0, indx) + name.substring(indx, indx + 1).toUpperCase() + name.substring(indx + 1);
return name;
}
/**
* ����ĸ��д
*
* @param name
* @return
*/
public static String captureName(String name) {
name = name.substring(0, 1).toUpperCase() + name.substring(1);
return name;
}
/***
* ����ĸСд
*
* @param name
* @return
*/
public static String lowerName(String name) {
name = name.substring(0, 1).toLowerCase() + name.substring(1);
return name;
}
/***
* ȥ���»��� �����շ�ԭ�����ת��
*
* @return
*/
public static String putOffUnderline(String columnName) {
StringBuffer fieldNameBuffer = null;
String tempNameArray[] = columnName.split("_");
for (int i = 0; i < tempNameArray.length; i++) {
if (i == 0) {
fieldNameBuffer = new StringBuffer(tempNameArray[i]);
} else {
fieldNameBuffer.append(captureName(tempNameArray[i]));
}
}
return fieldNameBuffer.toString();
}
public static String switchType(String columnType) {
String javaType = null;
switch (columnType) {
case "VARCHAR":
javaType = "String";
break;
case "INT":
javaType = "Integer";
break;
case "DATETIME":
javaType = "Date";
break;
case "BINGINT":
javaType = "Long";
break;
default:
javaType = "String";
break;
}
return javaType;
}
public static void main(String[] args) {
String a = putOffUnderline("a_bccc_d");
System.out.println(a);
}
}
package cn.code.Moudle;
public class Column {
private String columnName; //列名称
private String filedName; //属性名称
private String upperFiledName; //属性名称开头字母大写
private String columnType; //列类型
private String javaType; //转化java类型名称
private String remarks; //备注
public String getRemarks() {
return remarks;
}
public void setRemarks(String remarks) {
this.remarks = remarks;
}
public String getColumnName() {
return columnName;
}
public void setColumnName(String columnName) {
this.columnName = columnName;
}
public String getFiledName() {
return filedName;
}
public void setFiledName(String filedName) {
this.filedName = filedName;
}
public String getUpperFiledName() {
return upperFiledName;
}
public void setUpperFiledName(String upperFiledName) {
this.upperFiledName = upperFiledName;
}
public String getColumnType() {
return columnType;
}
public void setColumnType(String columnType) {
this.columnType = columnType;
}
public String getJavaType() {
return javaType;
}
public void setJavaType(String javaType) {
this.javaType = javaType;
}
}
package cn.code.Moudle;
import java.util.ArrayList;
import java.util.List;
public class Table {
private String tableName;//表名称
private String className; //转化成class类名称
private String lowerclassNameNoPrifex; //没有前缀的类名
List<Column> columns =new ArrayList<Column>();
public String getLowerclassNameNoPrifex() {
return lowerclassNameNoPrifex;
}
public void setLowerclassNameNoPrifex(String lowerclassNameNoPrifex) {
this.lowerclassNameNoPrifex = lowerclassNameNoPrifex;
}
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public List<Column> getColumns() {
return columns;
}
public void setColumns(List<Column> columns) {
this.columns = columns;
}
}
package cn.code;
import cn.code.Moudle.Table;
import cn.code.utils.StringUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ModuleHandller {
private Generator generator=new Generator();
private String savepath="E:\\generator";
public void executeModule()throws Exception {
List<Table> tableList=TableUtils.getTable();
Map<String,Object> param = new HashMap<String,Object>();
String templatepath=this.getClass().getClassLoader().getResource("").getPath()+"\\smbms\\";
for(Table table:tableList){
param.put("table",table);
generator.init(templatepath);
generator.process("NewDetail.ftl",savepath+"\\"+table.getClassName()+".java",param);
}
}
public static void main(String[] args) {
ModuleHandller moduleHandller =new ModuleHandller();
System.out.println(1);
try {
moduleHandller.executeModule();
} catch (Exception e) {
e.printStackTrace();
}
}
}