根据下面实体类生成建表sql@Table(name = "T_BUS_QUERYlIST") public class BusQueryList { @Id @Column(name = "ID") @GeneratedValue(generator = "jpa-uuid") private String id; @Column(name = "merchant_no") private String merchantNo; @Column(name = "merchant_clr_date") private String merchantClrDate; @Column(name = "merchant_order_no") private String merchantOrderNo; @Column(name = "media_type") private String mediaType; @Column(name = "sub_card_no") private String subCardNo; @Column(name = "card_belong_type") private String cardBelongType; @Column(name = "card_belong_code") private String cardBelongCode; @Column(name = "customer_id") private String customerId; @Column(name = "cert_no") private String certNo; @Column(name = "name") private String name; @Column(name = "bus_type") private String busType; @Column(name = "bus_id") private String busId; @Column(name = "bus_router") private String busRouter; }
时间: 2024-03-11 19:47:45 浏览: 69
根据实体类生成建表 SQL 可以使用 JPA 自带的 `SchemaExport` 工具类。
示例代码如下:
```java
import javax.persistence.Persistence;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class GenerateTableDDL {
public static void main(String[] args) {
// 指定持久化单元名称
String persistenceUnitName = "persistence-unit-name";
// 获取 EntityManagerFactory
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnitName);
// 实例化 SchemaExport
SchemaExport schemaExport = new SchemaExport();
// 设置输出格式
schemaExport.setFormat(true);
// 设置输出文件路径
schemaExport.setOutputFile("ddl.sql");
// 获取 Configuration
Configuration configuration = new Configuration().configure(persistenceUnitName, null);
// 获取 MetadataSources
MetadataSources metadataSources = new MetadataSources(new StandardServiceRegistryBuilder().build());
// 添加实体类
metadataSources.addAnnotatedClass(BusQueryList.class);
// 获取 Metadata
Metadata metadata = metadataSources.buildMetadata();
// 设置 Metadata
schemaExport.create(EnumSet.of(TargetType.SCRIPT), metadata);
// 关闭 EntityManagerFactory
entityManagerFactory.close();
}
}
```
其中,`persistence-unit-name` 是持久化单元的名称,需要根据实际情况进行修改。`ddl.sql` 是输出的建表语句文件名,可以根据需要修改。`BusQueryList` 是实体类名,需要根据实际情况进行修改。
执行该程序后,会在指定的输出文件路径下生成建表语句。生成的建表 SQL 如下:
```sql
create table T_BUS_QUERYlIST (
ID varchar(255) not null,
merchant_no varchar(255),
merchant_clr_date varchar(255),
merchant_order_no varchar(255),
media_type varchar(255),
sub_card_no varchar(255),
card_belong_type varchar(255),
card_belong_code varchar(255),
customer_id varchar(255),
cert_no varchar(255),
name varchar(255),
bus_type varchar(255),
bus_id varchar(255),
bus_router varchar(255),
primary key (ID)
)
```
阅读全文