1. 程式人生 > >04-使用SchemaExport生成表結構

04-使用SchemaExport生成表結構

Hibernate提供了根據實體類,建立表結構的API。

Hibernate版本:5.3.6

一、建立學生類物件

public class Students {

    private String sid;
    private String sname;
    private String gender;
    private Date birthday;
    private String address;

    public Students() {
    }

    public Students(String sid, String sname, String gender, Date birthday, String address) {
        this.sid = sid;
        this.sname = sname;
        this.gender = gender;
        this.birthday = birthday;
        this.address = address;
    }

    public String getSid() {
        return sid;
    }

    public void setSid(String sid) {
        this.sid = sid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Students{" +
                "sid='" + sid + '\'' +
                ", sname='" + sname + '\'' +
                ", gender='" + gender + '\'' +
                ", birthday=" + birthday +
                ", address='" + address + '\'' +
                '}';
    }
}

二、配置關係對映檔案Students.hbm.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

    <class name="entity.Students" table="STUDENTS">
        <id name="sid" type="java.lang.String" length="8">
            <!--手動賦值-->
            <generator class="assigned"/>
        </id>
        <property name="sname" type="java.lang.String"/>
        <property name="gender" type="java.lang.String"/>
        <!--不需要日期,這裡可以直接用 hibernate的 date型別-->
        <property name="birthday" type="date"/>
        <property name="address" type="java.lang.String"/>
    </class>
</hibernate-mapping>

三、配置Hibernate配置檔案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.url">jdbc:mysql://localhost:3306/BookDB?useSSL=false&amp;serverTimezone=UTC
        </property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.username">root</property>
        <property name="connection.password">12345678</property>

        <!-- 設定方言 -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>

        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>

        <property name="current_session_context_class">thread</property>

        <mapping resource="entity/Students.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

四、編寫並單元測試方法生成表結構

public class TestStudents {
    @Test
    public void testSchemaExport() {
        // 1.建立服務註冊物件
        ServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();
        // 2.生成Metadata
        Metadata metadata = new MetadataSources(registry).buildMetadata();
        // 3.生成表結構
        SchemaExport export = new SchemaExport();
        export.create(EnumSet.of(TargetType.DATABASE), metadata);
    }
}

使用的configure()方法來獲取配置檔案的內容,如果不寫引數,表示預設獲取的是hibernate.cfg.xml,配置檔案的名字是不能改的,如果改掉之後,就應該在configure()方法中傳入配置檔名字,有必要的話還要傳入路徑。