分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

hibernate框架的学习(二)

发布时间:2023-09-06 02:31责任编辑:熊小新关键词:暂无标签

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框架的学习(二)

原文地址:https://www.cnblogs.com/bao6/p/10335887.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved