Hibernate —— 概述与 HelloWorld
一、Hibernate 概述
1.Hibernate 是一个持久化框架
(1)从狭义的角度来讲,“持久化” 仅仅指把内存中的对象永久的保存到硬盘中的数据库中。
(2)从广义的角度来讲,“持久化” 包括和数据库相关的各种操作。如:CRUD。
2.Hibernate 是一个 ORM 框架
ORM:对象关系映射。O 面向对象:类、对象、属性。R 面向关系:表、行(记录)、列(字段)。M 映射。
ORM 思想:关系数据库中的 行(记录)映射一个对象,程序员可以把对数据库的操作转化为对对象的操作。
在 Hibernate 中,存在 对象关系映射文件用来描述对象与表记录之间的映射关系。
3.Hibernate 配置文件:hibernate.cfg.xml ,配置数据库连接、Hibernate 本身的一些信息、对象关系映射。
4.Entity.hbm.xml 配置文件:对象关系映射文件。
5.Session 接口:Session 是应用程序与数据库之间交互操作的一个单线程对象,是 Hibernate 运作的中心。Session 有一个一级缓存,相当于 JDBC 中的 Connection。
6.SessionFactory 接口:根据配置生成 Session 的工厂。
7.Transaction :事务,可以通过 Session 来开启事务。
二、HelloWorld
(1)在 Intellij Idea 下新建 hibernate.cfg.xml 和根据表创建 实体和 Entity.hbm.xml 文件已经在前一篇文章中讲过了。
(2)生成的 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.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property> <!-- 配置 Hibernate 的基本信息 --> <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <!-- 指定自动生成数据表的策略 --> <property name="hbm2ddl.auto">update</property> <mapping resource="com/nucsoft/hibernate/News.hbm.xml"/> </session-factory> </hibernate-configuration>
(2)创建的 news 表。
(3)生成的 News 实体和 News.hbm.xml 文件。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/** * @author solverpeng * @create 2016-09-28-19:36 */ public class News { private int id; private String title; private String author; private Date date; public int getId() { return id; } public void setId(int 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 boolean equals(Object o) { if(this == o) { return true; } if(o == null || getClass() != o.getClass()) { return false; } News news = (News) o; if(id != news.id) { return false; } if(title != null ? !title.equals(news.title) : news.title != null) { return false; } if(author != null ? !author.equals(news.author) : news.author != null) { return false; } if(date != null ? !date.equals(news.date) : news.date != null) { return false; } return true; } @Override public int hashCode() { int result = id; result = 31 * result + (title != null ? title.hashCode() : 0); result = 31 * result + (author != null ? author.hashCode() : 0); result = 31 * result + (date != null ? date.hashCode() : 0); return result; } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?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.nucsoft.hibernate.News" table="news" schema="hibernate"> <id name="id"> <column name="id" sql-type="int(11)"/> </id> <property name="title"> <column name="title" sql-type="varchar(255)" not-null="true"/> </property> <property name="author"> <column name="author" sql-type="varchar(50)" length="50" not-null="true"/> </property> <property name="date"> <column name="date" sql-type="date" not-null="true"/> </property> </class> </hibernate-mapping>
(4)测试
- 获取 SessionFactory 对象
- 通过 SessionFactory 获取 Session 对象
- 通过 Session 开启事务
- 执行数据库操作
- 提交事务
- 关闭资源
@Test public void test() { // 1.创建 SessionFactory SessionFactory sessionFactory = null; Configuration configuration = new Configuration().configure(); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); // 2.创建 Session Session session = sessionFactory.openSession(); // 3.开启事务 Transaction transaction = session.beginTransaction(); // 4.执行数据库操作 News news = new News(); news.setTitle("Title"); news.setAuthor("tom"); news.setDate(new Date(new java.util.Date().getTime())); session.save(news); // 5.提交事务 transaction.commit(); // 6.关闭 Session session.close(); // 7.关闭 SessionFactory sessionFactory.close(); }
说明:
①SessionFactory 对象是通过 Configuration 对象来获取的,Configuration 的 configure() 方法会默认取读取类路径下的 hibernate.cfg.xml 文件。
从 hibernate.cfg.xml 中读取的配置会向 ServiceRegistry 中注册,最终生成的 SessionFactory 对象就是根据 hibernate.cfg.xml 配置生成的。
②事务的开启:Session 的 beginTransaction() 方法。
③注意资源的关闭。
④事实上,在容器的环境下,我们不需要去关注 SessionFactory 的获取,资源的关闭。
结果:
三、总结
从第一部分 Hibernate 概述中,确定了学习 Hibernate,要从以下几个方面来学习:Hibernate的配置文件,对象关系映射文件(主要内容是关联关系),Session API 以及缓存,事务控制。
从第二部分 Helloworld 中,体验了 Hibernate的 便捷,关于配置的添加,上篇文章已经说过,在以后的文章中详细介绍具体配置。