zoukankan      html  css  js  c++  java
  • Spring3.1.2与Hibernate4.1.8整合

    整合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.   
    3. package cn.sh.model;  
    4.   
    5. public class UserModel {  
    6.     private int id;  
    7.     private String username;  
    8.     private String password;  
    9.       
    10.     --------getter & setter------  
    11. }  

    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.   
    7. jdbc.driverClassName=com.mysql.jdbc.Driver  
    8. jdbc.url=jdbc:mysql://localhost:3306/ssh  
    9. jdbc.username=root  
    10. 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.     <!-- 引入配置文件 -->  
    14.     <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
    15.         <property name="locations">  
    16.             <list>  
    17.                 <value>classpath:jdbc.properties</value>  
    18.             </list>  
    19.         </property>  
    20.     </bean>  
    21.        
    22.      <!-- 数据源 -->  
    23.     <bean id="dataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">  
    24.         <property name="targetDataSource">  
    25.             <bean class="org.logicalcobwebs.proxool.ProxoolDataSource">  
    26.                 <property name="driver" value="${jdbc.driverClassName}" />  
    27.                 <property name="driverUrl" value="${jdbc.url}" />  
    28.                 <property name="user" value="${jdbc.username}" />  
    29.                 <property name="password" value="${jdbc.password}" />  
    30.                 <property name="maximumConnectionCount" value="${proxool.maxConnCount}" />  
    31.                 <property name="minimumConnectionCount" value="${proxool.minConnCount}" />  
    32.                 <property name="statistics" value="${proxool.statistics}" />  
    33.                 <property name="simultaneousBuildThrottle" value="${proxool.simultaneousBuildThrottle}" />  
    34.                 <property name="trace" value="${proxool.trace}" />  
    35.             </bean>  
    36.         </property>  
    37.     </bean>  
    38.       
    39.     <!--  -->  
    40.     <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
    41.         <property name="dataSource" ref="dataSource" />  
    42.         <property name="mappingResources">  
    43.             <list>  
    44.                 <value>cn/sh/model/user.hbm.xml</value>  
    45.             </list>  
    46.         </property>  
    47.         <property name="hibernateProperties">  
    48.             <value>  
    49.                 hibernate.dialect=org.hibernate.dialect.HSQLDialect  
    50.                 hibernate.show_sql=true  
    51.             </value>  
    52.         </property>  
    53.     </bean>  
    54.       
    55.     <!-- 声明式事务 -->  
    56.     <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
    57.         <property name="sessionFactory" ref="sessionFactory" />  
    58.     </bean>  
    59.       
    60.     <aop:config>  
    61.         <aop:pointcut id="productServiceMethods" expression="execution(* cn.sh.service..*.*(..))" />  
    62.         <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" />  
    63.     </aop:config>  
    64.       
    65.     <tx:advice id="txAdvice" transaction-manager="transactionManager">  
    66.         <tx:attributes>  
    67.             <tx:method name="increasePrice*" propagation="REQUIRED" />  
    68.             <tx:method name="someOtherBusinessMethod" propagation="REQUIRES_NEW" />  
    69.             <tx:method name="*" propagation="SUPPORTS" read-only="true" />  
    70.         </tx:attributes>  
    71.     </tx:advice>  
    72.       
    73.       
    74. </beans>  


    整合测试:

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

  • 相关阅读:
    数据算法之汉诺塔
    Mvc模板页
    mvc局部视图
    Area区域
    mvc之文件下载
    MVC过滤器
    MVC_Ajax请求
    MVC之校验
    Json&Razor&控制器
    抓包分析,tcpdump 和 wireshark 配合使用的简单尝试
  • 原文地址:https://www.cnblogs.com/interdrp/p/3439635.html
Copyright © 2011-2022 走看看