配置hibernate数据源的5种方法

文章摘自:https://blog.csdn.net/zljjava/article/details/7535710


Spring配置文件中关于事务配置总是由三个组成部分,DataSource、TransactionManager和代理机制这三部分,无论是那种配置方法,一般变化的只是代理机制这块!

 

首先我创建了两个类,一个接口一个实现:

Java代码   收藏代码
  1. package com.dao;  
  2. public interface UserDao {  
  3.     public void getUser();    
  4. }  

 

实现:

Java代码   收藏代码
  1. package com.dao.impl;  
  2. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;  
  3. import com.dao.UserDao;  
  4. public class UserDaoImpl extends HibernateDaoSupport implements UserDao {  
  5.     public void getUser(){        
  6.     }     
  7. }  

 

 

第一种:每个Bean都有一个代理:

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 http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  7.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd  
  8.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">  
  9.       
  10.     <bean id="dataSource"  
  11.         class="org.apache.commons.dbcp.BasicDataSource"  
  12.         destroy-method="close">  
  13.         <property name="driverClassName" value="com.mysql.jdbc.Driver" />  
  14.         <property name="url"  
  15.             value="jdbc:mysql://192.168.0.244:3306/test?useUnicode=true&characterEncoding=UTF-8" />  
  16.         <property name="username" value="root" />  
  17.         <property name="password" value="root" />  
  18.           
  19.         <property name="initialSize" value="10" />  
  20.           
  21.         <property name="maxActive" value="10" />  
  22.           
  23.         <property name="maxIdle" value="20" />  
  24.           
  25.         <property name="minIdle" value="10" />  
  26.         <property name="defaultAutoCommit" value="true" />  
  27.     bean>  
  28.       
  29.     <bean id="sessionFactory"  
  30.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  31.         <property name="dataSource" ref="dataSource" />  
  32.         <property name="mappingLocations">  
  33.             <list>  
  34.                 <value>classpath:/com/nms/entity/**/*.hbm.xmlvalue>  
  35.             list>  
  36.         property>  
  37.         <property name="hibernateProperties">  
  38.             <props>  
  39.                 <prop key="hibernate.dialect">  
  40.                     org.hibernate.dialect.MySQL5Dialect  
  41.                 prop>  
  42.                 <prop key="hibernate.show_sql">trueprop>  
  43.                 <prop key="hibernate.format_sql">trueprop>  
  44.             props>  
  45.         property>  
  46.     bean>      
  47.         
  48.     <bean id="transactionManager"  
  49.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  50.         <property name="sessionFactory" ref="sessionFactory" />  
  51.     bean>      
  52.       
  53.     <bean id="userDaoAgency" class="com.dao.impl.UserDaoImpl">  
  54.         <property name="sessionFactory" ref="sessionFactory" />  
  55.     bean>      
  56.     <bean id="userDao"    
  57.         class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">    
  58.             
  59.         <property name="transactionManager" ref="transactionManager" />       
  60.         <property name="target" ref="userDaoAgency" />    
  61.         <property name="proxyInterfaces" value="com.dao.UserDao" />  
  62.             
  63.         <property name="transactionAttributes">    
  64.             <props>    
  65.                 <prop key="*">PROPAGATION_REQUIREDprop>  
  66.             props>    
  67.         property>    
  68.     bean>  
  69. beans>  

 

 

第二种:所有Bean共享一个代理:

Java代码   收藏代码
  1. "1.0" encoding="UTF-8"?>  
  2. "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 http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  7.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd  
  8.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">  
  9.       
  10.     "dataSource"  
  11.         class="org.apache.commons.dbcp.BasicDataSource"  
  12.         destroy-method="close">  
  13.         "driverClassName" value="com.mysql.jdbc.Driver" />  
  14.         "url"  
  15.             value="jdbc:mysql://192.168.0.244:3306/test?useUnicode=true&characterEncoding=UTF-8" />  
  16.         "username" value="root" />  
  17.         "password" value="root" />  
  18.           
  19.         "initialSize" value="10" />  
  20.           
  21.         "maxActive" value="10" />  
  22.           
  23.         "maxIdle" value="20" />  
  24.           
  25.         "minIdle" value="10" />  
  26.         "defaultAutoCommit" value="true" />  
  27.       
  28.       
  29.     "sessionFactory"  
  30.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  31.         "dataSource" ref="dataSource" />  
  32.         "mappingLocations">  
  33.               
  34.                 classpath:/com/nms/entity/**/*.hbm.xml  
  35.               
  36.           
  37.         "hibernateProperties">  
  38.               
  39.                 "hibernate.dialect">  
  40.                     org.hibernate.dialect.MySQL5Dialect  
  41.                   
  42.                 "hibernate.show_sql">true  
  43.                 "hibernate.format_sql">true  
  44.               
  45.           
  46.       
  47.       
  48.     "transactionManager"  
  49.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  50.         "sessionFactory" ref="sessionFactory" />  
  51.       
  52.        
  53.     "base"  
  54.         class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"  
  55.         lazy-init="true" abstract="true">  
  56.           
  57.         "transactionManager" ref="transactionManager" />  
  58.           
  59.         "transactionAttributes">  
  60.               
  61.                 "*">PROPAGATION_REQUIRED  
  62.               
  63.           
  64.       
  65.       
  66.     "userDao"  
  67.         class="com.dao.impl.UserDaoImpl">  
  68.         "sessionFactory" ref="sessionFactory" />  
  69.       
  70.       
  71.     "userDaoAgency" parent="base">  
  72.         "target" ref="userDao" />  
  73.       
  74.   

 

 

第三种:拦截器:

Java代码   收藏代码
  1. "1.0" encoding="UTF-8"?>  
  2. "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 http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  7.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd  
  8.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">  
  9.       
  10.     "dataSource"  
  11.         class="org.apache.commons.dbcp.BasicDataSource"  
  12.         destroy-method="close">  
  13.         "driverClassName" value="com.mysql.jdbc.Driver" />  
  14.         "url"  
  15.             value="jdbc:mysql://192.168.0.244:3306/test?useUnicode=true&characterEncoding=UTF-8" />  
  16.         "username" value="root" />  
  17.         "password" value="root" />  
  18.           
  19.         "initialSize" value="10" />  
  20.           
  21.         "maxActive" value="10" />  
  22.           
  23.         "maxIdle" value="20" />  
  24.           
  25.         "minIdle" value="10" />  
  26.         "defaultAutoCommit" value="true" />  
  27.       
  28.       
  29.     "sessionFactory"  
  30.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  31.         "dataSource" ref="dataSource" />  
  32.         "mappingLocations">  
  33.               
  34.                 classpath:/com/nms/entity/**/*.hbm.xml  
  35.               
  36.           
  37.         "hibernateProperties">  
  38.               
  39.                 "hibernate.dialect">  
  40.                     org.hibernate.dialect.MySQL5Dialect  
  41.                   
  42.                 "hibernate.show_sql">true  
  43.                 "hibernate.format_sql">true  
  44.               
  45.           
  46.           
  47.          
  48.     "transactionManager"  
  49.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  50.         "sessionFactory" ref="sessionFactory" />  
  51.          
  52.         
  53.     "transactionInterceptor"    
  54.         class="org.springframework.transaction.interceptor.TransactionInterceptor">    
  55.         "transactionManager" ref="transactionManager" />    
  56.             
  57.         "transactionAttributes">    
  58.                 
  59.                 "*">PROPAGATION_REQUIRED    
  60.                 
  61.             
  62.             
  63.     class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">    
  64.         "beanNames">    
  65.                 
  66.                 *DaoImpl  
  67.                 
  68.             
  69.         "interceptorNames">    
  70.                 
  71.                 transactionInterceptor    
  72.                 
  73.             
  74.         
  75.       
  76.     "userDaoAgency" class="com.dao.impl.UserDaoImpl">  
  77.         "sessionFactory" ref="sessionFactory" />  
  78.       
  79.   

 

 

第四种:使用tx标签配置的拦截器:

Java代码   收藏代码
  1. "1.0" encoding="UTF-8"?>  
  2. "http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:aop="http://www.springframework.org/schema/aop"  
  6.     xmlns:tx="http://www.springframework.org/schema/tx"  
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  8.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  9.            http://www.springframework.org/schema/context  
  10.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  11.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  12.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  13.       
  14.     "dataSource"  
  15.         class="org.apache.commons.dbcp.BasicDataSource"  
  16.         destroy-method="close">  
  17.         "driverClassName" value="com.mysql.jdbc.Driver" />  
  18.         "url"  
  19.             value="jdbc:mysql://192.168.0.244:3306/test?useUnicode=true&characterEncoding=UTF-8" />  
  20.         "username" value="root" />  
  21.         "password" value="root" />  
  22.           
  23.         "initialSize" value="10" />  
  24.           
  25.         "maxActive" value="10" />  
  26.           
  27.         "maxIdle" value="20" />  
  28.           
  29.         "minIdle" value="10" />  
  30.         "defaultAutoCommit" value="true" />  
  31.       
  32.       
  33.     "sessionFactory"  
  34.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  35.         "dataSource" ref="dataSource" />  
  36.         "mappingLocations">  
  37.               
  38.                 classpath:/com/nms/entity/**/*.hbm.xml  
  39.               
  40.           
  41.         "hibernateProperties">  
  42.               
  43.                 "hibernate.dialect">  
  44.                     org.hibernate.dialect.MySQL5Dialect  
  45.                   
  46.                 "hibernate.show_sql">true  
  47.                 "hibernate.format_sql">true  
  48.               
  49.           
  50.       
  51.       
  52.     package="com.dao" />  
  53.         
  54.     "transactionManager"  
  55.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  56.         "sessionFactory" ref="sessionFactory" />  
  57.       
  58.       
  59.     "txAdvice" transaction-manager="transactionManager">  
  60.           
  61.             "*" propagation="REQUIRED" />  
  62.           
  63.       
  64.       
  65.       
  66.         "interceptorPointCuts" expression="execution(* com.dao.*.*(..))" />  
  67.         "txAdvice" pointcut-ref="interceptorPointCuts" />  
  68.       
  69.   

 

 

第五种:注解:

Java代码   收藏代码
  1. "1.0" encoding="UTF-8"?>  
  2. "http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:aop="http://www.springframework.org/schema/aop"  
  6.     xmlns:tx="http://www.springframework.org/schema/tx"  
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  8.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  9.            http://www.springframework.org/schema/context  
  10.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  11.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  12.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  13.       
  14.     "dataSource"  
  15.         class="org.apache.commons.dbcp.BasicDataSource"  
  16.         destroy-method="close">  
  17.         "driverClassName" value="com.mysql.jdbc.Driver" />  
  18.         "url"  
  19.             value="jdbc:mysql://192.168.0.244:3306/test?useUnicode=true&characterEncoding=UTF-8" />  
  20.         "username" value="root" />  
  21.         "password" value="root" />  
  22.           
  23.         "initialSize" value="10" />  
  24.           
  25.         "maxActive" value="10" />  
  26.           
  27.         "maxIdle" value="20" />  
  28.           
  29.         "minIdle" value="10" />  
  30.         "defaultAutoCommit" value="true" />  
  31.       
  32.       
  33.     "sessionFactory"  
  34.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  35.         "dataSource" ref="dataSource" />  
  36.         "mappingLocations">  
  37.               
  38.                 classpath:/com/nms/entity/**/*.hbm.xml  
  39.               
  40.           
  41.         "hibernateProperties">  
  42.               
  43.                 "hibernate.dialect">  
  44.                     org.hibernate.dialect.MySQL5Dialect  
  45.                   
  46.                 "hibernate.show_sql">true  
  47.                 "hibernate.format_sql">true  
  48.               
  49.           
  50.       
  51.       
  52.       
  53.     package="com.dao" />  
  54.       
  55.     "transactionManager"/>  
  56.         
  57.     "transactionManager"  
  58.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  59.         "sessionFactory" ref="sessionFactory" />  
  60.       
  61.   

 

如果使用了注解,那么实现类应该这样写:

Java代码   收藏代码
  1. package com.dao.impl;  
  2. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;  
  3. import org.springframework.stereotype.Component;  
  4. import org.springframework.transaction.annotation.Transactional;  
  5. import com.dao.UserDao;  
  6. @Transactional  
  7. @Component("userDaoAgency")  
  8. public class UserDaoImpl extends HibernateDaoSupport implements UserDao {  
  9.     /** 
  10.      * 为方法增加事务处理特性 
  11.      */  
  12.     @Transactional(readOnly=true)  
  13.     public void getUser(){        
  14.     }     
  15. }  

你可能感兴趣的:(hibernate)