灵犀一脚C

博客园 首页 联系 订阅 管理

                                                                           Hibernate 框架

1.1   什么是框架?

   框架是一个提供了可重用的公共结构半成品。

 

2.1   关于Hibernate

     Hibernate是数据持久层的一个轻量级框架。数据持久层的框架有很多比如:iBATIS,myBatis,Nhibernate,Siena等等。并且Hibernate是一个开源的orm(Object relations mapping)框架,提供了查询获取数据的方法,用面向对象的思想来操作数据库,节省了我们开发处理数据的时间。

    (数据持久化是将内存中的数据模型转换为存储模型,以及将存储模型转换为内存中的数据模型的统称)

 2.2   Hibernate的优势

          1)、使用简介的hql语句(Hibernate query language)。可以不使用传统的insert,update等sql语句。比如insert一个对象,原来的做法是:insert into 表名称         alue(值1,值2,值3,……),而现在的做法是:save(对象)。

          2)、使用or映射。对象到关系数据库之间的映射。是从对象的角度操作数据库,再次体现了面向对象思想。原来的实体抽取方法:首先有了表,然后表映射实体对象。而现在Hibernate做法是:直接由对象映射到表。

          3)、没有侵入性,移植性比较好。什么是没有侵入性?就是Hibernate采用了pojo对象。所谓的pojo对象就是没有继承Hibernate类或实现Hibernate接口。这样的话,此类就是一个普通的java类,所以移植性比较好。  

          4)、支持透明持久化。透明是针对上层而言的。三层架构的理念是上层对下层的依赖,只是依赖接口不依赖具体实现。而Hibernate中的透明是指对业务逻辑层提供了一个接口session,而其他的都封装隐藏。持久化是指把内存中的数据存放到磁盘上的文件中。

2.3   Hibernate的缺点

       当然一个事物,不可能十全十美,即使如此优秀的Hibernate也有自己的弱点。比如:若是大量数据批量操作。则不适合使用Hibernate。并且一个持久化对象不能映射到多张表中。

2.4   持久化与ORM

       ORM:Object Relational Mapping)对象关系映射

 

     持久化:持久化是程序数据在瞬时状态和持久状态间转换的过程

 

3.1   Hibernate 环境搭建

    1.  下载需要的jar包

      

 

       

 

  4.1   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配置文件hibernate.cfg.xml

   

1 <?xml version='1.0' encoding='utf-8'?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 5 
 6 <hibernate-configuration>
 7 
 8     <session-factory>
 9 
10         <!-- 数据库连接设置 -->
11         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
12         <!-- Student为你的数据库名称 -->
13         <property name="connection.url">jdbc:mysql://localhost:3306/Student</property>
14         <property name="connection.username">root</property>
15         <property name="connection.password">1025263614</property>
16 
17         <!-- JDBC connection pool (use the built-in) -->
18         <property name="connection.pool_size">1</property>
19 
20         <!-- SQL方言 -->
21         <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
22 
23         <!-- Enable Hibernate's automatic session context management -->
24         <property name="current_session_context_class">thread</property>
25 
26         <!-- Disable the second-level cache -->
27         <property name="cache.provider_class">
28             org.hibernate.cache.NoCacheProvider
29         </property>
30 
31         <!-- 显示SQL语句 -->
32         <property name="show_sql">true</property>
33         <!-- 格式化输出SQL语句 -->
34         <property name="format_sql">true</property>
35 
36         <!-- validate 加载hibernate时,验证创建数据库表结构 -->
37         <!-- create 每次加载hibernate,重新创建数据库表结构,这就是导致数据库表数据丢失的原因。 -->
38         <!-- create-drop 加载hibernate时创建,退出是删除表结构 -->
39         <!-- update 加载hibernate自动更新数据库结构 -->
40         <property name="hbm2ddl.auto">create</property>
41         <property name="myeclipse.connection.profile"></property>
42 
43         <!-- hibernate与数据库的对象关系映射文件**.hbm.xml -->
44         <mapping resource="com/lsj/hibernate/model/Student.hbm.xml" />
45         <!-- 使用annotion注解时,hibernate与数据库对象关系映射表示 -->
46         <!-- 推荐使用annotion注解来表示映射关系 -->
47         <mapping class="com.lsj.hibernate.model.Teacher" />
48     </session-factory>
49 
50 </hibernate-configuration>

小配置文件  xxxxx.hbm.xml

<!--?xml version="1.0"?-->
 
<hibernate-mapping package="com.zhouxiang.model">
    <class name="User" polymorphism="explicit">
        <id name="id">
            <generator class="uuid"></generator>
        </id>
        <property name="name" column="username"></property>
        <property name="age" column="age"></property>
    </class>   
</hibernate-mapping>

 

 

 

创建一个Teacher的实体类,这里用的是annotion注解,还有一种方法是创建XXX.hbm.xml的关系映射文件,具体实现请参见附件的源码

 1 package com.lsj.hibernate.model;
 2 
 3 import javax.persistence.Entity;
 4 import javax.persistence.Id;
 5 import javax.persistence.Table;
 6 
 7 //Entity是javax.persistence.Entity包中的
 8 @Entity
 9 // 映射的表名
10 @Table(name = "teacher")
11 public class Teacher {
12 
13     private int id;
14     private int age;
15     private String name;
16 17 
18     // 主键,一般写在get方法中,而不是在定义中
19     @Id
20     public int getId() {
21         return id;
22     }
23 
24     public void setId(int id) {
25         this.id = id;
26     }
27 
28     public int getAge() {
29         return age;
30     }
31 
32     public void setAge(int age) {
33         this.age = age;
34     }
35 
36     public String getName() {
37         return name;
38     }
39 
40     public void setName(String name) {
41         this.name = name;
42     }
43 
44   45       4647
48     49       5051 }

创建一个TeacherTest的测试类

1 package com.lsj.hibernate.test;
 2 
 3 import org.hibernate.Session;
 4 import org.hibernate.SessionFactory;
 5 import org.hibernate.cfg.Configuration;
 6 
 7 import com.lsj.hibernate.model.Teacher;
 8 
 9 public class TeacherTest {
10 
11     public static void main(String[] args) {
12         Teacher t = new Teacher();
13         t.setAge(30);
14         t.setName("小杰");
15        16 
17         Configuration cfg = new Configuration();
18         // 读取hibernate.cfg.xml中的配置
19         cfg.configure();
20         // 获取SessionFactory
21         SessionFactory sf = cfg.buildSessionFactory();
22         // 获取Session
23         Session session = sf.openSession();
24         // 开启事务
25         session.beginTransaction();
26         // 保存
27         session.save(t);
28         // 提交事务
29         session.getTransaction().commit();
30         // 关闭连接
31         session.close();
32         sf.close();
33     }
34 }

 

 

 

 

 

 

 

 

5.1  总结

   其实简而言之hibernate只做了一件事,那就是将对象及对象关系持久化到关系型数据库中,而这种映射关系是直接使用面向对象编程思维来操作数据库,这样使得程序员在编程的过程中只需要关注如何处理对象与对象间的关系,而不需要去考虑对象是如何持久化到数据库中。hibernate使得编程人员在软件开发过程中将更多的精力集中在对象的处理上,简化了数据持久化的过程,加快了开发速度,增强了开发效率,降低了风险。

 

posted on 2017-01-10 14:32  灵犀一脚C  阅读(156)  评论(0编辑  收藏  举报