You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
233 lines
7.6 KiB
233 lines
7.6 KiB
package com.dsideal.Tools;
|
|
|
|
import cn.hutool.core.date.DateUnit;
|
|
import cn.hutool.core.io.FileUtil;
|
|
import com.dsideal.Utils.dsKit;
|
|
import com.jfinal.kit.PropKit;
|
|
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
|
|
import com.jfinal.plugin.hikaricp.HikariCpPlugin;
|
|
|
|
import javax.sql.DataSource;
|
|
import java.io.File;
|
|
import java.sql.*;
|
|
import java.util.*;
|
|
|
|
public class GenerateCode {
|
|
|
|
// JavaBean保存的位置
|
|
public static String beanPath;
|
|
// JavaBean包名
|
|
public static String beanPackage;
|
|
|
|
// mysql与java的数据类型映射
|
|
public static Map<String, String> dataTypeMap = new HashMap<>();
|
|
|
|
/**
|
|
* 获取当前数据库下的表
|
|
*
|
|
* @return List<String>
|
|
* @throws Exception
|
|
*/
|
|
public static List<String> getTableNames(Connection conn) throws Exception {
|
|
List<String> tables = new ArrayList<>();
|
|
Statement stmt = conn.createStatement();
|
|
ResultSet rs = stmt.executeQuery("show tables");
|
|
while (rs.next()) {
|
|
String tableName = rs.getString(1);
|
|
if (tableName.equals(PropKit.get("tableName"))) {
|
|
tables.add(tableName);
|
|
}
|
|
}
|
|
rs.close();
|
|
stmt.close();
|
|
return tables;
|
|
}
|
|
|
|
|
|
/**
|
|
* 获得某表的注释信息+列信息
|
|
*
|
|
* @param table
|
|
* @return
|
|
* @throws Exception
|
|
*/
|
|
public static void getStructure(Connection conn, String table) throws Exception {
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
//表注释
|
|
String comment = "";
|
|
Statement stmt = conn.createStatement();
|
|
ResultSet rs = stmt.executeQuery("show create table " + table);
|
|
if (rs != null && rs.next()) {
|
|
String createDDL = rs.getString(2);
|
|
int index = createDDL.indexOf("COMMENT='");
|
|
if (index >= 0) {
|
|
comment = createDDL.substring(index + 9);
|
|
comment = comment.substring(0, comment.length() - 1);
|
|
}
|
|
}
|
|
//类名
|
|
table = capitalizeFirstLetter(table);
|
|
String className = toCamelCase(table);
|
|
|
|
//包名
|
|
sb.append("package " + beanPackage + ";\n");
|
|
sb.append("\n");
|
|
|
|
sb.append("import lombok.Setter;\n");
|
|
sb.append("import lombok.Getter;\n");
|
|
|
|
//表描述
|
|
sb.append("// " + comment + "\n");
|
|
sb.append("public class " + className + " {\n");
|
|
|
|
List<String> fields = new ArrayList<>();
|
|
//字段信息
|
|
rs = stmt.executeQuery("show full columns from " + table);
|
|
while (rs.next()) {
|
|
String Type = rs.getString("Type");
|
|
String Field = rs.getString("Field");
|
|
fields.add(Field);
|
|
|
|
if (Type.contains("(")) {
|
|
Type = Type.substring(0, Type.indexOf("("));
|
|
}
|
|
Type = Type.toUpperCase();
|
|
|
|
// System.out.println(rs.getString("Key"));//是不是主键 PRI是主键
|
|
sb.append(" // " + rs.getString("Comment") + "\n");
|
|
sb.append(" @Getter\n");
|
|
sb.append(" @Setter\n");
|
|
sb.append(" private " + dataTypeMap.get(Type) + " " + Field + ";\n");
|
|
}
|
|
sb.append("\n");
|
|
sb.append(" public String toString(){\n");
|
|
sb.append(" return \"{");
|
|
|
|
for (int i = 0; i < fields.size(); i++) {
|
|
String field = fields.get(i);
|
|
sb.append(" " + field + ": \" + " + field + " +\"");
|
|
if (i < fields.size() - 1) {
|
|
sb.append(",");
|
|
}
|
|
}
|
|
sb.append("}\";\n");
|
|
sb.append(" }\n");
|
|
sb.append("}\n");
|
|
String fileName = beanPath + File.separator + className + ".java";
|
|
FileUtil.writeUtf8String(sb.toString(), fileName);
|
|
//关闭连接
|
|
rs.close();
|
|
stmt.close();
|
|
}
|
|
|
|
/**
|
|
* 功能:初始化数据库类型映射
|
|
*/
|
|
public static void initMap() {
|
|
//mysql与java的数据类型映射
|
|
dataTypeMap.put("INT", "int");
|
|
dataTypeMap.put("BIGINT", "long");
|
|
dataTypeMap.put("DOUBLE", "double");
|
|
dataTypeMap.put("FLOAT", "float");
|
|
dataTypeMap.put("TINYINT", "int");
|
|
dataTypeMap.put("DATE", "java.util.Date");
|
|
dataTypeMap.put("TIMESTAMP", "java.util.Date");
|
|
dataTypeMap.put("BIGINT UNSIGNED", "long");
|
|
dataTypeMap.put("INT UNSIGNED", "long");
|
|
dataTypeMap.put("MEDIUMINT", "int");
|
|
dataTypeMap.put("MEDIUMINT UNSIGNED", "long");
|
|
dataTypeMap.put("SMALLINT", "int");
|
|
dataTypeMap.put("SMALLINT UNSIGNED", "long");
|
|
dataTypeMap.put("TINYINT UNSIGNED", "long");
|
|
dataTypeMap.put("LONGTEXT", "String");
|
|
dataTypeMap.put("VARCHAR", "String");
|
|
dataTypeMap.put("CHAR", "String");
|
|
dataTypeMap.put("TEXT", "String");
|
|
dataTypeMap.put("DATETIME", "java.util.Date");
|
|
dataTypeMap.put("DECIMAL", "BigDecimal");
|
|
dataTypeMap.put("BOOLEAN", "boolean");
|
|
}
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
//加载配置文件
|
|
PropKit.use("application.properties");
|
|
beanPackage = PropKit.get("beanPackage");
|
|
beanPath = PropKit.get("beanPath");
|
|
beanPath = beanPath + "/" + beanPackage.replace(".", "/");
|
|
if (!FileUtil.exist(beanPath)) {
|
|
FileUtil.mkdir(beanPath);
|
|
}
|
|
initMap();
|
|
|
|
// 配置Druid数据源插件
|
|
HikariCpPlugin hpPlugin = new HikariCpPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"),
|
|
PropKit.get("password").trim(), PropKit.get("driverClassName"));
|
|
hpPlugin.start();
|
|
|
|
// 配置ActiveRecord插件
|
|
ActiveRecordPlugin arp = new ActiveRecordPlugin(hpPlugin);
|
|
hpPlugin.start();
|
|
arp.start();
|
|
DataSource dataSource = hpPlugin.getDataSource();
|
|
// 数据库连接
|
|
Connection conn = dataSource.getConnection();
|
|
|
|
//开始生成
|
|
List<String> tables = getTableNames(conn);
|
|
|
|
String tableNames = "";
|
|
//表描述
|
|
for (String table : tables) {
|
|
getStructure(conn, table);
|
|
tableNames += table + ",";
|
|
}
|
|
//去掉最后的逗号
|
|
tableNames = tableNames.substring(0, tableNames.length() - 1);
|
|
|
|
//关闭数据库
|
|
conn.close();
|
|
arp.stop();
|
|
hpPlugin.stop();
|
|
|
|
System.out.println(dsKit.getCurrentTimeStr() + " 恭喜,表" + tableNames + "代码已成功生成!");
|
|
}
|
|
|
|
|
|
/**
|
|
* 将表名转换为驼峰命名法
|
|
*/
|
|
private static String toCamelCase(String tableName) {
|
|
StringBuilder sb = new StringBuilder();
|
|
boolean upperCase = false;
|
|
|
|
for (char c : tableName.toCharArray()) {
|
|
if (c == '_') {
|
|
upperCase = true;
|
|
} else if (upperCase) {
|
|
sb.append(Character.toUpperCase(c));
|
|
upperCase = false;
|
|
} else {
|
|
sb.append(c);
|
|
}
|
|
}
|
|
return sb.toString();
|
|
}
|
|
|
|
/**
|
|
* 功能:首字母转大写
|
|
*
|
|
* @param input
|
|
* @return
|
|
*/
|
|
public static String capitalizeFirstLetter(String input) {
|
|
// 检查输入是否为空或者null
|
|
if (input == null || input.isEmpty()) {
|
|
return input;
|
|
}
|
|
|
|
// 将第一个字符转换为大写,然后与其余部分拼接
|
|
return input.substring(0, 1).toUpperCase() + input.substring(1);
|
|
}
|
|
}
|