GenerateParamsUtil类
import java.io.*;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;
/**
* cmbxb 55.12.221.63/55.12.205.225 3306 app_usr/App_usr@2018
* cma11 55.12.221.57/55.12.205.219 3306 app_usr/App_usr@2018
*/
public class GenerateParamsUtil {
private final String bean_path = "d:/generator/bean";//生成到指定路径
private final String xml_path = "d:/generator/xml";
private final String txt_path = "d:/generator/txt";
private final String mapper_path = "d:/generator/mapper";
private final String bean_package = "com.cmb.dw.rtl.yearbil.dao.yearbil";
private final String mapper_namespace = "com.cmb.dw.rtl.yearbil.dao.yearbil";
private final String mapper_package = "com.cmb.dw.rtl.yearbil.dao.yearbil";
private final String driverName = "com.mysql.jdbc.Driver";
private final String user = "app_usr";
private final String password = "App_usr@2018";
private final String url = "jdbc:mysql://55.12.221.57:3306/cmb2r1?characterEncoding=utf-8";
private Connection conn = null;
static Map tables =new HashMap();
static{
tables.put("artc_r_cb_pb_cust_uid_mng_inf", "CustUidMngInfOutParams");
}
/**
*
* @Title: init
* @Description: 连接数据库
* @param @throws ClassNotFoundException
* @param @throws SQLException 设定文件
* @return void 返回类型
* @throws
*/
private void init() throws ClassNotFoundException, SQLException {
Class.forName(driverName);
conn = DriverManager.getConnection(url, user, password);
}
public void generate() throws ClassNotFoundException, SQLException, IOException {
init();
String prefix = "show full fields from ";
List columns = null;
List types = null;
List comments = null;
PreparedStatement pstate = null;
Map tableComments = getTableComment();
for ( String table : tables.keySet() ) {
columns = new ArrayList();
types = new ArrayList();
comments = new ArrayList();
pstate = conn.prepareStatement(prefix + table);
ResultSet results = pstate.executeQuery();
while ( results.next() ) {
columns.add(results.getString("FIELD"));
types.add(results.getString("TYPE"));
comments.add(results.getString("COMMENT"));
}
String entityName=tables.get(table);
String tableComment = tableComments.get(table);
buildEntityBean(entityName,columns, types, comments, tableComment);
buildMapperXml(entityName,columns, types, table);
buildBeanTxt(entityName, columns, comments);
//buildMapperDao(entityName,columns, types, comments, tableComment);
}
conn.close();
}
/**
*
* @Title: processType
* @Description: 字段类型都为String
* @param @param type
* @param @return 设定文件
* @return String 返回类型
* @throws
*/
private String processType( String type ) {
return "String";
}
/**
* 生成实体类
*
* @param columns
* @param types
* @param comments
* @throws java.io.IOException
*/
private void buildEntityBean(String entityName, List columns, List types, List comments, String tableComment )
throws IOException {
File folder = new File(bean_path);
if ( !folder.exists() ) {
folder.mkdir();
}
File beanFile = new File(bean_path, entityName + ".java");
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(beanFile)));
bw.write("package " + bean_package + ";");
bw.newLine();
bw.write("import java.io.Serializable;");
bw.newLine();
bw = buildClassComment(bw, entityName,tableComment);
bw.newLine();
bw.write("public class " + entityName + " implements Serializable {");
bw.newLine();
bw.write("\tprivate static final long serialVersionUID = 8929159233764146422L;");
bw.newLine();
int size = columns.size();
for ( int i = 0 ; i < size ; i++ ) {
bw.write("\t/**" + comments.get(i) + "**/");
bw.newLine();
bw.write("\tprivate " + processType(types.get(i)) + " " + processField(columns.get(i)) + ";");
bw.newLine();
bw.newLine();
}
bw.newLine();
// 生成get 和 set方法
String tempField = null;
String _tempField = null;
String tempType = null;
for ( int i = 0 ; i < size ; i++ ) {
tempType = processType(types.get(i));
_tempField = processField(columns.get(i));
tempField = _tempField.substring(0, 1).toUpperCase() + _tempField.substring(1);
bw.newLine();
// bw.write("\tpublic void set" + tempField + "(" + tempType + " _" + _tempField + "){");
bw.write("\tpublic void set" + tempField + "(" + tempType + " " + _tempField + "){");
bw.newLine();
// bw.write("\t\tthis." + _tempField + "=_" + _tempField + ";");
bw.write("\t\tthis." + _tempField + " = " + _tempField + ";");
bw.newLine();
bw.write("\t}");
bw.newLine();
bw.newLine();
bw.write("\tpublic " + tempType + " get" + tempField + "(){");
bw.newLine();
bw.write("\t\treturn this." + _tempField + ";");
bw.newLine();
bw.write("\t}");
bw.newLine();
}
bw.newLine();
bw.write("}");
bw.newLine();
bw.flush();
bw.close();
}
private String processField( String field ) {
StringBuffer sb = new StringBuffer(field.length());
//field = field.toLowerCase();
String[] fields = field.split("_");
String temp = null;
sb.append(fields[0].toLowerCase());
for ( int i = 1 ; i < fields.length ; i++ ) {
temp = fields[i].trim();
sb.append(temp.substring(0, 1).toUpperCase()).append(temp.substring(1).toLowerCase());
}
return sb.toString();
}
/**
* 构建类上面的注释
*
* @param bw
* @param text
* @return
* @throws java.io.IOException
*/
private BufferedWriter buildClassComment( BufferedWriter bw,String entityName, String text ) throws IOException {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
bw.newLine();
bw.newLine();
bw.write("/**");
bw.newLine();
bw.write(" * ");
bw.newLine();
bw.write(" *@ClassName: " + entityName);
bw.newLine();
bw.write(" *@Description:"+text);
bw.newLine();
bw.write(" *@author:lvyanghui");
bw.newLine();
bw.write(" *@date:"+sdf.format(new Date()));
bw.newLine();
bw.write(" **/");
return bw;
}
/**
* 构建实体类映射XML文件
*
* @param columns
* @param types
* @param table
* @throws java.io.IOException
*/
private void buildMapperXml(String entityName, List columns, List types,String table) throws IOException {
File folder = new File(xml_path);
if ( !folder.exists() ) {
folder.mkdirs();
}
File mapperXmlFile = new File(xml_path, entityName + ".xml");
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(mapperXmlFile)));
bw.write("");
bw.newLine();
bw.write("");
bw.newLine();
bw.write("");
bw.newLine();
bw.newLine();
buildResultMap(entityName,bw, columns);
bw.newLine();
bw.newLine();
buildSqlList(entityName, bw, columns);
bw.newLine();
bw.newLine();
buildSelectSql(entityName,bw,columns,table);
bw.newLine();
bw.newLine();
buildInsertSql(entityName,bw,columns,table);
bw.newLine();
bw.newLine();
buildUpdateSql(entityName,bw,columns,table);
bw.newLine();
bw.newLine();
buildDeleteSql(entityName,bw,columns,table);
bw.newLine();
bw.newLine();
bw.write(" ");
bw.flush();
bw.close();
}
private void buildResultMap(String entityName,BufferedWriter bw, List columns) throws IOException {
bw.write(" ");
bw.newLine();
int size = columns.size();
for ( int i = 0 ; i < size ; i++ ) {
bw.write(" ");
bw.newLine();
}
bw.write(" ");
}
private void buildSqlList(String entityName,BufferedWriter bw, List columns) throws IOException {
bw.write(" ");
bw.newLine();
int size = columns.size();
for ( int i = 0 ; i < size ; i++ ) {
if(i == size - 1){
bw.write(" " + columns.get(i));
bw.newLine();
}else{
bw.write(" " + columns.get(i) + ",");
bw.newLine();
}
}
bw.write(" ");
}
private void buildSelectSql(String entityName,BufferedWriter bw, List columns, String table)throws IOException{
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
String selectId = "search" + entityName.replace("OutParams","") + "Pb";
String resultMap = entityName + "Map";
bw.write(" ");
bw.newLine();
bw.write(" ");
}
private void buildInsertSql(String entityName,BufferedWriter bw, List columns, String table)throws IOException{
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
String insertId = "insert" + entityName.replace("OutParams","") + "Pb";
bw.write(" ");
bw.newLine();
bw.write(" ");
bw.newLine();
bw.write(" INSERT INTO " + table );
bw.newLine();
bw.write(" (");
bw.newLine();
int size = columns.size();
for (int i = 0; i < size; i++) {
if(i == size - 1){
bw.write(" " + columns.get(i));
}else{
bw.write(" " + columns.get(i) + ",");
bw.newLine();
}
}
bw.newLine();
bw.write(" )");
bw.newLine();
bw.write(" VALUES");
bw.newLine();
bw.write(" (");
bw.newLine();
for (int i = 0; i < size; i++) {
if(i == size - 1){
bw.write(" #{" + processField(columns.get(i)) + "}");
}else{
bw.write(" #{" + processField(columns.get(i)) + "},");
bw.newLine();
}
}
bw.newLine();
bw.write(" )");
bw.newLine();
bw.write(" ");
}
private void buildUpdateSql(String entityName,BufferedWriter bw, List columns, String table)throws IOException{
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
String updateId = "update" + entityName.replace("OutParams","") + "Pb";
bw.write(" ");
bw.newLine();
bw.write(" ");
bw.newLine();
bw.write(" UPDATE " + table );
bw.newLine();
bw.write(" SET");
bw.newLine();
int size = columns.size();
for (int i = 0; i < size; i++) {
if(i == size - 1){
bw.write(" " + columns.get(i) + " = #{" + processField(columns.get(i)) + "}");
}else{
bw.write(" " + columns.get(i) + " = #{" + processField(columns.get(i)) + "},");
bw.newLine();
}
}
bw.newLine();
bw.write(" WHERE");
bw.newLine();
bw.write(" " + columns.get(0) + " = #{" + processField(columns.get(0)) + "}");
bw.newLine();
bw.write(" ");
}
private void buildDeleteSql(String entityName,BufferedWriter bw, List columns, String table)throws IOException{
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
String deleteId = "delete" + entityName.replace("OutParams","") + "Pb";
bw.write(" ");
bw.newLine();
bw.write(" ");
bw.newLine();
bw.write(" DELETE FROM " + table );
bw.newLine();
bw.write(" WHERE");
bw.newLine();
bw.write(" " + columns.get(0) + " = #{" + processField(columns.get(0)) + "}");
bw.newLine();
bw.write(" ");
}
/**
* 生成字段加注释txt文件
* @param entityName
* @param columns
* @param comments
* @throws java.io.IOException
*/
public void buildBeanTxt(String entityName, List columns, List comments)throws IOException{
File folder = new File(txt_path);
if ( !folder.exists() ) {
folder.mkdir();
}
File txtFile = new File(txt_path, entityName + ".txt");
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(txtFile)));
int size = columns.size();
for (int i = 0; i < size; i++) {
bw.write(processField(columns.get(i)) + " " + comments.get(i));
bw.newLine();
}
bw.flush();
bw.close();
}
/**
* 生成dao层
*
* @param columns
* @param types
* @param comments
* @throws java.io.IOException
*/
private void buildInterfaceDao(String entityName, List columns, List types, List comments, String tableComment )
throws IOException {
File folder = new File(mapper_path);
if ( !folder.exists() ) {
folder.mkdir();
}
String fileName = entityName.replace("OutParams","") + "Dao.java";
File beanFile = new File(fileName);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(beanFile)));
bw.write("package " + mapper_package + ";");
bw.newLine();
bw.write("import " + bean_package + entityName + ";");
bw.newLine();
bw.write("import com.cmb.framework.exception.FrameworkException;");
bw.newLine();
bw = buildClassComment(bw, entityName,tableComment);
bw.newLine();
bw.write("public class " + entityName + " implements Serializable {");
bw.newLine();
bw.write("\tprivate static final long serialVersionUID = 8929159233764146422L;");
bw.newLine();
int size = columns.size();
for ( int i = 0 ; i < size ; i++ ) {
bw.write("\t/**" + comments.get(i) + "**/");
bw.newLine();
bw.write("\tprivate " + processType(types.get(i)) + " " + processField(columns.get(i)) + ";");
bw.newLine();
bw.newLine();
}
bw.newLine();
// 生成get 和 set方法
String tempField = null;
String _tempField = null;
String tempType = null;
for ( int i = 0 ; i < size ; i++ ) {
tempType = processType(types.get(i));
_tempField = processField(columns.get(i));
tempField = _tempField.substring(0, 1).toUpperCase() + _tempField.substring(1);
bw.newLine();
// bw.write("\tpublic void set" + tempField + "(" + tempType + " _" + _tempField + "){");
bw.write("\tpublic void set" + tempField + "(" + tempType + " " + _tempField + "){");
bw.newLine();
// bw.write("\t\tthis." + _tempField + "=_" + _tempField + ";");
bw.write("\t\tthis." + _tempField + " = " + _tempField + ";");
bw.newLine();
bw.write("\t}");
bw.newLine();
bw.newLine();
bw.write("\tpublic " + tempType + " get" + tempField + "(){");
bw.newLine();
bw.write("\t\treturn this." + _tempField + ";");
bw.newLine();
bw.write("\t}");
bw.newLine();
}
bw.newLine();
bw.write("}");
bw.newLine();
bw.flush();
bw.close();
}
/**
* 获取所有的数据库表注释
*
* @return
* @throws java.sql.SQLException
*/
private Map getTableComment() throws SQLException {
Map maps = new HashMap();
PreparedStatement pstate = conn.prepareStatement("show table status");
ResultSet results = pstate.executeQuery();
while ( results.next() ) {
String tableName = results.getString("NAME");
if(tables.containsKey(tableName)){
String comment = results.getString("COMMENT");
maps.put(tableName, comment);
}
}
return maps;
}
public static void main( String[] args ) {
try {
new GenerateParamsUtil().generate();
// 自动打开生成文件的目录
//Runtime.getRuntime().exec("cmd /c start explorer D:\\");
} catch ( ClassNotFoundException e ) {
e.printStackTrace();
} catch ( SQLException e ) {
e.printStackTrace();
} catch ( IOException e ) {
e.printStackTrace();
}
}
}