1. 程式人生 > >hibernate框架的學習(二)

hibernate框架的學習(二)

drive 所有 設置 builds phone font mat not lec

hibernate的核心配置和API

一:核心配置分為三大部分 必須的配置可選的配置和引入映射文件。

1.必須的配置 連接數據庫的參數:驅動類 url路徑 用戶名 密碼 方言

<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql:///hibernate_day01</property>
        <
property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">000000</property> <!-- 他的方言 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

  2.可選的配置 顯示sql 整理sql語句的格式

<!-- 可選的配置 -->
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>

  3.引入映射文件

<mapping resource="com/itheima/domain/Customer.hbm.xml"/>

  二 映射的配置

  首先介紹裏面的標簽的屬性

  

class標簽的配置 作用:標簽用來建立類與表的映射關系


屬性
name:類的全路徑
talbe數據庫的表名
id標簽的配置 作用:標簽用來建立類中的屬性與表中的主鍵的對應關系


    name:類的屬性名,只要是name就去類中去找
    column類中的字段名
property標簽的設置 作用:建立類中的普通屬性與表的聯系

    name:類中 的屬性名
    column類中的字段名
l    ength長度 type 類型 not-null設置非空 unique設置唯一

<hibernate-mapping>
    <!-- 建立類與表的映射關系 -->
                        <!--當時這裏後面少了個引號,直接導致後面的id變成藍色 -->
    <class name="com.itheima.domain.Customer" table="cst_customer">
            <id  name="cust_id" column="cust_id">
                <!-- <generator class="native"/>     -->    
                <generator class="native"/>
            </id>
            
            <!-- 與普通字段建立對應關系 -->
            <property name="cust_name" column="cust_name"/>
            <property name="cust_source" column="cust_source"/>
            <property name="cust_industry" column="cust_industry"/>
            <property name="cust_level" column="cust_level"/>
            <property name="cust_phone" column="cust_phone"/>
            <property name="cust_mobile" column="cust_mobile"/>
            
    </class>
</hibernate-mapping>

  三 核心的API

  Hibernate的API一共有6個,分別為:Session、SessionFactory、Transaction、Query、Criteria和Configuration。通過這些接口,可以對持久化對象進行存取、事務控制。

   1 SessionFactory

     SessionFactory接口負責初始化Hibernate。它充當數據存儲源的代理,並負責創建Session對象。這裏用到了工廠模式。需要註意的是SessionFactory並不是輕量級的,因為一般情況下,一個項目通常只需要一個SessionFactory就夠,當需要操作多個數據庫時,可以為每個數據庫指定一個SessionFactory。

hibernate的二級緩存現在在企業中已經不用了,用redis來替換他了。 sessionFactory一個程序只須創建一次就行,那麽我們就抽取一個工具類,這樣效率會提升。

  抽取的工具類

public class HibernateUtils {
    public static final Configuration configuration;
    public static final SessionFactory sessionFactory;
    //寫一個靜態代碼快
    static{
        configuration=new Configuration().configure();
        sessionFactory=configuration.buildSessionFactory();
    }
    public static Session openSession(){
        return sessionFactory.openSession();
    }
}

   2 Configuration

    作用:加載核心配置文件

   3 Session :類似JDBC的connection對象是一個連接對象,是數據庫交互的橋梁

      get方法和load方法的區別 (面試經常會問)
        get 采用立即加載 查詢到的是對象本身 找不到對象的時候會返回空
        load就不一樣了 采用的是延遲加載(Lazy懶加載) 查詢後返回的是代理對象 查詢不到一個對象的時候會拋異常
        在開發中用的比較多的還是get

  

     
      //上面的是get方法
     Customer customer = session.get(Customer.class, 11l);//這裏是long類型 System.out.println(customer);
      //下面的是load方法 Customer customer
= session.load(Customer.class, 4l); System.out.println(customer);

  更新操作

      //第二種方式是先查詢,再更新,推薦這種方式
        Customer customer = session.get(Customer.class,6l);
        customer.setCust_name("王宏");
        session.update(customer);

   刪除操作

@Test
    public void demo4(){
        Session session = HibernateUtils.openSession();
        Transaction beginTransaction = session.beginTransaction();
        //先查詢再刪除,級聯刪除
        Customer customer = session.get(Customer.class,4l);
        session.delete(customer);
        beginTransaction.commit();
        session.close();
    }

  查詢所有

  

@Test
    //查詢所有
    public void demo5(){
        Session session = HibernateUtils.openSession();
        Transaction beginTransaction = session.beginTransaction();
        Query query = session.createQuery("from Customer");    //面向對象
        List list = query.list();
        for (Object object : list) {
            System.out.println(object);
        }
        beginTransaction.commit();
        session.close();
    }

  

hibernate框架的學習(二)