- package com.angen.util;
-
- import java.io.FileWriter;
- import java.io.IOException;
- import java.sql.*;
- import java.util.ArrayList;
- import java.util.List;
-
- /**
- * @description: PojoUtil
- * @date: 2022/7/20
- */
- public class PojoUtil {
- public static void main(String[] args) throws ClassNotFoundException, SQLException {
- String url = "jdbc:mysql://localhost:3306/test_mybatis?useSSL=false";
- String user = "root";
- String pass = "root";
- Class.forName("com.mysql.jdbc.Driver");
- Connection conn = DriverManager.getConnection(url, user, pass);
- //java文件输出路径
- String path = "E:\\java重新学习\\mybatis_test\\src\\main\\java\\com\\angen\\pojo";
- GetModel getModel = new GetModel(conn);
- /*
- 使用:
- pojo: 包含属性与getter、setter
- bean: 包含属性与getter、setter与无参构造器,并实现Serializable接口
- */
- //生成pojo到path
- //getModel.generatePojo(path);
- //生成pojo到path,在pojo中添加包信息"package com.ren.model"
- //getModel.generatePojo(path, "com.ren.model");
- //生成Bean到path
- //getModel.generateBean(path);
- //生成Bean到path,在Bean中添加包信息"package com.ren.bean"
- //getModel.generateBean(path, "com.ren.bean");
- getModel.generateBean(path);
- }
- }
- class GetModel {
- private Connection connection;
- private List
tables;-
- public GetModel() {
- }
-
- public GetModel(Connection connection) throws SQLException {
- this.connection = connection;
- this.tables = getTableList();
- }
-
- public Connection getConnection() {
- return connection;
- }
-
- public void setConnection(Connection connection) {
- this.connection = connection;
- }
-
- /**
- * 获取数据库中所有表信息封装到List
tables- * @return List
包含数据库中所有的表- * @throws SQLException
- */
- private List
getTableList() throws SQLException {- List
tableList = new ArrayList<>();- DatabaseMetaData dbMetaData = connection.getMetaData();
- ResultSet rs = dbMetaData.getTables(null, null, null, new String[] { "TABLE" });
- while (rs.next()) {
- Table table = new Table();
- String tableName = rs.getString("TABLE_NAME");
- table.setTableName(tableName);
- table.setDatabaseName(rs.getString("TABLE_CAT"));
- table.setColumns(getTableColumnList(tableName));
- tableList.add(table);
- }
- return tableList;
- }
-
- /**
- * 获取某表的所有column信息封装到List
columnList中 - */
- private List
getTableColumnList(String tableName) throws SQLException { - List
columnList = new ArrayList<>(); - String sql = "select * from " + tableName;
- PreparedStatement stmt;
- try {
- stmt = connection.prepareStatement(sql);
- ResultSet rs = stmt.executeQuery(sql);
- ResultSetMetaData data = rs.getMetaData();
- for (int i = 1; i <= data.getColumnCount(); i++) {
- Column column = new Column();
- // 获得所有列的数目及实际列数
- int columnCount = data.getColumnCount();
- // 获得指定列的列名
- String columnName = data.getColumnName(i);
- // 获得指定列的数据类型名
- String columnTypeName = data.getColumnTypeName(i);
- // 对应数据类型的类
- String columnClassName = data.getColumnClassName(i);
- // 在数据库中类型的最大字符个数
- int columnDisplaySize = data.getColumnDisplaySize(i);
- // 某列类型的精确度(类型的长度)
- int precision = data.getPrecision(i);
- // 小数点后的位数
- int scale = data.getScale(i);
- // 是否自动递增
- Boolean isAutoInctement = data.isAutoIncrement(i);
- // 是否为空
- int isNullable = data.isNullable(i);
- column.setColumnName(columnName);
- column.setColumnTypeName(columnTypeName);
- column.setColumnClassName(columnClassName);
- column.setTableName(tableName);
- column.setColumnDisplaySize(columnDisplaySize);
- column.setPrecision(precision);
- column.setScale(scale);
- column.setAutoInctement(isAutoInctement);
- columnList.add(column);
- }
- }
- catch (SQLException e) {
- e.printStackTrace();
- }
- return columnList;
- }
-
- /**
- * 为table创建pojo
- * @param table 表
- * @param path 输出路径
- */
- private static void writeFilePojo(Table table, String path, String packagePath) {
- String fileName = path + "\\" + upperCase(table.getTableName()) + ".java";
- try {
- FileWriter writer = new FileWriter(fileName);
- StringBuilder getterSetter = new StringBuilder();
- //需要添加包路径
- if (!packagePath.equals("0")) {
- writer.write("package " + packagePath + ";\n\n");
- }
- writer.write("public class " + upperCase(table.getTableName()) + " {\n");
- for (Column column : table.getColumns()) {
- writer.write(" private " + changeType(column.getColumnTypeName()) + " " + column.getColumnName() + ";\n");
- //getter()
- getterSetter.append("\n public " + changeType(column.getColumnTypeName()) + " get" + upperCase(column.getColumnName()) + "() {\n");
- getterSetter.append(" return " + column.getColumnName() + ";\n");
- getterSetter.append(" }\n\n");
- //setter()
- getterSetter.append(" public void set" + upperCase(column.getColumnName()) + "(" + changeType(column.getColumnTypeName()) + " " + column.getColumnName() + ") {\n");
- getterSetter.append(" this." + column.getColumnName() + " = " + column.getColumnName() + ";\n");
- getterSetter.append(" }\n");
- }
- writer.write(getterSetter.toString());
- writer.write("}");
- writer.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- /**
- * 为table创建Bean
- * @param table 表
- * @param path 输出路径
- */
- private static void writeFileBean(Table table, String path, String packagePath) {
- String fileName = path + "\\" + upperCase(table.getTableName()) + ".java";
- try {
- FileWriter writer = new FileWriter(fileName);
- StringBuilder getterSetter = new StringBuilder();
- //需要添加包路径
- if (!packagePath.equals("0")) {
- writer.write("package " + packagePath + ";\n\n");
- }
- writer.write("import java.io.Serializable;\n\n");
- writer.write("public class " + upperCase(table.getTableName()) + " implements Serializable {\n");
- for (Column column : table.getColumns()) {
- writer.write(" private " + changeType(column.getColumnTypeName()) + " " + column.getColumnName() + ";\n");
- //getter()
- getterSetter.append("\n public " + changeType(column.getColumnTypeName()) + " get" + upperCase(column.getColumnName()) + "() {\n");
- getterSetter.append(" return " + column.getColumnName() + ";\n");
- getterSetter.append(" }\n\n");
- //setter()
- getterSetter.append(" public void set" + upperCase(column.getColumnName()) + "(" + changeType(column.getColumnTypeName()) + " " + column.getColumnName() + ") {\n");
- getterSetter.append(" this." + column.getColumnName() + " = " + column.getColumnName() + ";\n");
- getterSetter.append(" }\n");
- }
- //无参构造器
- writer.write("\n public " + upperCase(table.getTableName()) + "() { }\n");
- writer.write(getterSetter.toString());
- writer.write("}");
- writer.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- /**
- * 将 str 首字母大写
- * @param str
- * @return
- */
- private static String upperCase(String str) {
- return str.substring(0, 1).toUpperCase() + str.substring(1);
- }
-
- /**
- * 将数据库类型转换为java中合适的类型
- * @param typeName
- * @return
- */
- private static String changeType(String typeName) {
- switch (typeName) {
- case "VARCHAR":
- case "CHAR":
- case "TEXT":
- case "TINYTEXT":
- case "MEDIUMTEXT":
- case "LONGTEXT":
- case "ENUM":
- case "SET":
- return "String";
- case "BLOB":
- case "BINARY":
- case "VARBINARY":
- case "TINYBLOB":
- case "MEDIUMBLOB":
- case "LONGBLOB":
- return "byte[]";
- case "INTEGER":
- case "ID":
- case "BIGINT":
- return "Long";
- case "TINYINT":
- case "SMALLINT":
- case "MEDIUMINT":
- case "INT":
- return "Integer";
- case "DECIMAL":
- return "BigDecimal";
- case "BIT":
- return "Boolean";
- case "FLOAT":
- return "Float";
- case "DOUBLE":
- return "Double";
- case "DATE":
- case "YEAR":
- return "Date";
- case "TIME":
- return "Time";
- }
- return "String";
- }
-
- /**
- * 根据tables生成pojo到path
- * @param path 输出路径
- */
- public void generatePojo(String path) {
- for (Table table : tables) {
- writeFilePojo(table, path, "0");
- System.out.println("已创建Pojo: " + path + "\\" + upperCase(table.getTableName()) + ".java");
- }
- }
-
- /**
- * 根据tables生成Bean到path
- * @param path 输出路径
- */
- public void generateBean(String path) {
- for (Table table : tables) {
- writeFileBean(table, path, "0");
- System.out.println("已创建Bean: " + path + "\\" + upperCase(table.getTableName()) + ".java");
- }
- }
-
- /**
- * 根据tables生成pojo到path
- * @param path 输出路径
- * @param packagePath 添加包路径
- */
- public void generatePojo(String path, String packagePath) {
- for (Table table : tables) {
- writeFilePojo(table, path, packagePath);
- System.out.println("已创建Pojo: " + path + "\\" + upperCase(table.getTableName()) + ".java 包名:" + packagePath);
- }
- }
-
- /**
- * 根据tables生成Bean到path
- * @param path 输出路径
- * @param packagePath 添加包路径
- */
- public void generateBean(String path, String packagePath) {
- for (Table table : tables) {
- writeFileBean(table, path, packagePath);
- System.out.println("已创建Bean: " + path + "\\" + upperCase(table.getTableName()) + ".java 包名:" + packagePath);
- }
- }
- }
- class Table {
- private String tableName;
- private String databaseName;
- private List
columns; -
- public String getTableName() {
- return tableName;
- }
-
- public void setTableName(String tableName) {
- this.tableName = tableName;
- }
-
- public String getDatabaseName() {
- return databaseName;
- }
-
- public void setDatabaseName(String databaseName) {
- this.databaseName = databaseName;
- }
-
- public List
getColumns() { - return columns;
- }
-
- public void setColumns(List
columns) { - this.columns = columns;
- }
-
- @Override
- public String toString() {
- return "Table{" +
- "\n tableName='" + tableName + '\'' +
- "\n databaseName='" + databaseName + '\'' +
- "\n columns=" + columns +
- '}';
- }
- }
- class Column {
- // 列名
- private String columnName;
- // 列的数据类型名
- private String columnTypeName;
- // 对应数据类型的类
- private String columnClassName;
- // 在数据库中类型的最大字符个数
- private int columnDisplaySize;
- // 某列类型的精确度(类型的长度)
- private int precision;
- // 小数点后的位数
- private int scale;
- // 获取某列对应的表名
- private String tableName;
- // 是否自动递增
- private Boolean isAutoInctement;
-
- public String getColumnName() {
- return columnName;
- }
-
- public void setColumnName(String columnName) {
- this.columnName = columnName;
- }
-
- public String getColumnTypeName() {
- return columnTypeName;
- }
-
- public void setColumnTypeName(String columnTypeName) {
- this.columnTypeName = columnTypeName;
- }
-
- public String getColumnClassName() {
- return columnClassName;
- }
-
- public void setColumnClassName(String columnClassName) {
- this.columnClassName = columnClassName;
- }
-
- public int getColumnDisplaySize() {
- return columnDisplaySize;
- }
-
- public void setColumnDisplaySize(int columnDisplaySize) {
- this.columnDisplaySize = columnDisplaySize;
- }
-
- public int getPrecision() {
- return precision;
- }
-
- public void setPrecision(int precision) {
- this.precision = precision;
- }
-
- public int getScale() {
- return scale;
- }
-
- public void setScale(int scale) {
- this.scale = scale;
- }
-
- public String getTableName() {
- return tableName;
- }
-
- public void setTableName(String tableName) {
- this.tableName = tableName;
- }
-
- public Boolean getAutoInctement() {
- return isAutoInctement;
- }
-
- public void setAutoInctement(Boolean autoInctement) {
- isAutoInctement = autoInctement;
- }
-
- @Override
- public String toString() {
- return "\n Column{" +
- "columnName='" + columnName + '\'' +
- ", columnTypeName='" + columnTypeName + '\'' +
- ", columnClassName='" + columnClassName + '\'' +
- ", columnDisplaySize=" + columnDisplaySize +
- ", precision=" + precision +
- ", scale=" + scale +
- ", tableName='" + tableName + '\'' +
- ", isAutoInctement=" + isAutoInctement +
- '}';
- }
- }
-
相关阅读:
天谋科技 Timecho 完成近亿元人民币天使轮融资,打造工业物联网原生时序数据库
个人开发者轻松接入支付回调
软件测试面试题:黑盒测试、白盒测试以及单元测试、集成测试、系统测试、验收测试的区别与联系?
Kafka核心概念详解
java代码审计-某酒店管理系统
Day 62 数据结构(单向链表,单向循环链表,双向链表)
软考高级系统架构设计师系列之:深入理解设计模式
Java实现-跳跃游戏
磁环选型攻略及EMC整改技巧
STM32串口详解
-
原文地址:https://blog.csdn.net/qq_42572322/article/details/125897514