hibernate入门学习(一)

来源:互联网 发布:截获特定端口的数据包 编辑:程序博客网 时间:2024/06/14 12:50

  hibernate工作原理:

  1.通过Configuration config = new Configuration().configure();//读取并解析hibernate.cfg.xml配置文件

  2.由hibernate.cfg.xml中的读取并解析映射信息

  3.通过SessionFactory sf = config.buildSessionFactory();//创建SessionFactory

  4.Session session = sf.openSession();//打开Sesssion

  5.Transaction tx = session.beginTransaction();//创建并启动事务Transation

  6.persistent operate操作数据,持久化操作

  7.tx.commit();//提交事务

  8.关闭Session

  9.关闭SesstionFactory

  为什么要用hibernate:

  1. 对JDBC访问数据库的代码做了封装,大大简化了数据访问层繁琐的重复性代码。

  2. Hibernate是一个基于JDBC的主流持久化框架,是一个优秀的ORM实现。他很大程度的简化DAO层的编码工作

  3. hibernate使用Java反射机制,而不是字节码增强程序来实现透明性。

  4. hibernate的性能非常好,因为它是个轻量级框架。映射的灵活性很出色。它支持各种关系数据库,从一对一到多对多的各种复杂关系。

    hibernate案例

  搭建一个Hibernate环境,开发步骤:

  1. 下载源码

  版本:hibernate-distribution-3.6.0.Final

  2. 引入jar文件

  hibernate3.jar核心 + required 必须引入的(6个) + jpa 目录 + 数据库驱动包

  3. 写对象以及对象的映射

  Employee.java 对象

  Employee.hbm.xml 对象的映射 (映射文件)

  4. src/hibernate.cfg.xml 主配置文件

  -à 数据库连接配置

  -à 加载所用的映射(*.hbm.xml)

package com.hyxd.entity;import java.util.Date;public class Dept {/**          *get/set方法          */private int d_id;private String d_name;private Date d_date;}

<?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 package="com.hyxd.entity.Dept"><class name="com.hyxd.entity.Dept" table="depts"><id name="d_id" column="d_id" type="java.lang.Integer"><generator class="native"></generator></id><property name="d_name" column="d_name" type="java.lang.String"></property><property name="d_date" column="d_date" type="java.util.Date"></property></class></hibernate-mapping>

注意:有些人可能第一次配置好hibernate文件之后,测试的时候,不好使,一定要仔细检查自己的*.hbn.xml映射文件

<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>       <property name="hibernate.connection.url">jdbc:mysql:///erp</property>       <property name="hibernate.connection.username">root</property>       <property name="hibernate.connection.password">root</property>       <property name="hibernate.show_sql">true</property>       <mapping resource="com/hyxd/entity/dept.hbm.xml"/></session-factory></hibernate-configuration>

这是hibernate.cfg.xml这个配置文件要放到src目录下面

public class DeptTest {private DeptService service = new DeptServiceImpl();@Testpublic void testSave() throws Exception {Dept d = new Dept();d.setD_name("工程部");d.setD_date(new Date());service.save(d);}}

这里省略了一些加载hibernate.cfg.xml文件的一些类主要还是跟hibernate的工作原理是类似的,先是加载src下面的hibernate.cfg.xml然后在读取hibernate.cfg.xml里面数据库连接信息,保存到session工厂里面,然后在加载<Mapping resource"*.hbm.xml"/>的文件获取实体类的一些信息,然后创建并启动事物,操作数据,提交事物,关闭session。

        

Hibernate  Api

|-- Configuration       配置管理类对象

         config.configure();    加载主配置文件的方法(hibernate.cfg.xml)  默认加载src/hibernate.cfg.xml

         config.configure(“cn/config/hibernate.cfg.xml”);   加载指定路径下指定名称的主配置文件

         config.buildSessionFactory();   创建session的工厂对象

 

|-- SessionFactory     session的工厂(或者说代表了这个hibernate.cfg.xml配置文件)

                                     sf.openSession();   创建一个sesison对象

                                      sf.getCurrentSession();  创建session或取出session对象

 

|--Session      session对象维护了一个连接(Connection), 代表了与数据库连接的会话。

                         Hibernate最重要的对象: 只用使用hibernate与数据库操作,都用到这个对象

                         session.beginTransaction();开启一个事务; hibernate要求所有的与数据库的操作必须有事务的环境,否则报错!

0 0
原创粉丝点击