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();
}
}
}