#2025上海国际车展#
package com.alatus.builder;
import com.alatus.Entity.TableInfo;
import com.alatus.constant.Constants;
import com.alatus.utils.PropertiesUtils;
import com.alatus.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class TableBuilder {
private static Connection CONNECTION = null;
private static Logger LOGGER = LoggerFactory.getLogger(TableBuilder.class);
private static String SHOW_TABLE_STATUS = "show table status";
static{
String driverName = PropertiesUtils.getValue("db.driver.name");
String url = PropertiesUtils.getValue("db.url");
String username = PropertiesUtils.getValue("db.username");
String password = PropertiesUtils.getValue("db.password");
try {
Class.forName(driverName);
CONNECTION = DriverManager.getConnection(url, username, password);
LOGGER.info("数据库连接成功");
} catch (ClassNotFoundException e) {
LOGGER.error("数据库驱动加载失败");
throw new RuntimeException(e);
} catch (SQLException e) {
LOGGER.error("数据库连接失败");
throw new RuntimeException(e);
}
}
public static void getTables(){
PreparedStatement preparedStatement = null;
ResultSet results = null;
List<TableInfo> tableInfoList = new ArrayList<>();
try{
preparedStatement = CONNECTION.prepareStatement(SHOW_TABLE_STATUS);
results = preparedStatement.executeQuery();
while(results.next()){
String tableName = results.getString("name");
String comment = results.getString("comment");
LOGGER.info("表名:{},注释:{}",tableName,comment);
TableInfo tableInfo = new TableInfo();
tableInfo.setTableName(tableName);
tableInfo.setComment(comment);
String beanName = tableName;
if(Constants.IGNORE_TABLE_PREFIX){
beanName = tableName.substring(tableName.indexOf("_")+1);
}
beanName = processFields(beanName,true);
LOGGER.info("bean名称:{}",beanName);
}
}
catch (SQLException e){
LOGGER.error("获取表失败");
}
finally {
LOGGER.info("关闭数据库连接");
if(preparedStatement!=null){
try {
preparedStatement.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if(results!=null){
try {
results.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (CONNECTION != null){
try {
CONNECTION.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
}
private static String processFields(String fieldName,Boolean upperCase){
StringBuilder builder = new StringBuilder();
String[] fields = fieldName.split("_");
builder.append(upperCase? StringUtils.firstCharToUpperCase(fields[0]):fields[0]);
for (int i = 1; i < fields.length; i++) {
builder.append(StringUtils.firstCharToUpperCase(fields[i]));
}
return builder.toString();
}
}
package com.alatus.constant;
import com.alatus.utils.PropertiesUtils;
public class Constants {
public static Boolean IGNORE_TABLE_PREFIX;
static{
IGNORE_TABLE_PREFIX = Boolean.valueOf(PropertiesUtils.getValue("ignore.table.prefix"));
}
}
package com.alatus.utils;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
public class PropertiesUtils {
private static Properties properties = new Properties();
private static Map<String, String> propertiesMap = new ConcurrentHashMap<>();
static {
InputStream inputStream = null;
try{
// 典中典资源加载
inputStream = PropertiesUtils.class.getClassLoader().getResourceAsStream("application.properties");
properties.load(inputStream);
// 我想起来了,这个Properties对象是JAVA提供的特殊Map结构
Iterator<Object> iterator = properties.keySet().iterator();
// 所以也是自带KV结构的,因此我只要遍历它的KeySet,取出每一个Key,再把对应的Key和对应的Value放入Map中
// 就可以实现完整的加载properties中的数据了
while(iterator.hasNext()){
String key = (String) iterator.next();
propertiesMap.put(key,properties.getProperty(key));
}
}catch (Exception e){
e.printStackTrace();
}finally {
if(inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
public static String getValue(String key){
// 因为我们的配置信息是静态读取的,所以直接返回Map中的值即可
return propertiesMap.get(key);
}
}
package com.alatus.utils;
public class StringUtils {
/**
* 首字母大写
* @return string
*/
public static String firstCharToUpperCase(String word) {
if(org.apache.commons.lang3.StringUtils.isEmpty(word)){
return word;
}
return word.substring(0,1).toUpperCase()+word.substring(1);
}
/**
* 首字母小写
* @return string
*/
public static String firstCharToLowerCase(String str) {
if(org.apache.commons.lang3.StringUtils.isEmpty(str)){
return str;
}
return str.substring(0,1).toLowerCase()+str.substring(1);
}
}
package com.alatus.Entity;
import lombok.Data;
@Data
public class FieldInfo {
/**
* 字段名
*/
private String fieldName;
/**
* 属性名称
*/
private String propertyName;
/**
* sql字段类型
*/
private String sqlType;
/**
* 字段类型
*/
private String javaType;
/**
* 字段注释
*/
private String comment;
/**
* 是否自增
*/
private Boolean isAutoIncrement;
}
package com.alatus.Entity;
import lombok.Data;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@Data
public class TableInfo {
/**
* 表注释
*/
private String comment;
/**
* 字段信息
*/
private List<FieldInfo> fieldList;
/**
* 主键索引
*/
private Map<String, List<FieldInfo>> keyIndexMap = new LinkedHashMap<>();
/**
* 是否包含日期字段
*/
private Boolean haveDate;
/**
* 是否包含时间字段
*/
private Boolean haveDateTime;
/**
* 是否包含BigDecimal字段
*/
private Boolean haveBigDecimal;
/**
* 表名
*/
private String tableName;
/**
* Bean名
*/
private String beanName;
/**
* Bean参数名
*/
private String beanParamName;
}
package com.alatus;
import com.alatus.builder.TableBuilder;
public class GeneratorApplication {
public static void main(String[] args) {
TableBuilder.getTables();
}
}
db.driver.name=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/ourapp?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
db.username=root
db.password=abc123
ignore.table.prefix=true
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.alatus</groupId>
<artifactId>CodeGenerator</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<mysql.version>5.1.5</mysql.version>
<apache.commons.version>3.4</apache.commons.version>
<logback.version>1.2.10</logback.version>
<slf4j.version>1.7.7</slf4j.version>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${apache.commons.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>