Hibernate5的学习笔记(二)

Hibernate5的实际运用

就像奶茶大续杯一样,让我们继续和Hibernate打交道,捧好温热的奶茶在这个寒冬里,继续Hibernate的学习。
1、我们的配置hibernate.cfg.xml文件的主要作用就是创建一个sessionFactory,其会通过property配置一些数据库的连接,

也会配置Hibernate的相关属性比如方言、自动创建表机制、格式化sql等,还有就是持久化类的所在路径。

当只是用Hibernate的时候主要是用Hibernate从配置文件里获取sessionFactory,再从sessionFactory中取session,再用session对持久化对象进行操作。

2、hibernate默认加载就是名为hibernate.cfg.xml的配置文件,这是在源代码里面的

public Configuration configure() throws HibernateException {
     //1.StandardServiceRegistryBuilder.DEFAULT_CFG_RESOURCE_NAME的值是"hibernate.cfg.xml"
        return configure( StandardServiceRegistryBuilder.DEFAULT_CFG_RESOURCE_NAME );
    }

3、配置SeesionFactory的方式有两种,一种是通过DataSource来配置sessionFactory,另一种是通过Hibernate.cxg.xml来配置。通过DataSource的方法,是Datasource数据源是注入给sessionfactory的SessionFactory是基于dataSource上建立的。

4、在单独使用hibernate的方法时,我们应该这样做

  • a、配置好applicationContext.xml一般放在对应的Service的实现包下xxx\src\main\resources\applicationContext.xml
  • <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="
    		http://www.springframework.org/schema/beans
    		http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    		http://www.springframework.org/schema/context
    		http://www.springframework.org/schema/context/spring-context-4.2.xsd
    		http://www.springframework.org/schema/aop
    		http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
    		http://www.springframework.org/schema/tx
    		http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
    
        <!-- 用于持有ApplicationContext,可以使用SpringContextHolder.getBean('xxxx')的静态方法得到spring bean对象
         该工具类主要用于:那些没有归入spring框架管理的类却要调用spring容器中的bean提供的工具类。
    
          在spring中要通过IOC依赖注入来取得对应的对象,但是该类通过实现ApplicationContextAware接口,
          以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext.
         -->
        <bean id="springContextHolder" class="com.mp.jdbc.context.SpringContextHolder" lazy-init="false"/>
    
        <!-- 扫描路径不扫描controller
        如果某个类的头上带有特定的注解@Component,@Repository,@Service,@Controller,就会将这个对象作为Bean注册进Spring容器。
        启用了对类包进行扫描以实施注释驱动 Bean 定义的功能,同时还启用了注释驱动自动注入的功能
        -->
        <context:component-scan base-package="com.test">
            <context:exclude-filter type="annotation"
                                    expression="org.springframework.stereotype.Controller"/>
        </context:component-scan>
    
        <!-- 自动注入properties文件中
        DecryptPropertyPlaceholderConfigurer在加载上下文的时候,可以在XML配置文件中加入外部属性文件并加密,
        当然也可以指定外部文件的编码
        location指向配置文件的位置
         设置为lazy的bean将不会在ApplicationContext启动时提前被实例化,而是第一次向容器通过getBean索取bean时实例化的。-->
        <bean id="property
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值