zoukankan      html  css  js  c++  java
  • Hibernate入门(1)

    Hibernate是持久性框架(数据访问层使用),【hibernate最终执行的也是jdbc代码!】

    一、Hibernate框架

    ORM概念(对象关系映射)

    O  Object  对象

    R  Relation  关系

    M  Mapping 映射

    (1)ORM解决的问题?

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

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

      想要做到上面的2点必须要有映射。

    (2)Hibernate与ORM的关系?

        Hibernate是ORM的实现!ORM是一种思想。

    二、组件学习步骤

    (1)下载源码,引入jar文件

    (2)配置

    (3)查看api

     三、Hibernate第一个案例

    搭建Hibernate环境的步骤:

      1、下载源码

             版本:hibernate-distribution-3.6.0.Final

      2、引入jar包

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

      3、编写对象,以及对象的映射

        (1)Employee.java             对象

             (2)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.employ">
        
        <class name="Employee" table="employee">
            
            <!-- 主键 ,映射-->
            <id name="empId" column="id">
                <generator class="native"/>
            </id>
            
            <!-- 非主键,映射 -->
            <property name="empName" column="name"></property>
            <property name="empDate" column="date"></property>
            
        </class>
    
    </hibernate-mapping>

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

        (1) 数据库连接配置

             (2)加载所用的映射(*.hbm.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">
            <![CDATA[jdbc:mysql:///bank?useUnicode=true&amp;characterEncoding=utf8]]>
            </property>
            <property name="hibernate.connection.username">root</property>
            <property name="hibernate.connection.password">123456</property>
            <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
            
            <property name="hibernate.show_sql">true</property>
            <!-- 加载所有映射 -->
            <mapping resource="cn/itcast/employ/Employee.hbm.xml"/>
        </session-factory>
    </hibernate-configuration>

      5、App.java测试

    package cn.itcast.employ;
    
    import java.util.Date;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.Configuration;
    import org.junit.Test;
    
    public class App {
        @Test
        public void testHello() {
            //对象
            Employee emp = new Employee();
            emp.setEmpName("甘兴瑞");
            emp.setEmpDate(new Date());
            
            //获取加载配置文件的管理类对象
            Configuration config = new Configuration();
            config.configure();//默认加载src/hibernate.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();
        }
    }

    数据库名:bank,  表名:employee->字段id  name  date.

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

    更新:

             (1)session.save(obj);   保存一个对象

             (2)session.update(emp);  更新一个对象

             (3)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事务对象

  • 相关阅读:
    日报10.11
    日报10.9
    日报10.8
    日报10.7
    换马甲啦
    CSP2019知识点整理
    字符logo存档
    QHDYZ模拟赛20191027 提前透题
    数竞大佬jhc的三角函数复习题
    IO流
  • 原文地址:https://www.cnblogs.com/h-g-f-s123/p/6376033.html
Copyright © 2011-2022 走看看