Hibernate核心类和接口

来源:互联网 发布:用友医疗器械软件 编辑:程序博客网 时间:2024/04/30 12:44

一、Hibernate核心类和接口预览图

二、hibernate.properties

这个文件是以前老版本使用的,类似于hibernate.cfg.xml文件,作用和hibernate.cfg.xml一致。

三、hibernate.cfg.xml 

(1)详细介绍

  • 该文件主要用于指定各个参数,是hibernate核心文件
  • 默认放在src目录下,也可以放在别的目录下
  • 指定连接数据库的驱动、用户名、密码、url、连接池
  • 指定对象关系映射文件的位置
  • 也可使用hibernate.properties文件来替代该文件.(推荐使用hibernate.cfg.xml)。

(2)配置文件模板

<?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.microsoft.sqlserver.jdbc.SQLServerDriver</property>        <property name="connection.url">jdbc:sqlserver://127.0.0.1:1433;DatabaseName=hibernate</property>        <property name="connection.username">sa</property>        <property name="connection.password">123456</property>        <!-- SQL方言,即明确告诉Hibernate连接的是哪种数据库 -->        <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>        <!-- 二级缓存设置,这里是关闭 -->        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>        <!-- 显示对应的SQL语句 -->        <property name="show_sql">true</property>                 <!-- 格式化输出SQL语句 -->          <property name="format_sql">true</property>             <!-- Drop and re-create the database schema on startup -->        <!--         update和create的区别:        create:每次先查询是否存在Student表,如果有则drop后再新建表,如果没有新建表        update:先查询是否存在Student表,如果有则更新数据(不会新建表,即使表结构发生变化),如果没有则新建表         --><!--    <property name="hbm2ddl.auto">create</property> -->        <property name="hbm2ddl.auto">update</property>        <!-- 指定管理对象映射文件 -->        <mapping resource="com/chongqing/hibernate/model/Student.hbm.xml"/>    </session-factory></hibernate-configuration>

四、*.hbm.xml

(1)对象关系映射文件(*.hbm.xml)

  • 该文件主要作用是建立表和类的映射关系,是不可或缺的重要文件
  • 一般放在其映射的类同一个目录下,但不是必须的
  • 命名方式一般是 类名.hbm.xml,但不是必须的
  • 示意图:

(2)配置文件模板

<?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.chongqing.hibernate.model"><class name="Student" table="student"><!-- id元素用于指定主键属性 -->  <id name="id" column="id" type="java.lang.Integer"><generator class="identity" /></id><!-- 对其他属性的配置 --><property name="name" type="java.lang.String"><column name="name" not-null="false" />  </property><property name="age" type="java.lang.Integer"><column name="age" not-null="false" /> </property></class></hibernate-mapping>

五、Configuration类

(1)详细介绍

  • 负责管理hibernate的配置信息
  • 读取hibernate.cfg.xml
  • 加载hibernate.cfg.xml配置文件中配置的驱动,url,用户名,密码,连接池
  • 管理 *.hbm.xml对象关系文件

(2)示意代码

//创建Configuration,该对象用于读取hibernate.cfg.xml配置文件,并完成初始化configuration = new Configuration().configure();

六、SessionFactory(会话工厂)接口

(1)详细介绍

  • 缓存sql语句和某些数据,它是重量级的
  • 之所以称SessionFactory是重量级的,是因为它需要一个很大的缓存,用来存放预定义的SQL语句及映射元素等
  • 在应用程序初始化的时候创建,是一个重量级的类(吃内存),一般用单例模式保证一个应用中只需要一个 SessionFactory实例
  • 如果某个应用访问多个数据库,则要创建多个会话工厂实例,一般是一个数据库一个会话工厂实例
  • 通过SessionFactory接口可以获得Session(会话)实例

(2)示意代码

//创建Configuration,该对象用于读取hibernate.cfg.xml配置文件,并完成初始化configuration = new Configuration().configure();//创建SessionFactory,一个SessionFactory对应一个数据存储源sessionfactory = configuration.buildSessionFactory();session = sessionfactory.openSession();//或者session = sessionfactory.getCurrentSession();

七、Session(会话)接口

(1)接口介绍 

  • Session一个实例代表与数据库的一次操作(当然一次操作可以是crud组合)
  • Session实例是轻量级的,所谓轻量级,是指它的创建和销毁不需要消耗太多的资源
  • Session实例通过SessionFactory获取,用完需要关闭
  • Session是线程不同步的(不安全),因此要保证在同一线程中使用,可以用getCurrentSessiong()
  • Session可以看做是持久化管理器,它是与持久化操作相关的接口

(2)Session(会话)接口的几个重要方法

初识化过程结束后,就可以调用SessionFactory实例的openSession()方法来获得Session实例,然后通过它来执行访问数据库的操作。Session接口提供了操作数据库的各自方法,如下:

  • 保存一个对象(记录)—save方法
  • 删除一个对象(记录)—delete方法
  • 查询一个对象(记录)—get/load方法
  • 修改一个对象(记录)—update方法

八、Transaction(事务)接口

(1)事务接口

事务简单的说,就是一组对数据库的操作集合,它们要么全部成功,要么全部失败.这个可以保证数据的一致性,事务具有原子性。

  • Transaction是底层的事物实现中抽象出来的接口
  • 可能是一个jdbc或者jta的事务,这样有利于hibernate在不同执行环境的移植
  • Hibernate要求显示的调用事务(如果仅仅是查询可以不调用)
try {session = sessionfactory.openSession();ts = session.beginTransaction();//do some work.....ts.commit();} catch (Exception e) {e.printStackTrace();if(ts!=null){ts.rollback();}}finally{if(session!=null&&session.isOpen()){session.close();}}

(2)全局事务和本地事务

本地事务:针对一个数据库的事务(jabc事务)
全局事务:跨数据库的事务(jta事务)

九、Query接口

 Query接口类型的对象可以对数据库操作,它可以使用HQL,Qbc,Qbe和原生SQL(native Sql)对数据库操作,官方推荐使用HQL语句。

Session session = null;Transaction ts = null;try {SessionFactory sessionfactory = MySessionFactory.getSessionFactory();session = sessionfactory.openSession();ts = session.beginTransaction();//注意这里的Student不是表,而是domian类名//where后面的条件即可以是表的字段名,也可以是domain类的属性,推荐使用类的属性名Query query = (Query) session.createQuery("from Student where name='s1'");//通过list方法获取结果,这个list会自动的封装成对应的domain对象//所以不用对jdbc进行二次封装List<Student> list = query.list();for(Student s:list) {System.out.println("id="+s.getId()+"name="+s.getName());}ts.commit();} catch (Exception e) {e.printStackTrace();if(ts!=null){ts.rollback();}}finally{if(session!=null&&session.isOpen()){session.close();}}

十、 Criteria接口

Criteria接口也可用于面向对象方式的查询,Criteria一般用于条件查询

Session session = null;Transaction ts = null;try {SessionFactory sessionfactory = MySessionFactory.getSessionFactory();session = sessionfactory.openSession();ts = session.beginTransaction();//查询Criteria criteria = session.createCriteria(Student.class).setMaxResults(4);List<Student> list = criteria.list();for(Student s:list) {System.out.println("id="+s.getId()+"name="+s.getName());}ts.commit();} catch (Exception e) {e.printStackTrace();if(ts!=null){ts.rollback();}}finally{if(session!=null&&session.isOpen()){session.close();}}

1 0
原创粉丝点击