java读取实体类生成数据库表结构

 

1、创建数据库连接



import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DatabaseConnection {
private static final String URL = "jdbc:mysql://localhost:3306/movie?allowMultiQueries=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC";
private static final String USERNAME = "root";
private static final String PASSWORD = "pyrx123";

public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(URL, USERNAME, PASSWORD);
}
}

2、数据表生成工具


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.TableName;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

public class TableCreator {
public static void createTable(Connection connection, Class<?> clazz) throws SQLException {
StringBuilder sql = new StringBuilder("CREATE TABLE IF NOT EXISTS ");

TableName annotation = clazz.getAnnotation(TableName.class);
if (annotation == null || annotation.value()==null) {
sql.append(clazz.getSimpleName()).append(" (");
} else {
sql.append(annotation.value()).append(" (");
}
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
String fieldName = field.getName();
sql.append(StrUtil.toUnderlineCase(fieldName)).append(" ").append(getColumnType(field.getType())).append(", ");
}
sql.setLength(sql.length() - 2);
sql.append(")");
try (Statement statement = connection.createStatement()) {
statement.executeUpdate(sql.toString());
}
}

private static String getColumnType(Class<?> type) {
if (type == Integer.class || type == int.class) {
return "INT(11)";
} else if (type == String.class) {
return "VARCHAR(255)";
} else if (type == Date.class) {
return "DATE";
} else if (type == Float.class || type == float.class) {
return "FLOAT";
} else if (type == Double.class || type == double.class) {
return "DOUBLE";
} else if (type == Byte.class) {
return "TINYINT";
} else if (type == Long.class || type == long.class) {
return "BIGINT";
} else if (type == Short.class || type == short.class) {
return "SMALLINT";
} else if (type == Boolean.class || type == boolean.class) {
return "BOOLEAN";
} else if (type == Character.class || type == char.class) {
return "CHAR(1)";
} else {
return "VARCHAR(255)";
}
}
}

3、读取文件夹下的实体类并转成class


import java.io.File;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.SQLException;

public class CreateMain {
public static void main(String[] args) throws Exception {

try (Connection connection = DatabaseConnection.getConnection()) {
// 指定文件夹路径
File folder = new File("D:\\qyt\\2023-10-23\\springboot-movie\\api\\src\\main\\java\\com\\movie\\api\\model\\entity");
// 获取文件夹中的所有文件
File[] files = folder.listFiles();
String packagePath="com.movie.api.model.entity.";
// 遍历文件数组
for (File file : files) {
// 打印文件路径和名称
System.out.println(file.getAbsolutePath());
Class<?> clazz = Class.forName(packagePath+file.getName().replace(".java",""));
TableCreator.createTable(connection, clazz);
}
System.out.println("Table created successfully.");
} catch (SQLException e) {
e.printStackTrace();
}
}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值