整合Spring3.1.2 与 Hibernate 4.1.8

首先准备整合jar:

Spring3.1.2:

org.springframework.aop-3.1.2.RELEASE.jar
org.springframework.asm-3.1.2.RELEASE.jar
org.springframework.aspects-3.1.2.RELEASE.jar
org.springframework.beans-3.1.2.RELEASE.jar
org.springframework.context.support-3.1.2.RELEASE.jar
org.springframework.context-3.1.2.RELEASE.jar
org.springframework.core-3.1.2.RELEASE.jar
org.springframework.expression-3.1.2.RELEASE.jar(使用表达式${})
org.springframework.jdbc-3.1.2.RELEASE.jar
org.springframework.orm-3.1.2.RELEASE.jar
org.springframework.transaction-3.1.2.RELEASE.jar

Hibernate4.1.8:

--------------required下面---------------
antlr-2.7.7.jar
dom4j-1.6.1.jar
hibernate-commons-annotations-4.0.1.Final.jar
hibernate-core-4.1.8.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
javassist-3.15.0-GA.jar
jboss-logging-3.1.0.GA.jar
jboss-transaction-api_1.1_spec-1.0.0.Final.jar
----------------------------

-----proxool-------
proxool-0.9.1.jar
proxool-cglib.jar

其他依赖包
aopalliance-1.0.jar
aspectjrt-1.7.0.jar
aspectjweaver-1.7.0.jar
commons-logging-1.1.1.jar

--数据库
mysql-connector-java-5.1.21.jar

整合示例:

UserModel:

  1. UserModel:
  2. package cn.sh.model;
  3. public class UserModel {
  4. private int id;
  5. private String username;
  6. private String password;
  7. --------getter & setter------
  8. }

user.hbm.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping>
  6. <class name="cn.sh.model.UserModel" table="user">
  7. <id name="id" column="id">
  8. <generator class="native" />
  9. </id>
  10. <property name="username" column="username" />
  11. <property name="password" column="password" />
  12. </class>
  13. </hibernate-mapping>

resources/jdbc.properties:

  1. proxool.maxConnCount=10
  2. proxool.minConnCount=5
  3. proxool.statistics=1m,15m,1h,1d
  4. proxool.simultaneousBuildThrottle=30
  5. proxool.trace=false
  6. jdbc.driverClassName=com.mysql.jdbc.Driver
  7. jdbc.url=jdbc:mysql://localhost:3306/ssh
  8. jdbc.username=root
  9. jdbc.password=admin

resources/applicationContext-common.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. http://www.springframework.org/schema/tx
  9. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  10. http://www.springframework.org/schema/aop
  11. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
  12. <!-- 引入配置文件 -->
  13. <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  14. <property name="locations">
  15. <list>
  16. <value>classpath:jdbc.properties</value>
  17. </list>
  18. </property>
  19. </bean>
  20. <!-- 数据源 -->
  21. <bean id="dataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
  22. <property name="targetDataSource">
  23. <bean class="org.logicalcobwebs.proxool.ProxoolDataSource">
  24. <property name="driver" value="${jdbc.driverClassName}" />
  25. <property name="driverUrl" value="${jdbc.url}" />
  26. <property name="user" value="${jdbc.username}" />
  27. <property name="password" value="${jdbc.password}" />
  28. <property name="maximumConnectionCount" value="${proxool.maxConnCount}" />
  29. <property name="minimumConnectionCount" value="${proxool.minConnCount}" />
  30. <property name="statistics" value="${proxool.statistics}" />
  31. <property name="simultaneousBuildThrottle" value="${proxool.simultaneousBuildThrottle}" />
  32. <property name="trace" value="${proxool.trace}" />
  33. </bean>
  34. </property>
  35. </bean>
  36. <!--  -->
  37. <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  38. <property name="dataSource" ref="dataSource" />
  39. <property name="mappingResources">
  40. <list>
  41. <value>cn/sh/model/user.hbm.xml</value>
  42. </list>
  43. </property>
  44. <property name="hibernateProperties">
  45. <value>
  46. hibernate.dialect=org.hibernate.dialect.HSQLDialect
  47. hibernate.show_sql=true
  48. </value>
  49. </property>
  50. </bean>
  51. <!-- 声明式事务 -->
  52. <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
  53. <property name="sessionFactory" ref="sessionFactory" />
  54. </bean>
  55. <aop:config>
  56. <aop:pointcut id="productServiceMethods" expression="execution(* cn.sh.service..*.*(..))" />
  57. <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" />
  58. </aop:config>
  59. <tx:advice id="txAdvice" transaction-manager="transactionManager">
  60. <tx:attributes>
  61. <tx:method name="increasePrice*" propagation="REQUIRED" />
  62. <tx:method name="someOtherBusinessMethod" propagation="REQUIRES_NEW" />
  63. <tx:method name="*" propagation="SUPPORTS" read-only="true" />
  64. </tx:attributes>
  65. </tx:advice>
  66. </beans>

整合测试:

    1. public class SpringHibernateTest {
    2. private SessionFactory sessionFactory;
    3. private ApplicationContext ctx;
    4. @Before
    5. public void setUp() {
    6. String[] configLocations = new String[] {"classpath:applicationContext-*.xml"};
    7. ctx = new ClassPathXmlApplicationContext(configLocations);
    8. sessionFactory = ctx.getBean("sessionFactory", SessionFactory.class);
    9. }
    10. @Test
    11. public void testSessionFactory(){
    12. System.out.println(sessionFactory);
    13. System.out.println(ctx.getBean("dataSource"));
    14. Session session = sessionFactory.openSession();
    15. UserModel model = new UserModel();
    16. model.setUsername("wangwu");
    17. model.setPassword("123456");
    18. session.save(model);
    19. }
    20. }

Spring3.1.2与Hibernate4.1.8整合的更多相关文章

  1. Struts2.3.4.1+Spring3.2.3+Hibernate4.1.9整合

    java教程|Struts2.3.4.1+Spring3.2.3+Hibernate4.1.9整合教程并测试成功一.创建项目二.搭建struts-2.3.4.11.struts2必须的Jar包(放到W ...

  2. springmvc+spring3+hibernate4框架简单整合,简单实现增删改查功能

    转自:https://blog.csdn.net/thinkingcao/article/details/52472252 C 所用到的jar包     数据库表 数据库表就不用教大家了,一张表,很简 ...

  3. Spring3 + Spring MVC+ Mybatis 3+Mysql 项目整合(注解及源码)

    Spring3 + Spring MVC+ Mybatis 3+Mysql 项目整合(注解及源码) 备注: 之前在Spring3 + Spring MVC+ Mybatis 3+Mysql 项目整合中 ...

  4. Struts2+Hibernate4+Spring4框架整合搭建Java项目原型

    收藏 http://www.cnblogs.com/mageguoshi/p/5850956.html Struts2+Hibernate4+Spring4框架整合搭建Java项目原型

  5. Struts2.3.4.1 + Spring3.1.2 + Hibernate4.1.6整合

    1. Jar包 2. web.xml配置 3. struts.xml配置 4. hibernate.cfg.xml配置 5. applicationContext.xml配置 6. log4j.pro ...

  6. 最新版本的Struts2+Spring4+Hibernate4三大框架整合(截止2014-10-15,提供源码下载)

    一. 项目名称:S2316S411H436 项目原型:Struts2.3.16 + Spring4.1.1 + Hibernate4.3.6 + Quartz2.2.1 源代码下载地址: 基本版:ht ...

  7. spring4+hibernate4+struts2项目整合的步骤及注意事项

    首先,在整合框架之前,我们需要知道Spring框架在普通Java project和Web project中是略有不同的. 这个不同地方就在于创建IOC容器实例的方式不同,在普通java工程中,可以在m ...

  8. hibernate4.3.8整合struts2过程中遇到的问题

    1.遇到的异常: Exception in thread "main" org.hibernate.service.spi.ServiceException: Unable to ...

  9. spring3.1的BeanFactory与Quartz1.8整合

    spring的applicationContext.xml配置文件: 加入 <bean id="myJob" class="org.springframework. ...

随机推荐

  1. 华三IRF的配置

    https://blog.csdn.net/VictoryKingLIU/article/details/79255901 拓扑结构 1 配置成员编号(重启) 2 配置成员优先级(大的主设备) 3 配 ...

  2. Java异常处理中的恢复模型

    异常处理理论上有两种基本模型.Java支持终止模型,在这种模型中,假设错误非常关键,以至于程序无法返回到异常发生的地方继续执行.一旦异常被抛出,就表明错误已无法挽回,也不能回来继续执行.长久以来,尽管 ...

  3. 深度学习基础系列(九)| Dropout VS Batch Normalization? 是时候放弃Dropout了

    Dropout是过去几年非常流行的正则化技术,可有效防止过拟合的发生.但从深度学习的发展趋势看,Batch Normalizaton(简称BN)正在逐步取代Dropout技术,特别是在卷积层.本文将首 ...

  4. Gumbel-Softmax Trick和Gumbel分布

      之前看MADDPG论文的时候,作者提到在离散的信息交流环境中,使用了Gumbel-Softmax estimator.于是去搜了一下,发现该技巧应用甚广,如深度学习中的各种GAN.强化学习中的A2 ...

  5. NetCore+Dapper WebApi架构搭建(二):底层封装

    看下我们上一节搭建的架构,现在开始从事底层的封装 1.首先需要一个实体的接口IEntity namespace Dinner.Dapper { public interface IEntity< ...

  6. 深入理解ajax系列第三篇

    前面的话 我们接收到的响应主体类型可以是多种形式的,包括字符串String.ArrayBuffer对象.二进制Blob对象.JSON对象.javascirpt文件及表示XML文档的Document对象 ...

  7. 深入理解ajax系列第四篇

    前面的话 现代Web应用中频繁使用的一项功能就是表单数据的序列化,XMLHttpRequest 2级为此定义了FormData类型.FormData为序列化表单以及创建与表单格式相同的数据提供了便利. ...

  8. QT学习笔记7:C++函数默认参数

    C++中允许为函数提供默认参数,又名缺省参数. 使用默认参数时的注意事项: ① 有函数声明(原型)时,默认参数可以放在函数声明或者定义中,但只能放在二者之一.建议放在函数声明中. double sqr ...

  9. 某谷 P5153 简单的函数

    题面在这里 个人感觉这个题可以被打表随便艹过,当然我不是这么做的... 虽然n可达10^18,但随便分析一下就可以发现f(n)是极小的,因为f(n)一步就可以跳到f(前100),不信你算一下前100个 ...

  10. 转MySQL常见错误分析与解决方法总结

    一.Can't connect to MySQL server on 'localhost' (10061)翻译:不能连接到 localhost 上的mysql分析:这说明“localhost”计算机 ...