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类中的字段名
               length长度 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();
    }

 

 

 

 

  

posted @ 2019-01-29 21:20  bao666  阅读(319)  评论(0编辑  收藏  举报