全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

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

之前的文章都是讲解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 


相关文章: mc皮肤壁纸制作器,苹果平板怎么设置自己想要的壁纸我的世界?  齐河建站公司:营销型网站建设与SEO优化双核驱动策略  个人网站制作流程图片大全,个人网站如何注销?  建站一年半SEO优化实战指南:核心词挖掘与长尾流量提升策略  Swift开发中switch语句值绑定模式  宝塔新建站点为何无法访问?如何排查?  如何确保FTP站点访问权限与数据传输安全?  TestNG的testng.xml配置文件怎么写  如何有效防御Web建站篡改攻击?  太平洋网站制作公司,网络用语太平洋是什么意思?  儿童网站界面设计图片,中国少年儿童教育网站-怎么去注册?  赚钱网站制作软件,建一个网站怎样才能赚钱?是如何盈利的?  如何用PHP工具快速搭建高效网站?  如何注册花生壳免费域名并搭建个人网站?  如何选择CMS系统实现快速建站与SEO优化?  阿里云高弹*务器配置方案|支持分布式架构与多节点部署  高端建站三要素:定制模板、企业官网与响应式设计优化  如何零成本快速生成个人自助网站?  网站制作的方法有哪些,如何将自己制作的网站发布到网上?  建站之星如何开启自定义404页面避免用户流失?  已有域名能否直接搭建网站?  简单实现Android文件上传  网站视频怎么制作,哪个网站可以免费收看好莱坞经典大片?  杭州银行网站设计制作流程,杭州银行怎么开通认证方式?  电影网站制作价格表,那些提供免费电影的网站,他们是怎么盈利的?  香港服务器部署网站为何提示未备案?  西安市网站制作公司,哪个相亲网站比较好?西安比较好的相亲网站?  重庆市网站制作公司,重庆招聘网站哪个好?  制作营销网站公司,淘特是干什么用的?  建站三合一如何选?哪家性价比更高?  广州网站制作的公司,现在专门做网站的公司有没有哪几家是比较好的,性价比高,模板也多的?  制作网站的过程怎么写,用凡科建站如何制作自己的网站?  建站之星代理如何优化在线客服效率?  深圳网站制作案例,网页的相关名词有哪些?  如何制作新型网站程序文件,新型止水鱼鳞网要拆除吗?  如何通过远程VPS快速搭建个人网站?  如何用y主机助手快速搭建网站?  深圳企业网站制作设计,在深圳如何网上全流程注册公司?  如何在Golang中处理模块冲突_解决依赖版本不兼容问题  建站之星图片链接生成指南:自助建站与智能设计教程  制作宣传网站的软件,小红书可以宣传网站吗?  建站DNS解析失败?如何正确配置域名服务器?  网站制作知乎推荐,想做自己的网站用什么工具比较好?  如何快速生成ASP一键建站模板并优化安全性?  如何在Golang中使用replace替换模块_指定本地或远程路径  网站制作网站,深圳做网站哪家比较好?  桂林网站制作公司有哪些,桂林马拉松怎么报名?  常州自助建站费用包含哪些项目?  如何选择高效可靠的多用户建站源码资源?  jQuery 常见小例汇总 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。