1、工程目录结构如下
2、引入需要的jar包,如上图。
3、创建持久化类User对应数据库中的user表
package com.hibernate.配置文件.pojo; import java.sql.Date; public class User {
private Integer id; private String username; private String password; private Date update_time; public User() {
super();
} public User( String username, String password, Date update_time) {
super();
this.username = username;
this.password = password;
this.update_time = update_time;
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} 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;
} public Date getUpdate_time() {
return update_time;
} public void setUpdate_time(Date update_time) {
this.update_time = update_time;
} }
4、创建持久化类对应的配置文件User.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.hibernate.配置文件.pojo">
<class name="User" table="user" dynamic-insert="true">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<!-- 指定主键的生成方式, native: 使用数据库本地方式 -->
<generator class="native" />
</id> <property name="username" type="java.lang.String" column="username"/>
<property name="password" type="java.lang.String" column="password"/>
<property name="update_time" type="date">
<column name="update_time" />
</property>
</class>
</hibernate-mapping>
5、创建hibernate的配置文件hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 配置连接数据库的基本信息 -->
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://127.0.0.1/hibernate</property> <!-- 配置hibernate的基本信息 -->
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<!-- 执行操作时是否在控制台打印 SQL -->
<property name="show_sql">true</property> <!-- 是否对 SQL 进行格式化 -->
<property name="format_sql">true</property> <!-- 指定自动生成数据表的策略 -->
<property name="hbm2ddl.auto">update</property> <!-- 指定关联的.hbm.xml文件 -->
<mapping resource="com/hibernate/配置文件/pojo/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
6、创建SessionFactoryUtil,单例模式,用于生成SessionFactory对象
package com.hibernate.配置文件.util; import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder; public class SessionFactoryUtil {
private SessionFactoryUtil(){}
private static SessionFactory sessionFactory = null; public static SessionFactory getSessionFactory() { if(sessionFactory == null){
/**
* 1、 Configuration 类负责管理 Hibernate 的配置信息。包括如下内容: Hibernate
* 运行的底层信息:数据库的URL、用户名、密码、JDBC驱动类,数据库Dialect,数据库连接池等(对应
* hibernate.cfg.xml 文件)。 持久化类与数据表的映射关系(*.hbm.xml 文件)
*/
Configuration configeration = new Configuration().configure(); /**
* 2、 创建一个 ServiceRegistry 对象: hibernate 4.x 新添加的对象 hibernate
* 的任何配置和服务都需要在该对象中注册后才能有效.
*/
ServiceRegistry serviceRegistry =
new ServiceRegistryBuilder().applySettings(configeration.getProperties())
.buildServiceRegistry();
/**
* 3、创建SessionFactory对象
*/
sessionFactory = configeration.buildSessionFactory(serviceRegistry);
}
return sessionFactory; }
}
7、测试代码
package com.hibernate.配置文件.service.impl; import java.sql.Date; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction; import com.hibernate.配置文件.pojo.User;
import com.hibernate.配置文件.service.HibernateService;
import com.hibernate.配置文件.util.SessionFactoryUtil; public class HibernateServiceImpl implements HibernateService { public void hibernateTest(){
//1、创建SessionFactory对象
SessionFactory sessionFactory = SessionFactoryUtil.getSessionFactory(); //2、创建session
Session session = sessionFactory.openSession(); //3、开启事务
Transaction tran = session.beginTransaction(); //4、执行持久化操作
User user = new User("lvyf","123456",new Date(new java.util.Date().getTime()));
session.save(user); //5、提交事务
tran.commit(); //6、关闭session
session.close(); //7、关闭sessionFactory
sessionFactory.close();
} public static void main(String[] args) {
HibernateServiceImpl hs = new HibernateServiceImpl(); hs.hibernateTest();
}
}

