一、Hibernate简介
1、什么是Hibernate?
Hibernate是数据持久层的一个轻量级框架。数据持久层的框架有很多比如:iBATIS,myBatis,Nhibernate,Siena等等。并且Hibernate是一个开源的orm(Object relations mapping)框架,提供了查询获取数据的方法,用面向对象的思想来操作数据库,节省了我们开发处理数据的时间。
2、Hibernate的优点
1)、使用简洁的hql语句(Hibernate query language)。可以不使用传统的insert,update等sql语句。比如insert一个对象,原来的做法是:insert into 表名称 value(值1,值2,值3,……),而现在的做法是:save(对象)。
2)、使用or映射。对象到关系数据库之间的映射。是从对象的角度操作数据库,再次体现了面向对象思想。原来的实体抽取方法:首先有了表,然后表映射实体对象。而现在Hibernate做法是:直接由对象映射到表。
3)、没有侵入性,移植性比较好。什么是没有侵入性?就是Hibernate采用了pojo对象。所谓的pojo对象就是没有继承Hibernate类或实现Hibernate接口。这样的话,此类就是一个普通的java类,所以移植性比较好。
4)、支持透明持久化。透明是针对上层而言的。三层架构的理念是上层对下层的依赖,只是依赖接口不依赖具体实现。而Hibernate中的透明是指对业务逻辑层提供了一个接口session,而其他的都封装隐藏。持久化是指把内存中的数据存放到磁盘上的文件中。
3、当然一个事物,不可能十全十美,即使如此优秀的Hibernate也有自己的弱点。比如:若是大量数据批量操作。则不适合使用Hibernate。并且一个持久化对象不能映射到多张表中。
4、Hibernate中5个核心接口
1)、Configuration接口:负责配置及启动Hibernate,用来创建sessionFactory
2)、SessionFactory接口:一个SessionFactory对应一个数据源存储,也就是一个数据库对应一个SessionFactory。SessionFactory用来创建Session对象。并且SessionFactory是线程安全的,可以由多个线程访问SessionFactory共享。
3)、Session接口:这个接口是Hibernate中常用的接口,主要用于对数据的操作(增删改查)。而这个Session对象不是线程安全的。不能共享。
4)、Query接口:用于数据库的查询对象。
5)、Transaction接口:Hibernate事务接口。它封装了底层的事务操作,比如JTA(;java transcation architecture)所有的数据操作,比如增删改查都写在事务中。
二、实例:一个Hibernate小程序
创建一个Hello_World的Java项目,导入所需jar包
在src目录下,创建一个hibernate.cfg.xml的配置文件,文件配置设置可以参见hibernate的官方文档。
<?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.driver_class">com.mysql.jdbc.Driver</property> ???????<!-- Student为你的数据库名称 --> ???????<property name="connection.url">jdbc:mysql://localhost:3306/Student</property> ???????<property name="connection.username">root</property> ???????<property name="connection.password">123456</property> ???????<!-- JDBC connection pool (use the built-in) --> ???????<property name="connection.pool_size">1</property> ???????<!-- SQL方言 --> ???????<property name="dialect">org.hibernate.dialect.MySQLDialect</property> ???????<!-- Enable Hibernate‘s automatic session context management --> ???????<property name="current_session_context_class">thread</property> ???????<!-- Disable the second-level cache --> ???????<property name="cache.provider_class"> ???????????org.hibernate.cache.NoCacheProvider ???????</property> ???????<!-- true:在控制台显示SQL语句 --> ???????<property name="show_sql">true</property> ???????<!-- 格式化输出SQL语句 --> ???????<property name="format_sql">true</property> ???????<!-- validate 加载hibernate时,验证创建数据库表结构 --> ???????<!-- create 每次加载hibernate,重新创建数据库表结构,这就是导致数据库表数据丢失的原因。 --> ???????<!-- create-drop 加载hibernate时创建,退出是删除表结构 --> ???????<!-- update 加载hibernate自动更新数据库结构 --> ???????<property name="hbm2ddl.auto">create</property> ???????<property name="myeclipse.connection.profile"></property> ???????<!-- hibernate与数据库的对象关系映射文件**.hbm.xml --> ???????<mapping resource="com/lsj/hibernate/model/Student.hbm.xml" /> ???????<!-- 使用annotion注解时,hibernate与数据库对象关系映射表示 --> ???????<!-- 推荐使用annotion注解来表示映射关系 --> ???????<mapping class="com.lsj.hibernate.model.Teacher" /> ???</session-factory></hibernate-configuration>
创建一个Teacher的实体类,这里用的是annotion注解,还有一种方法是创建XXX.hbm.xml的关系映射文件
package com.lsj.hibernate.model;import javax.persistence.Entity;import javax.persistence.Id;import javax.persistence.Table;//Entity是javax.persistence.Entity包中的@Entity// 映射的表名@Table(name = "teacher")public class Teacher { ???private int id; ???private int age; ???private String name; ???private String title; ???// 主键,一般写在get方法中,而不是在定义中 ???@Id ???public int getId() { ???????return id; ???} ???public void setId(int id) { ???????this.id = id; ???} ???public int getAge() { ???????return age; ???} ???public void setAge(int age) { ???????this.age = age; ???} ???public String getName() { ???????return name; ???} ???public void setName(String name) { ???????this.name = name; ???} ???public String getTitle() { ???????return title; ???} ???public void setTitle(String title) { ???????this.title = title; ???}}
创建一个TeacherTest的测试类
package com.lsj.hibernate.test;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import com.lsj.hibernate.model.Teacher;public class TeacherTest { ???public static void main(String[] args) { ???????Teacher t = new Teacher(); ???????t.setAge(30); ???????t.setName("小杰"); ???????t.setTitle("背包客"); ???????Configuration cfg = new Configuration(); ???????// 读取hibernate.cfg.xml中的配置 ???????cfg.configure(); ???????// 获取SessionFactory ???????SessionFactory sf = cfg.buildSessionFactory(); ???????// 获取Session ???????Session session = sf.openSession(); ???????// 开启事务 ???????session.beginTransaction(); ???????// 保存 ???????session.save(t); ???????// 提交事务 ???????session.getTransaction().commit(); ???????// 关闭连接 ???????session.close(); ???????sf.close(); ???}}
这样一个简单的Hibernate程序就写完,运行的时候记得打开MySQL的服务。
Hibernate简介与实例
原文地址:http://www.cnblogs.com/goloving/p/7609078.html