使用hibernate生成表结构

时间:2022-05-02 10:40:10

准备工作

首先创建一个空的web项目,在其中添加hibernate的jar包
在src目录下创建hibernate配置文件:hibernate.cfg.xml
(如果使用intellij idea创建maven项目,src默认不是Source Folders,可以在file->project Structure->modules下右键src选sources然后apply即可)

<?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/test</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!--show_sql 用来在控制台输出控制语句-->
<property name="show_sql">true</property>
<!--format_sql 用来格式化输出的语句-->
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.current_session_context_class">thread</property>
</session-factory>
</hibernate-configuration>

创建实体类

在src文件夹下创建entity包,并在entity下创建Users.java和Students.java两个类
Users.java

package entity;

/**
* Created by mycomputer on 2017/5/29.
*/

public class Users {
private int uid;
private String username;
private String password;

public Users() {
}

public int getUid() {
return uid;
}

public void setUid(int uid) {
this.uid = uid;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

@Override
public String toString() {
return "Users{" +
"uid=" + uid +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}

Students.java

package entity;

import java.util.Date;

/**
* Created by mycomputer on 2017/5/29.
*/

public class Students {
private String sid;
private String sname;
private String gender;
private Date birthday;
private String address;

public Students() {
}

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 + '\'' +
'}';
}
}

然后创建两个类对应的配置文件
Users.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.Users" >
<id name="uid" type="int">
<generator class="native"/>
</id>
<property name="username" type="java.lang.String"/>
<property name="password" type="java.lang.String"/>

</class>
</hibernate-mapping>

Strudents.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" >
<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"/>
<property name="birthday" type="date"/>
<property name="address" type="java.lang.String"/>
</class>
</hibernate-mapping>

将Users.hbm.xml和Students.hbm.xml两个配置文件添加到主配置文件hibernate.cfg.xml中
在hibernate.cfg.xml的session-Factory标签中添加如下两行代码:

        <mapping resource="entity/Students.hbm.xml"/>
<mapping resource="entity/Users.hbm.xml"/>

生成表结构

创建一个单元测试类TestStudents.java
在单元测试类中添加测试方法public void testSchemaExport(){}
在测试类中使用SchemaExport创建表结构
完整代码如下:

package entity;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;

/**
* Created by mycomputer on 2017/5/29.
*/

public class TestStudents {
@Test
public void testSchemaExport(){
//创建配置对象
Configuration config = new Configuration().configure();
//创建服务注册对象
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
//创建sessionFactory
SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);
//第一个TRUE表示生成表结构
//第二个TRUE表示输出sql语句
export.create(true,true);
}
}

运行单元测试

在运行单元测试之前要先创建test数据库。然后运行测试单元即可;