开始之前先介绍一下Hibernate开发的步骤:
①.创建hIbernate的配置文件
②. 创建持久化类
③. 创建对象-关系映射文件
④. 通过Hibernate API 编写访问数据库的代码
1. 搭建环境
2. 项目结构:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~分割线~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1.创建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">123456</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///hibernate</property>
<!-- 配置hibernate 的基本信息 -->
<!-- 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>
<!-- 设置Hibernate 的事务隔离级别-->
<property name="hibernate.connection.isolation">2</property>
<!-- 删除对象后,使其OID 置为null-->
<property name="use_identifier_rollback">true</property>
<!-- 配置c3p0 数据源-->
<property name="hibernate.c3p0.max_size">30</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.acquire_increment">5</property>
<property name="hibernate.c3p0.idle_test_period">2000</property>
<property name="hibernate.c3p0.timeout">2000</property>
<property name="hibernate.c3p0.max_statements">10</property>
<!-- 下面两条批量操作的设置 对mysql 是无效的 ,但是对Oracle 是有效的 -->
<!-- 设置JDBC 的 Statement 读取数据的时候,每次从数据库中取出的记录条数 -->
<property name="hibernate.jdbc.factory_class">100</property>
<!-- 设置对 数据库进行批量删除,批量更新和批量插入的时候的批次大小 -->
<property name="hibernate.jdbc.batch_size">30</property>
<!--启用二级缓存 -->
<property name="cache.use_second_level_cache">true</property>
<!--配置使用的二级缓存的产品 -->
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
<!-- 启用查询缓存 -->
<property name="hibernate.cache.use_query_cache">true</property>
<!-- 配置管理session 的方式 -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- 指定关联的 .hbm.xml 文件 -->
<mapping resource="com/baidu/hibernate/helloworld/News.hbm.xml"/>
<!-- 其他的配置项参考:hibernate-release-4.2.4.Final/documentation/manual/en-US/html_single/index.html -->
</session-factory>
</hibernate-configuration>
2.创建持久化类:News.java
package com.baidu.hibernate.helloworld;
import java.sql.Date;
public class News {
private Integer id;
private String title;
private String author;
private Date date;
public News(String title, String author, Date date) {
super();
this.title = title;
this.author = author;
this.date = date;
}
public News() {
super();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@Override
public String toString() {
return "News [id=" + id + ", title=" + title + ", author=" + author
+ ", date=" + date + "]";
}
}
3.创建对象-关系映射文件
: News.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">
<!-- Generated 2016-5-14 22:57:16 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<!--
name="com.baidu.hibernate.helloworld.News" :全类名
table="NEWS" :创建的 表 的名称
name="id" type="java.lang.Integer" :主键 和其类型
column name="ID" :表 NEWS 中 的 一列 列名是 ID
generator class="native" : 指定主键的生成方式
native :使用数据库本地方式生成主键
-->
<class name="com.baidu.hibernate.helloworld.News" table="NEWS">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<!-- 指定主键的生成方式 native:使用数据库本地方式 -->
<generator class="native" />
</id>
<property name="title" type="java.lang.String">
<column name="TITLE" />
</property>
<property name="author" type="java.lang.String">
<column name="AUTHOR" />
</property>
<property name="date" type="java.sql.Date">
<column name="DATE" />
</property>
</class>
</hibernate-mapping>
4. . 通过Hibernate API 编写访问数据库的代码 : TestHibernate.java
package com.baidu.hibernate.helloworld;
import static org.junit.Assert.*;
import java.sql.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.Test;
public class TestHibernate {
@Test
public void test() {
//1. 创建一个SessionFactory 对象
SessionFactory sessionFactory = null;
//① 创建Configuration 对象:对应hibernate 的基本配置信息,和对象关系映射信息
Configuration configuration = new Configuration().configure();
/**
* 在hibernate 4.0 以前 创建session工厂 就是下面一条信息就可以了,4.0 版本以后的稍稍麻烦一点
* sessionFactory = configuration.buildSessionFactory();
*/
//② 创建一个ServiceRegistry 对象: 这个对象是hibernate4.0版本以后新添加的对象
// 其作用:hibernate 的任何配置和服务都要在该对象中注册后才能有效
ServiceRegistry serviceRegistry =
new ServiceRegistryBuilder().applySettings(configuration.getProperties())
.buildServiceRegistry();
//③ 创建sessionFactory
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
//2. 创建一个Session 对象
Session session = sessionFactory.openSession();
//3. 开启事务
Transaction transaction = session.beginTransaction();
//4. 执行保存操作
News news = new News("mySql","BaiDu",new Date(new java.util.Date().getTime()));
session.save(news);
//5. 提交事务
transaction.commit();
//6. 关闭Session
session.close();
//7. 关闭SessionFactory 对象
sessionFactory.close();
}
}