Spring3.1.2与Hibernate4.1.8整合

时间:2022-12-08 17:01:06

整合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. }