版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lucky9322/article/details/83621421
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&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()方法中传入配置文件名字,有必要的话还要传入路径。