Hibernate框架基础

来源:互联网 发布:pbft 共识算法 实验 编辑:程序博客网 时间:2024/04/29 16:46

Hibernate框架基础

Hibernate框架

ORM概念

  • O, Object 对象
  • R, Realtion 关系 (关系型数据库: MySQL, Oracle…)
  • M,Mapping 映射

ORM, 对象关系映射!

ORM, 解决什么问题?

存储: 能否把对象的数据直接保存到数据库?

获取: 能否直接从数据库拿到一个对象?

想做到上面2点,必须要有映射!

  • 总结:

Hibernate与ORM的关系?

Hibernate是ORM的实现!

Hibernate HelloWorld案例

  • 搭建一个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)
  5. App.java 测试
Employee.java     对象//一、 对象public class Employee {private int empId;private String empName;private Date workDate;}Employee.hbm.xml  对象的映射<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="cn.itcast.a_hello"><class name="Employee" table="employee"><!-- 主键 ,映射--><id name="empId" column="id"><generator class="native"/></id><!-- 非主键,映射 --><property name="empName" column="empName"></property><property name="workDate" column="workDate"></property></class></hibernate-mapping>hibernate.cfg.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:///hib_demo</property><property name="hibernate.connection.username">root</property><property name="hibernate.connection.password">root</property><property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property><property name="hibernate.show_sql">true</property><!-- 加载所有映射 --><mapping resource="cn/itcast/a_hello/Employee.hbm.xml"/></session-factory></hibernate-configuration>App.java   测试类public class App {@Testpublic void testHello() throws Exception {// 对象Employee emp = new Employee();emp.setEmpName("班长");emp.setWorkDate(new Date());// 获取加载配置文件的管理类对象Configuration config = new Configuration();config.configure();  // 默认加载src/hibenrate.cfg.xml文件// 创建session的工厂对象SessionFactory sf = config.buildSessionFactory();// 创建session (代表一个会话,与数据库连接的会话)Session session = sf.openSession();// 开启事务Transaction tx = session.beginTransaction();//保存-数据库session.save(emp);// 提交事务tx.commit();// 关闭session.close();sf.close();}}

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要求所有的与数据库的操作必须有事务的环境,否则报错!
  • 更新:

    • session.save(obj); 保存一个对象
    • session.update(emp); 更新一个对象
    • session.saveOrUpdate(emp); 保存或者更新的方法:
      • 没有设置主键,执行保存;
      • 有设置主键,执行更新操作;
      • 如果设置主键不存在报错!
  • 主键查询:

    • session.get(Employee.class, 1); 主键查询
    • session.load(Employee.class, 1); 主键查询 (支持懒加载)
  • HQL查询:

    • HQL查询与SQL查询区别:
      • SQL: (结构化查询语句)查询的是表以及字段; 不区分大小写。
      • HQL: hibernate query language 即hibernate提供的面向对象的查询语言
        • 查询的是对象以及对象的属性。
        • 区分大小写。
  • Criteria查询:

    • 完全面向对象的查询。
  • 本地SQL查询:

    • 复杂的查询,就要使用原生态的sql查询,也可以,就是本地sql查询的支持! (缺点: 不能跨数据库平台!)
  • Transaction hibernate事务对象

Hibernate crud

public class EmployeeDaoImpl implements IEmployeeDao{@Overridepublic Employee findById(Serializable id) {Session session = null;Transaction tx = null;try {// 获取Sessionsession = HibernateUtils.getSession();// 开启事务tx = session.beginTransaction();// 主键查询return (Employee) session.get(Employee.class, id);} catch (Exception e) {throw new RuntimeException(e);} finally {tx.commit();session.close();}}@Overridepublic List<Employee> getAll() {Session session = null;Transaction tx = null;try {session = HibernateUtils.getSession();tx = session.beginTransaction();// HQL查询Query q = session.createQuery("from Employee");return q.list();} catch (Exception e) {throw new RuntimeException(e);} finally {tx.commit();session.close();}}@Overridepublic List<Employee> getAll(String employeeName) {Session session = null;Transaction tx = null;try {session = HibernateUtils.getSession();tx = session.beginTransaction();Query q =session.createQuery("from Employee where empName=?");// 注意:参数索引从0开始q.setParameter(0, employeeName);// 执行查询return q.list();} catch (Exception e) {throw new RuntimeException(e);} finally {tx.commit();session.close();}}@Overridepublic List<Employee> getAll(int index, int count) {Session session = null;Transaction tx = null;try {session = HibernateUtils.getSession();tx = session.beginTransaction();Query q = session.createQuery("from Employee");// 设置分页参数q.setFirstResult(index);  // 查询的其实行 q.setMaxResults(count);  // 查询返回的行数List<Employee> list = q.list();return list;} catch (Exception e) {throw new RuntimeException(e);} finally {tx.commit();session.close();}}@Overridepublic void save(Employee emp) {Session session = null;Transaction tx = null;try {session = HibernateUtils.getSession();tx = session.beginTransaction();// 执行保存操作session.save(emp);} catch (Exception e) {throw new RuntimeException(e);} finally {tx.commit();session.close();}}@Overridepublic void update(Employee emp) {Session session = null;Transaction tx = null;try {session = HibernateUtils.getSession();tx = session.beginTransaction();session.update(emp);} catch (Exception e) {throw new RuntimeException(e);} finally {tx.commit();session.close();}}@Overridepublic void delete(Serializable id) {Session session = null;Transaction tx = null;try {session = HibernateUtils.getSession();tx = session.beginTransaction();// 先根据id查询对象,再判断删除Object obj = session.get(Employee.class, id);if (obj != null) {session.delete(obj);}} catch (Exception e) {throw new RuntimeException(e);} finally {tx.commit();session.close();}}}

Hibernate.cfg.xml 主配置

  • Hibernate.cfg.xml

    • 主配置文件中主要配置:数据库连接信息、其他参数、映射信息!
  • 常用配置查看源码:

    • hibernate-distribution-3.6.0.Final\project\etc\hibernate.properties
  • 配置示例

<?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="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>        <property name="connection.url">jdbc:mysql://localhost:3306/day14?useUnicode=true&amp;characterEncoding=UTF-8</property>        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>        <property name="connection.username">root</property>        <property name="connection.password">1234</property>        <!--其他配置信息-->        <property name="show_sql">true</property>         <property name="hbm2ddl.auto">update</property>        <!--导入映射文件-->        <mapping resource="cn/csx/domain/User.hbm.xml"/>    </session-factory></hibernate-configuration>

数据库连接参数配置

  • 数据库连接参数配置

例如:

## MySQL#hibernate.dialect org.hibernate.dialect.MySQLDialect#hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect#hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect#hibernate.connection.driver_class com.mysql.jdbc.Driver#hibernate.connection.url jdbc:mysql:///test#hibernate.connection.username gavin#hibernate.connection.password
  • 自动建表

Hibernate.properties

#hibernate.hbm2ddl.auto create-drop 每次在创建sessionFactory时候执行创建表;当调用sesisonFactory的close方法的时候,删除表!#hibernate.hbm2ddl.auto create   每次都重新建表; 如果表已经存在就先删除再创建#hibernate.hbm2ddl.auto update  如果表不存在就创建; 表存在就不创建;#hibernate.hbm2ddl.auto validate  (生成环境时候) 执行验证: 当映射文件的内容与数据库表结构不一样的时候就报错!

映射配置

映射配置

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><!-- 映射文件: 映射一个实体类对象;  描述一个对象最终实现可以直接保存对象数据到数据库中。  --><!-- package: 要映射的对象所在的包(可选,如果不指定,此文件所有的类都要指定全路径)auto-import 默认为true, 在写hql的时候自动导入包名如果指定为false, 再写hql的时候必须要写上类的全名;  如:session.createQuery("from cn.itcast.c_hbm_config.Employee").list(); --><hibernate-mapping package="cn.csx.c_hbm_config" auto-import="true"><!-- class 映射某一个对象的(一般情况,一个对象写一个映射文件,即一个class节点)name 指定要映射的对象的类型table 指定对象对应的表;  如果没有指定表名,默认与对象名称一样  --><class name="Employee" table="employee"><!-- 主键 ,映射--><id name="empId" column="id"><!-- 主键的生成策略identity  自增长(mysql,db2)sequence  自增长(序列), oracle中自增长是以序列方法实现native  自增长【会根据底层数据库自增长的方式选择identity或sequence】如果是mysql数据库, 采用的自增长方式是identity如果是oracle数据库, 使用sequence序列的方式实现自增长increment  自增长(会有并发访问的问题,一般在服务器集群环境使用会存在问题。)assigned  指定主键生成策略为手动指定主键的值uuid      指定uuid随机生成的唯一的值foreign   (外键的方式, one-to-one讲) --><generator class="uuid"/></id><!-- 普通字段映射propertyname  指定对象的属性名称column 指定对象属性对应的表的字段名称,如果不写默认与对象属性一致。length 指定字符的长度, 默认为255type   指定映射表的字段的类型,如果不指定会匹配属性的类型java类型:     必须写全名hibernate类型:  直接写类型,都是小写--><property name="empName" column="empName" type="java.lang.String" length="20"></property><property name="workDate" type="java.util.Date"></property><!-- 如果列名称为数据库关键字,需要用反引号或改列名。 --><property name="desc" column="`desc`" type="java.lang.String"></property></class></hibernate-mapping>

复合主键映射

// 复合主键类public class CompositeKeys implements Serializable{private String userName;private String address;   // .. get/set}public class User {// 名字跟地址,不会重复private CompositeKeys keys;private int age;}User.hbm.xml<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="cn.itcast.d_compositeKey" auto-import="true"><class name="User"><!-- 复合主键映射 --><composite-id name="keys"><key-property name="userName" type="string"></key-property><key-property name="address" type="string"></key-property></composite-id><property name="age" type="int"></property></class></hibernate-mapping>App.javapublic class App2 {private static SessionFactory sf;static  {// 创建sf对象sf = new Configuration().configure().addClass(User.class)  //(测试) 会自动加载映射文件:Employee.hbm.xml.buildSessionFactory();}//1. 保存对象@Testpublic void testSave() throws Exception {Session session = sf.openSession();Transaction tx = session.beginTransaction();// 对象CompositeKeys keys = new CompositeKeys();keys.setAddress("广州棠东");keys.setUserName("Jack");User user = new User();user.setAge(20);user.setKeys(keys);// 保存session.save(user);tx.commit();session.close();}@Testpublic void testGet() throws Exception {Session session = sf.openSession();Transaction tx = session.beginTransaction();//构建主键再查询CompositeKeys keys = new CompositeKeys();keys.setAddress("广州棠东");keys.setUserName("Jack");// 主键查询User user = (User) session.get(User.class, keys);// 测试输出if (user != null){System.out.println(user.getKeys().getUserName());System.out.println(user.getKeys().getAddress());System.out.println(user.getAge());}tx.commit();session.close();}}
1 0
原创粉丝点击