代码生成器
- 通过数据库连接对象获取所有的表信息
- 将全部表信息响应给前端
- 搭建前端界面,展示表结构,用户可以编辑数据的类型或者添加注解
- 前端配置生成的信息,传送给后端,后端渲染模板,最后前端可以下载 zip
- 前端生成代码预览效果
- 欢迎提供其他建议
获取表信息的主要代码如下:
package com.example.generate.service;
import com.example.generate.model.Column;
import com.example.generate.model.Db;
import com.example.generate.model.Table;
import com.google.common.base.CaseFormat;
import lombok.SneakyThrows;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.springframework.stereotype.Service;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
@Service
public class GenerateService {
private final ThreadLocal<Connection> connection = new ThreadLocal<>();
@SneakyThrows
public List<Table> getDbInfo(Db db){
connection.set(DriverManager.getConnection(db.getUrl(), db.getUsername(), db.getPassword()));
return getGenerateInfo();
}
@SneakyThrows
private List<Table> getTableInfo(String tableName){
List<Table> tableInfoList = new ArrayList<>();
DatabaseMetaData metaData = connection.get().getMetaData();
ResultSet tables = metaData.getTables(connection.get().getCatalog(),null,tableName,new String[]{"TABLE"});
while (tables.next()) {
Table table = new Table();
String name = tables.getString("table_name");
String remark = tables.getString("REMARKS");
table.setTableName(name);
table.setRemark(remark);
ResultSet primaryKeys = metaData.getPrimaryKeys(null, null, name);
while (primaryKeys.next()){
String primaryKey = primaryKeys.getString("COLUMN_NAME");
table.setPrimaryKey(primaryKey);
}
tableInfoList.add(table);
}
return tableInfoList;
}
@SneakyThrows
private List<Column> getColumnInfo(String tableName){
List<Column> columnListInfo = new ArrayList<>();
DatabaseMetaData metaData = connection.get().getMetaData();
ResultSet columns = metaData.getColumns(connection.get().getCatalog(), null, tableName, null);
while (columns.next()) {
Column column = new Column();
String columnName = columns.getString("COLUMN_NAME");
String remark = columns.getString("REMARKS");
String typeName = columns.getString("TYPE_NAME");
column.setColName(columnName);
column.setRemark(remark);
column.setColType(typeName);
columnListInfo.add(column);
}
return columnListInfo;
}
private void setColumnInfo(Table table){
table.setColumns(getColumnInfo(table.getTableName()));
}
private void setColumnInfo(Table table,Boolean handelColumn){
setColumnInfo(table);
if (handelColumn){
table.getColumns().forEach(this::handleColumn);
}
}
@SneakyThrows
private void handleColumn(Column column){
PropertiesConfiguration propertiesConfiguration = new PropertiesConfiguration("generator.properties");
// 将JDBC 类型转化为 Java 类型
String javaTypeName = propertiesConfiguration.getString(column.getColType().toLowerCase(), column.getColType());
column.setJavaType(javaTypeName);
column.setJavaName(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL,column.getColName()));
}
@SneakyThrows
private void handleTable(Table table){
table.setJavaName(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, table.getTableName()));
}
@SneakyThrows
private List<Table> getGenerateInfo(){
List<Table> tables = getTableInfo(null);
tables.forEach(this::handleTable);
for (Table table : tables) {
setColumnInfo(table,true);
}
connection.remove();
return tables;
}
}