feat: 增加DDL语句生成的小工具
This commit is contained in:
7
pom.xml
7
pom.xml
@@ -43,6 +43,13 @@
|
|||||||
<version>${querydsl.version}</version>
|
<version>${querydsl.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hibernate.orm</groupId>
|
||||||
|
<artifactId>hibernate-ant</artifactId>
|
||||||
|
<version>6.6.15.Final</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.mapstruct</groupId>
|
<groupId>org.mapstruct</groupId>
|
||||||
<artifactId>mapstruct</artifactId>
|
<artifactId>mapstruct</artifactId>
|
||||||
|
|||||||
@@ -0,0 +1,116 @@
|
|||||||
|
package com.lanyuanxiaoyao.service.template.util;
|
||||||
|
|
||||||
|
import jakarta.persistence.Entity;
|
||||||
|
import java.util.EnumSet;
|
||||||
|
import java.util.List;
|
||||||
|
import org.hibernate.boot.MetadataSources;
|
||||||
|
import org.hibernate.boot.model.naming.CamelCaseToUnderscoresNamingStrategy;
|
||||||
|
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||||
|
import org.hibernate.tool.hbm2ddl.SchemaExport;
|
||||||
|
import org.hibernate.tool.schema.TargetType;
|
||||||
|
import org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy;
|
||||||
|
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
|
||||||
|
import org.springframework.core.type.filter.AnnotationTypeFilter;
|
||||||
|
import org.springframework.util.ClassUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构造DDL建表语句
|
||||||
|
* <p>
|
||||||
|
* 该工具类用于生成数据库表结构的DDL语句,通过扫描指定包下的实体类,
|
||||||
|
* 利用Hibernate的SchemaExport工具生成建表SQL脚本。
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* 使用示例:
|
||||||
|
* <pre>
|
||||||
|
* DDLGenerator.generateDDL(
|
||||||
|
* List.of("com.example.entity", "com.another.package"),
|
||||||
|
* "./sql",
|
||||||
|
* MySQL8Dialect.class,
|
||||||
|
* "jdbc:mysql://localhost:3306/test",
|
||||||
|
* "username",
|
||||||
|
* "password",
|
||||||
|
* com.mysql.cj.jdbc.Driver.class
|
||||||
|
* );
|
||||||
|
* </pre>
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @author lanyuanxiaoyao
|
||||||
|
*/
|
||||||
|
public class DDLGenerator {
|
||||||
|
public static void generateDDL(
|
||||||
|
List<String> entityPackages,
|
||||||
|
String ddlFilePath,
|
||||||
|
Class<?> dialect,
|
||||||
|
String jdbc,
|
||||||
|
String username,
|
||||||
|
String password,
|
||||||
|
Class<?> driver
|
||||||
|
) {
|
||||||
|
var metadataSources = new MetadataSources(
|
||||||
|
new StandardServiceRegistryBuilder()
|
||||||
|
.applySetting("hibernate.dialect", dialect.getName())
|
||||||
|
.applySetting("hibernate.physical_naming_strategy", CamelCaseToUnderscoresNamingStrategy.class.getName())
|
||||||
|
.applySetting("hibernate.implicit_naming_strategy", SpringImplicitNamingStrategy.class.getName())
|
||||||
|
.applySetting("hibernate.connection.url", jdbc)
|
||||||
|
.applySetting("hibernate.connection.username", username)
|
||||||
|
.applySetting("hibernate.connection.password", password)
|
||||||
|
.applySetting("hibernate.connection.driver_class", driver.getName())
|
||||||
|
.build()
|
||||||
|
);
|
||||||
|
|
||||||
|
// 使用Spring类路径扫描方式查找所有@Entity注解的类
|
||||||
|
var scanner = new ClassPathScanningCandidateComponentProvider(false);
|
||||||
|
scanner.addIncludeFilter(new AnnotationTypeFilter(Entity.class));
|
||||||
|
|
||||||
|
// 遍历所有包路径,扫描@Entity注解的类
|
||||||
|
for (String entityPackage : entityPackages) {
|
||||||
|
var candidates = scanner.findCandidateComponents(entityPackage);
|
||||||
|
|
||||||
|
// 将找到的实体类添加到metadataSources中
|
||||||
|
for (var candidate : candidates) {
|
||||||
|
// 处理candidate或getBeanClassName可能为null的情况
|
||||||
|
if (candidate == null || candidate.getBeanClassName() == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
var entityClass = ClassUtils.forName(candidate.getBeanClassName(), DDLGenerator.class.getClassLoader());
|
||||||
|
metadataSources.addAnnotatedClass(entityClass);
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
throw new RuntimeException("Failed to load entity class: " + candidate.getBeanClassName(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var export = new SchemaExport();
|
||||||
|
export.setFormat(true);
|
||||||
|
export.setDelimiter(";");
|
||||||
|
export.setOutputFile(ddlFilePath + "/" + dialect.getSimpleName() + ".sql");
|
||||||
|
export.setOverrideOutputFileContent();
|
||||||
|
export.execute(EnumSet.of(TargetType.SCRIPT), SchemaExport.Action.CREATE, metadataSources.buildMetadata());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 兼容旧版本的方法签名
|
||||||
|
*
|
||||||
|
* @param entityPackage 实体类包路径
|
||||||
|
* @param ddlFilePath DDL文件输出路径
|
||||||
|
* @param dialect 方言类
|
||||||
|
* @param jdbc JDBC连接URL
|
||||||
|
* @param username 数据库用户名
|
||||||
|
* @param password 数据库密码
|
||||||
|
* @param driver JDBC驱动类
|
||||||
|
*/
|
||||||
|
public static void generateDDL(
|
||||||
|
String entityPackage,
|
||||||
|
String ddlFilePath,
|
||||||
|
Class<?> dialect,
|
||||||
|
String jdbc,
|
||||||
|
String username,
|
||||||
|
String password,
|
||||||
|
Class<?> driver
|
||||||
|
) {
|
||||||
|
generateDDL(List.of(entityPackage), ddlFilePath, dialect, jdbc, username, password, driver);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user