package com.jm.base.generate;
import java.io.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 自动生成MyBatis的实体类、实体映射XML文件、Mapper、Service、ServiceImpl、Controller
*
* @author wp
* @version v1.0
* @date 2018-12-15
*/
@SuppressWarnings("hiding")
public class EntityUtil {
/**
* *********************************使用前必读*******************
* *
* * 使用前请将moduleName更改为自己模块的名称即可(一般情况下与数据库名一致),其他无须改动。
* *
* **********************************************************
*/
private final String type_char = "char";
private final String type_date = "date";
private final String type_timestamp = "timestamp";
private final String type_int = "int";
private final String type_bigint = "bigint";
private final String type_text = "text";
private final String type_bit = "bit";
private final String type_decimal = "decimal";
private final String type_blob = "blob";
private final String type_double = "double";
private final String type_time = "time";
private final String bean_path = "D:";
private final String mapper_path = "D:";
private final String xml_path = "D:";
private final String service_path = "D:";
private final String service_impl_path = "D:";
private final String controller_path = "D:";
private final String bean_package = "com.demo.domain";
private final String mapper_package = "com.demo.mapper";
private final String service_package = "com.demo.service";
private final String service_impl_package = "com.demo.service";
private final String controller_package = "com.demo.controller";
//库名
private final String moduleName = "crm"; // 对应模块名称(根据自己模块做相应调整!!!务必修改^_^)
private final String driverName = "com.mysql.jdbc.Driver";
private final String user = "root";
private final String password = "admin";
private final String url = "jdbc:mysql://127.0.0.1:3306/" + moduleName + "?characterEncoding=utf8";
private String tableName = null;
private String beanName = null;
private String mapperName = null;
private Connection conn = null;
public static void main(String[] args) {
try {
//customizeTables有值,生成指定表,否则生成全库的表
List<String> customizeTables = new ArrayList<>();
customizeTables.add("tb_stu");
new EntityUtil().generate(customizeTables);
//自动打开生成文件的目录
// Runtime.getRuntime().exec("cmd /c start explorer D:\\");
} catch (Exception e) {
e.printStackTrace();
}
}
public void generate(List customizeTables) throws ClassNotFoundException, SQLException, IOException {
init();
String prefix = "show full fields from ";
List<String> columns = null;
List<String> types = null;
List<String> comments = null;
PreparedStatement pstate = null;
List<String> tables = null;
if (customizeTables.size() == 0) {
tables = getTables();
} else {
tables = customizeTables;
}
Map<String, String> tableComments = getTableComment();
for (String table : tables) {
columns = new ArrayList<String>();
types = new ArrayList<String>();
comments = new ArrayList<String>();
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"));
}
tableName = table;
processTable(table);
// this.outputBaseBean();
String tableComment = tableComments.get(tableName);
buildEntityBean(columns, types, comments, tableComment);
buildMapper();
buildMapperXml(columns, types, comments);
buildService();
buildServiceImpl();
buildController();
}
conn.close();
}
private void init() throws ClassNotFoundException, SQLException {
Class.forName(driverName);
conn = DriverManager.getConnection(url, user, password);
}
/**
* 获取所有的表
*
* @return
* @throws SQLException
*/
private List<String> getTables() throws SQLException {
List<String> tables = new ArrayList<String>();
PreparedStatement pstate = conn.prepareStatement("show tables");
ResultSet results = pstate.executeQuery();
while (results.next()) {
String tableName = results.getString(1);
// if ( tableName.toLowerCase().startsWith("yy_") ) {
tables.add(tableName);
// }
}
return tables;
}
private void processTable(String table) {
StringBuffer sb = new StringBuffer(table.length());
String tableNew = table.toLowerCase();
String[] tables = tableNew.split("_");
String temp = null;
for (int i = 1; i < tables.length; i++) {
temp = tables[i].trim();
sb.append(temp.substring(0, 1).toUpperCase()).append(temp.substring(1));
}
beanName = sb.toString();
mapperName = beanName + "Mapper";
}
private String processType(String type) {
if (type.indexOf(type_char) > -1) {
return "String";
} else if (type.indexOf(type_bigint) > -1) {
return "Long";
} else if (type.indexOf(type_int) > -1) {
return "Integer";
} else if (type.indexOf(type_date) > -1) {
return "java.util.Date";
} else if (type.indexOf(type_text) > -1) {
return "String";
} else if (type.indexOf(type_timestamp) > -1) {
return "java.util.Date";
} else if (type.indexOf(type_bit) > -1) {
return "Boolean";
} else if (type.indexOf(type_decimal) > -1) {
return "java.math.BigDecimal";
} else if (type.indexOf(type_blob) > -1) {
return "byte[]";
} else if (type.indexOf(type_double) > -1) {
return "Double";
} else if (type.indexOf(type_time) > -1) {
return "String";
}
return null;
}
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]);
for (int i = 1; i < fields.length; i++) {
temp = fields[i].trim();
sb.append(temp.substring(0, 1).toUpperCase()).append(temp.substring(1));
}
return sb.toString();
}
/**
* 将实体类名首字母改为小写
*
* @param beanName
* @return
*/
private String processResultMapId(String beanName) {
return beanName.substring(0, 1).toLowerCase() + beanName.substring(1);
}
/**
* 构建类上面的注释
*
* @param bw
* @param text
* @return
* @throws IOException
*/
private BufferedWriter buildClassComment(BufferedWriter bw, String text) throws IOException {
bw.newLine();
bw.write("/**");
bw.newLine();
bw.write(" * ");
bw.newLine();
bw.write(" * " + text);
bw.newLine();