spring+hibernate 两种整合方式配置文件的方法

发布时间 - 2026-01-11 00:31:58    点击率:

之前的文章都是讲解springmvc+spring+mybatis 的整合,而很少有springmvc+spring+hibernate 因为工作的需要,最近在使用hibernate 所以下面我们来看看 spring整合hibernate的配置文件,这里只说spring+hibernate 的配置文件而不说springmvc 因为这些是不用变的。

spring整合hibernate 有两种方式 1、注解方式 2、xml方式实现

1、注解方式实现:

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:aop="http://www.springframework.org/schema/aop" 
   xmlns:tx="http://www.springframework.org/schema/tx" 
   xmlns:context="http://www.springframework.org/schema/context" 
   xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx.xsd 
     http://www.springframework.org/schema/aop 
     http://www.springframework.org/schema/aop/spring-aop.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context.xsd"> 
  <context:component-scan base-package="com.test" /> 
  <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
   <property name="locations"> 
    <list> 
      <value>classpath:jdbc.properties</value> 
    </list> 
   </property> 
  </bean> 
  <bean id="c3p0DataSource" destroy-method="close" 
    class="com.mchange.v2.c3p0.ComboPooledDataSource"> 
    <property name="driverClass" value="${driverClass}" /> 
    <property name="jdbcUrl" value="${url}" /> 
    <property name="user" value="${user}" /> 
    <property name="password" value="${password}" /> 
    <property name="initialPoolSize" value="${initialPoolSize}" /> 
    <property name="minPoolSize" value="${minPoolSize}" /> 
    <property name="maxPoolSize" value="${maxPoolSize}" /> 
    <property name="maxIdleTime" value="${maxIdleTime}" /> 
  </bean>          
  <bean id="sessionFactory" 
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="c3p0DataSource" /> 
    <property name="packagesToScan"> 
      <list> 
        <value>com.test.bean</value> 
      </list> 
    </property> 
    <property name="hibernateProperties"> 
      <props> 
        <prop key="hibernate.dialect">${dialect}</prop> 
        <prop key="hibernate.show_sql">${show_sql}</prop> 
        <prop key="hibernate.format_sql">${format_sql}</prop> 
        <prop key="hibernate.use_sql_commants">${use_sql_comments}</prop> 
        <prop key="hibernate.hbm2ddl.auto">${hbm2ddl.auto}</prop> 
      </props> 
    </property> 
  </bean> 
  <bean id="txManager" 
    class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory" /> 
  </bean> 
  <tx:advice id="txAdvice" transaction-manager="txManager"> 
    <tx:attributes> 
      <tx:method name="get*" read-only="true" /> 
      <tx:method name="*" /> 
    </tx:attributes> 
  </tx:advice> 
  <aop:config> 
    <aop:pointcut id="bizMethods" expression="execution(* com.test.biz.*.*(..))" /> 
    <aop:advisor advice-ref="txAdvice" pointcut-ref="bizMethods" /> 
  </aop:config> 
</beans> 

2.xml方式实现

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: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.xsd 
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx.xsd 
           http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop.xsd"> 
      
  <!-- 让spring 去读取指定路径下的资源文件 --> 
  <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
   <property name="locations" value="classpath:jdbc.properties"/> 
  </bean> 
   
  <!-- 配置c3p0连接池 --> 
  <bean id="c3p0Source" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> 
   <property name="driverClass" value="${driverClass}" /> 
   <property name="jdbcUrl" value="${url}" /> 
   <property name="user" value="${user}" /> 
   <property name="password" value="${password}" /> 
   <property name="initialPoolSize" value="${initialPoolSize}" /> 
   <property name="minPoolSize" value="${minPoolSize}" /> 
   <property name="maxPoolSize" value="${maxPoolSize}" /> 
   <property name="maxIdleTime" value="${maxIdleTime}" /> 
  </bean> 
   
  <!-- 配置SessionFactory --> 
  <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
   <property name="dataSource" ref="c3p0Source" /> 
   <property name="mappingResources"> 
     <list> 
      <value>/com/cdzg/spring/bean/User.hbm.xml</value> 
     </list> 
   </property> 
   <property name="hibernateProperties"> 
    <props> 
        <prop key="hibernate.dialect">${dialect}</prop> 
        <prop key="hibernate.hbm2ddl.auto">${hbm2ddl.auto}</prop> 
        <prop key="hibernate.show_sql">${show_sql}</prop> 
        <prop key="hibernate.format_sql">${format_sql}</prop> 
        <prop key="hibernate.use_sql_comments">${use_sql_comments}</prop> 
      </props> 
   </property> 
  </bean> 
   
  <!-- 配置事务管理器 --> 
  <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
   <property name="sessionFactory" ref="sessionFactory" /> 
  </bean> 
   
  <!-- 定义事务通知 --> 
  <tx:advice id="txAdvice" transaction-manager="txManager"> 
   <tx:attributes> 
    <tx:method name="get*" read-only="true"/> 
    <tx:method name="*"/> 
   </tx:attributes> 
  </tx:advice> 
    
   <!-- 定义事务切面,并应用事务通知 -->   
   <aop:config> 
   <aop:pointcut id="xxxBizImpl" expression="execution(* com.cdzg.spring.biz.*.*(..))"/> 
   <aop:advisor pointcut-ref="xxxBizImpl" advice-ref="txAdvice"/> 
   </aop:config> 
      
  <bean id="userDaoImpl" class="com.cdzg.spring.dao.impl.UserDaoImpl"> 
    <property name="sessionFactory" ref="sessionFactory" /> 
  </bean> 
  <bean id="userBizImpl" class="com.cdzg.spring.biz.impl.UserBizImpl"> 
    <property name="userDao" ref="userDaoImpl" /> 
  </bean> 
  <bean id="userAction" class="com.cdzg.spring.web.actions.UserAction"> 
    <property name="userBiz" ref="userBizImpl" /> 
  </bean> 
</beans> 

两种配置最大的区别就是注解方式不用在写O/R映射配置文件而xml方式实现的要配置O/R映射配置文件

注解的这种方式,直接扫描bean包就可以,剩下的对应关系由框架完成

而xml配置方式要配置O/R 映射文件并在这里指定文件,如果多的话可以使用通配符 "*"

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


# spring  # hibernate整合  # spring和hibernate  # 配置hibernate  # Spring整合SpringMVC + Mybatis基础框架的配置文件详解  # springboot的yml配置文件通过db2的方式整合mysql的教程  # spring boot-2.1.16整合swagger-2.9.2 含yml配置文件的代码详解  # Spring 整合多个配置文件的方法  # spring与mybatis整合配置文件  # Spring配置文件的拆分和整合过程分析  # 配置文件  # 都是  # 两种  # 并在  # 来看看  # 可以使用  # 管理器  # 有两种  # 用在  # 只说  # 大家多多  # 就可以  # 连接池  # scan  # base  # component  # bean  # factory  # package  # test 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: Linux系统运维自动化项目教程_Ansible批量管理实战  如何在服务器上配置二级域名建站?  教学论文网站制作软件有哪些,写论文用什么软件 ?  如何用JavaScript实现文本编辑器_光标和选区怎么处理  浅谈javascript alert和confirm的美化  微信小程序 配置文件详细介绍  js实现点击每个li节点,都弹出其文本值及修改  如何用IIS7快速搭建并优化网站站点?  网站建设要注意的标准 促进网站用户好感度!  Laravel定时任务怎么设置_Laravel Crontab调度器配置  小视频制作网站有哪些,有什么看国内小视频的网站,求推荐?  如何快速搭建高效WAP手机网站吸引移动用户?  今日头条AI怎样推荐抢票工具_今日头条AI抢票工具推荐算法与筛选【技巧】  HTML5段落标签p和br怎么选_文本排版常用标签对比【解答】  西安市网站制作公司,哪个相亲网站比较好?西安比较好的相亲网站?  如何用AWS免费套餐快速搭建高效网站?  如何用wdcp快速搭建高效网站?  如何在Tomcat中配置并部署网站项目?  百度浏览器网页无法复制文字怎么办 百度浏览器复制修复  Android使用GridView实现日历的简单功能  阿里云高弹*务器配置方案|支持分布式架构与多节点部署  html文件怎么打开证书错误_https协议的html打开提示不安全【指南】  Windows10如何更改计算机工作组_Win10系统属性修改Workgroup  如何续费美橙建站之星域名及服务?  如何在阿里云虚拟机上搭建网站?步骤解析与避坑指南  通义万相免费版怎么用_通义万相免费版使用方法详细指南【教程】  微博html5版本怎么弄发超话_超话进入入口及发帖格式要求【教程】  Mybatis 中的insertOrUpdate操作  如何用VPS主机快速搭建个人网站?  javascript事件捕获机制【深入分析IE和DOM中的事件模型】  独立制作一个网站多少钱,建立网站需要花多少钱?  Laravel Asset编译怎么配置_Laravel Vite前端构建工具使用  如何在IIS中新建站点并配置端口与IP地址?  大学网站设计制作软件有哪些,如何将网站制作成自己app?  Python自然语言搜索引擎项目教程_倒排索引查询优化案例  韩国代理服务器如何选?解析IP设置技巧与跨境访问优化指南  如何在Windows虚拟主机上快速搭建网站?  javascript中的try catch异常捕获机制用法分析  Firefox Developer Edition开发者版本入口  javascript中闭包概念与用法深入理解  Laravel怎么使用Blade模板引擎_Laravel模板继承与Component组件复用【手册】  如何选择PHP开源工具快速搭建网站?  如何快速查询网站的真实建站时间?  Laravel如何操作JSON类型的数据库字段?(Eloquent示例)  详解免费开源的.NET多类型文件解压缩组件SharpZipLib(.NET组件介绍之七)  Laravel Sail是什么_基于Docker的Laravel本地开发环境Sail入门  如何快速搭建高效服务器建站系统?  js代码实现下拉菜单【推荐】  Laravel中间件如何使用_Laravel自定义中间件实现权限控制  详解jQuery中的事件