本程序简单实现了数据库内省,生成PO对象。
数据库内省有如下两种实现方式:
- 通过mysql元表
- 通过desc table,show tables等命令
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class Generator {
class Table {
String name;
List<String> fieldList = new ArrayList<>();
List<String> typeList = new ArrayList<>();
}
final static String database = "valley";
final static String url = "jdbc:mysql://localhost:3306/" + database + "?serverTimezone=Asia/Shanghai";
final static String user = "root", password = "123";
final static String targetFolder = "src/main/java/valley/po";
final static String packageName="valley.po";
final static String tab = " ";
final static List<Table> tables = new ArrayList<>();
String convertType(String sqlType) {
if (sqlType.startsWith("varchar")) return "String";
if (sqlType.startsWith("bigint")) return "Long";
if (sqlType.startsWith("int")) return "Integer";
if (sqlType.startsWith("smallint")) return "Short";
if (sqlType.startsWith("tinyint")) return "Byte";
if(sqlType.startsWith("double"))return "Double";
return null;
}
String convertField(String field) {
return field;
}
//表名中只允许字母和下划线,并且下划线不能连续两道
String convertTableName(String name) {
StringBuilder builder = new StringBuilder();
if (name.charAt(0) != '_') builder.append(Character.toUpperCase(name.charAt(0)));
for (int i = 1; i < name.length(); i++) {
if (name.charAt(i) == '_') continue;
if (name.charAt(i - 1) == '_') {
builder.append(Character.toUpperCase(name.charAt(i)));
} else {
builder.append(name.charAt(i));
}
}
builder.append("Po");
return builder.toString();
}
String uppercaseFirstLetter(String s) {
return Character.toUpperCase(s.charAt(0)) + s.substring(1);
}
void writeTableToFile(Table table, PrintWriter writer) {
StringBuilder fields = new StringBuilder();
StringBuilder gettersAndSetters = new StringBuilder();
for (int i = 0; i < table.fieldList.size(); i++) {
String field = table.fieldList.get(i), type = table.typeList.get(i);
fields.append(String.format(" %s %s;
", type, field));
String uppercase = uppercaseFirstLetter(field);
gettersAndSetters.append(String.format(" %s get%s(){
return %s;
}
", type, uppercase, field));
gettersAndSetters.append(String.format(" void set%s(%s a%s){
%s=a%s;
}
", uppercase, type, uppercase, field, uppercase));
}
String ans = String.format("package %s;
class %s{
%s
%s
}",packageName, table.name, fields, gettersAndSetters);
ans = ans.replace(" ", tab);
writer.printf(ans);
writer.close();
}
void output() throws IOException {
Path folder = Paths.get(targetFolder);
if (Files.exists(folder) == false) {
Files.createDirectory(folder);
}
for (Table i : tables) {
Path file = folder.resolve(i.name + ".java");
writeTableToFile(i, new PrintWriter(Files.newBufferedWriter(file)));
}
}
void init() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection(url, user, password);
Statement showTablesStatement = connection.createStatement();
ResultSet showTablesResultSet = showTablesStatement.executeQuery("show tables");
while (showTablesResultSet.next()) {
Table t = new Table();
t.name = convertTableName(showTablesResultSet.getString(1));
Statement descTableStatement = connection.createStatement();
ResultSet descTableResult = descTableStatement.executeQuery("desc " + showTablesResultSet.getString(1));
while (descTableResult.next()) {
t.fieldList.add(convertField(descTableResult.getString(1)));
t.typeList.add(convertType(descTableResult.getString(2)));
}
tables.add(t);
descTableResult.close();
descTableStatement.close();
}
showTablesResultSet.close();
showTablesStatement.close();
connection.close();
}
Generator() throws Exception {
init();
output();
}
public static void main(String[] args) throws Exception {
new Generator();
}
}