• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >Java > spring+hibernate 两种整合方式配置文件的方法

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

作者:QH_JAVA 字体:[增加 减小] 来源:互联网 时间:2017-05-28

QH_JAVA 通过本文主要向大家介绍了spring hibernate配置,spring配置hibernate4,spring hibernate,spring hibernate事务,spring hibernate.jar等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

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

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> 

</div>

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

注解的这种方式,直接扫描bean包就可以,剩下的对

分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • Spring Hibernate实现分页功能
  • spring+hibernate 两种整合方式配置文件的方法
  • Spring Hibernate实现分页功能
  • spring+hibernate 两种整合方式配置文件的方法

相关文章

  • 2017-05-28Java中Builder模式的实现详解
  • 2017-05-28Java正则表达式之split()方法实例详解
  • 2017-05-28javamail实现注册激活邮件
  • 2017-05-28SpringBoot拦截器实现对404和500等错误的拦截
  • 2017-05-28详解hibernate双向多对多关联映射XML与注解版
  • 2017-05-28java 关键字super详解及用法
  • 2017-05-28java实现ftp文件上传下载功能
  • 2017-05-28SpringBoot拦截器的使用小结
  • 2017-05-28java 获取用户的MAC地址多种方法实例详解
  • 2017-05-28springMVC4之强大类型转换器实例解析

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • java读取txt文件代码片段
    • 简单实现Java验证码功能
    • Java正则验证电话,手机,邮箱,日期,金额的方法示例
    • Java使用NioSocket手动实现HTTP服务器
    • listview点击无效的处理方法(推荐)
    • Java 中 Reference用法详解
    • 详解Spring Boot 自定义PropertySourceLoader
    • Java枚举类型enum的详解及使用
    • java中多态概念、实现原理详解
    • Java中LocalCache本地缓存实现代码

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有