1、在IDEA中建立名为ormlab1的Java工程,并导入相关类包(*.jar)
2、在src目录下创建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/test1</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.MySQL5InnoDBDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/entity/TUserEntity.hbm.xml"/>
<!-- DB schema will be updated if needed -->
<!-- <property name="hbm2ddl.auto">update</property> -->
</session-factory>
</hibernate-configuration>
3、建立数据库
4、创建实体类(User.java)和对应的对象-关系映射文件(User.hbm.xml),并在hibernate.cfg.xml中配置标签;
(1)User.java实体类
package com.mypack;
import java.util.Objects;
public class User {
private int ID;
private String user_name;
private String user_age;
private String user_gender;
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public String getUser_age() {
return user_age;
}
public void setUser_age(String user_age) {
this.user_age = user_age;
}
public String getUser_gender() {
return user_gender;
}
public void setUser_gender(String user_gender) {
this.user_gender = user_gender;
}
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
User that = (User) o;
return ID == that.ID &&
Objects.equals(user_name, that.user_name) &&
Objects.equals(user_age, that.user_age)&&
Objects.equals(user_gender, that.user_gender);
}
public int hashCode() {
return Objects.hash(ID, user_name, user_age, user_gender);
}
}
(2)映射
<?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="com.entity.TUserEntity" table="t_user" schema="hibernate">
<id name="ID">
<column name="ID" sql-type="int(11)"/>
<generator class="identity"/>
</id>
<property name="username">
<column name="user_name" sql-type="varchar(20)" length="20" not-null="true"/>
</property>
<property name="age">
<column name="user_age" sql-type="varchar(20)" length="20" not-null="true"/>
</property>
<property name="gender">
<column name="user_gender" sql-type="varchar(20)" length="20" not-null="true"/>
</property>
</class>
</hibernate-mapping>
5、测试类
package test;
import com.mypack.User;
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import org.junit.*;
public class mainTest {
public void test() {
System.out.println("test---------");
//创建配置对象(读取配置文档)
Configuration config = new Configuration().configure();
//创建会话工厂对象
SessionFactory sessionFactory = config.buildSessionFactory();
//会话对象
Session session = sessionFactory.openSession();
//开启事务
Transaction transaction = session.beginTransaction();
User user = new User();
user.setUser_name("rbx");
user.setUser_age("21");
user.setUser_gender("male");
session.save(user);
//提交事务
transaction.commit();
//关闭事务
session.close();
sessionFactory.close();
}
}
运行测试类:
insert
into
hibernate.t_user
(username, age, gender)
values
(?, ?, ?)
多次插入后的数据库:
多次插入都成功插入数据,同时ID自动增加。