step1:首先创建 hibernate.cfg.xml文件,里面的代码就不多解释
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.username">root</property>
<property name="connection.password">1234</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///studentcourse</property>
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/bzu/entity/Adminer.hbm.xml"/>
<mapping resource="com/bzu/entity/Course.hbm.xml"/>
<mapping resource="com/bzu/entity/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
step2:保证bean、bean.hbm.xml 的完整性(这里不多介绍)
step3:编写 ExportDB 类(也就是一个执行方法)
package com.bzu.entity;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class ExportDB {
public static void main(String[] args) {
// 读取配置文件
Configuration cfg = new Configuration().configure();
// 创建SchemaExport对象
SchemaExport export = new SchemaExport(cfg);
// 创建数据库表
export.create(true, true);
}
}
step4:执行 main 方法即可自动创建表