package com.eic.common.generator;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;
import com.eic.config.EicConfig;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.c3p0.C3p0Plugin;
/**
* 《锦瑟》
年代: 唐 作者: 李商隐
锦瑟无端五十弦,一弦一柱思华年。
庄生晓梦迷蝴蝶,望帝春心托杜鹃。
沧海月明珠有泪,蓝田日暖玉生烟。
此情可待成追忆,只是当时已惘然。
*
*/
enum Data_Type{
/**
* 数据库类型,可自行扩充,
*/
sqlserver,
mysql,
oracle
}
/**
* 自己定义的生成器,在控制台打出
*/
public class MyGenerator
{
private static Data_Type data_type = Data_Type.sqlserver;//数据库类型
private static String db_name = "Java_FLDB_Formal";//数据库名称
private static String table_name = "Activity_Subject";//目标表名称
private static String getColumns() throws SQLException {
PropKit.use("a_little_config.txt");
C3p0Plugin c3p0Plugin = EicConfig.createC3p0Plugin();
c3p0Plugin.start();
String cols = "";
if(data_type == Data_Type.sqlserver){
Statement statement = c3p0Plugin.getDataSource().getConnection().createStatement();
String sql = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS where table_catalog = '"+db_name+"' "
+ "and table_name = '"+table_name+"'" ;
ResultSet resultSet = statement.executeQuery(sql);
while(resultSet.next()){
Object column = resultSet.getObject("COLUMN_NAME");
Object data_type = resultSet.getObject("DATA_TYPE");
String javaDataType = getJavaDataType(data_type.toString());
cols += column.toString() + "|" + javaDataType;
cols += ",";
}
cols = cols.substring(0, cols.lastIndexOf(","));
}
return cols;
}
public static void main(String[] args) throws SQLException {
String cols = getColumns();
String[] columns = cols.split(",");
for (String column : columns) {
String[] strs = column.split("\\|");
String col = strs[0];
String javaType = strs[1];
String columnNew = getColumnNew(col);
String sb_set = get_Column_Set(col, columnNew,javaType);
String sb_get = get_Column_Get(col, columnNew,javaType);
System.out.println(sb_set.toString());
System.out.println(sb_get.toString());
}
}
private static String getColumnNew(String column){
String column_new = column.substring(0, 1).toUpperCase() +
column.substring(1, 2).toLowerCase() +
column.substring(2);
return column_new;
}
private static String get_Column_Set(String column,String columnNew,String javaType){
StringBuilder sb_set = new StringBuilder().append("public void set")
.append(columnNew)
.append("(")
.append(javaType)
.append(" ")
.append(column)
.append(")")
.append("{\r\n")
.append(" ")
.append("set(\"")
.append(column)
.append("\",")
.append(column)
.append(");\r\n")
.append("}");
return sb_set.toString();
}
private static String get_Column_Get(String column,String columnNew,String javaType){
StringBuilder sb_get = new StringBuilder();
sb_get.append("public ")
.append(javaType)
.append(" get")
.append(columnNew)
.append("()")
.append("{\r\n")
.append(" ")
.append("return get(\"")
.append(column)
.append("\");")
.append("\r\n")
.append("}");
return sb_get.toString();
}
private static String getJavaDataType(String data_type){
if(MyGenerator.data_type == Data_Type.sqlserver){
if (data_type.equals("int"))
return "int";
if (data_type.equals("varchar"))
return "String";
if (data_type.equals("numeric"))
return "BigDecimal";
}
return "String";
}
}
输出结果如下:
public void setId(String ID){
set("ID",ID);
}
public void setId(String ID){
set("ID",ID);
}
public void setItype(String iType){
set("iType",iType);
}
public void setItype(String iType){
set("iType",iType);
}
public void setSname(String sName){
set("sName",sName);
}
public void setSname(String sName){
set("sName",sName);
}
Ps:非常简单,但这就是我想要的