• Spring+Mybatis 多数据源配置


         项目目录结构如下:



     

          spring配置文件

    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" xmlns:aop="http://www.springframework.org/schema/aop"  
    4.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    5.     xmlns:context="http://www.springframework.org/schema/context"  
    6.     xsi:schemaLocation="  
    7.      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  
    8.      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
    9.      http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd  
    10.      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
    11.      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">  
    12.   
    13.     <bean  
    14.         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
    15.         <property name="locations">  
    16.             <value>classpath:init-config.properties</value>  
    17.         </property>  
    18.     </bean>  
    19.       
    20.     <!-- enable component scanning (beware that this does not enable mapper scanning!) -->  
    21.     <context:component-scan base-package="org.zhuc.mybatis" />  
    22.   
    23.     <!-- enable autowire -->  
    24.     <context:annotation-config />  
    25.   
    26.     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"  
    27.         init-method="init" destroy-method="close">  
    28.         <!-- 基本属性 url、user、password -->  
    29.         <property name="url" value="${dataSource.url}" />  
    30.         <property name="username" value="${dataSource.username}" />  
    31.         <property name="password" value="${dataSource.password}" />  
    32.         <property name="connectionProperties" value="${dataSource.driver}"></property>  
    33.   
    34.         <!-- 配置初始化大小、最小、最大 -->  
    35.         <property name="initialSize" value="1" />  
    36.         <property name="minIdle" value="1" />  
    37.         <property name="maxActive" value="20" />  
    38.   
    39.         <!-- 配置获取连接等待超时的时间 -->  
    40.         <property name="maxWait" value="60000" />  
    41.   
    42.         <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->  
    43.         <property name="timeBetweenEvictionRunsMillis" value="60000" />  
    44.   
    45.         <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->  
    46.         <property name="minEvictableIdleTimeMillis" value="300000" />  
    47.   
    48.         <property name="validationQuery" value="SELECT 'x'" />  
    49.         <property name="testWhileIdle" value="true" />  
    50.         <property name="testOnBorrow" value="false" />  
    51.         <property name="testOnReturn" value="false" />  
    52.   
    53.         <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->  
    54.         <property name="poolPreparedStatements" value="true" />  
    55.         <property name="maxPoolPreparedStatementPerConnectionSize"  
    56.             value="20" />  
    57.   
    58.         <!-- 配置监控统计拦截的filters -->  
    59.         <property name="filters" value="stat" />  
    60.     </bean>  
    61.   
    62.     <!-- define the SqlSessionFactory -->  
    63.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
    64.         <property name="dataSource" ref="dataSource" />  
    65.         <property name="typeAliasesPackage" value="org.zhuc.mybatis.domain" />  
    66.     </bean>  
    67.   
    68.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
    69.         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>  
    70.         <property name="basePackage" value="org.zhuc.mybatis.mapper" />  
    71.     </bean>  
    72.   
    73.     <!-- transaction manager, use JtaTransactionManager for global tx -->  
    74.     <bean id="transactionManager"  
    75.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
    76.         <property name="dataSource" ref="dataSource" />  
    77.         <qualifier value="isap" />  
    78.     </bean>  
    79.   
    80.     <!-- 全注解方式   需加上@Transactional -->  
    81.     <tx:annotation-driven transaction-manager="transactionManager" />  
    82.       
    83.     <!-- 事务控制的业务方法配 -->  
    84.     <!--    
    85.     <tx:advice id="txAdvice" transaction-manager="transactionManager">  
    86.         <tx:attributes>  
    87.             <tx:method name="get*" read-only="true" />  
    88.             <tx:method name="page*" read-only="true" />  
    89.             <tx:method name="list*" read-only="true" />  
    90.             <tx:method name="*" />  
    91.         </tx:attributes>  
    92.     </tx:advice>  
    93.     -->  
    94.     <!-- 事务控制拦截 -->  
    95.     <!--    
    96.     <aop:config proxy-target-class="true">  
    97.         <aop:advisor pointcut="execution(* org.zhuc..*.service..*Service.*(..))"  
    98.             advice-ref="txAdvice" />  
    99.     </aop:config>  
    100.     -->  
    101.       
    102.     <!-- =================================================================== -->  
    103.     <!-- 数据源2 -->  
    104.     <bean id="dataSource2" class="com.alibaba.druid.pool.DruidDataSource"  
    105.         init-method="init" destroy-method="close">  
    106.         <!-- 基本属性 url、user、password -->  
    107.         <property name="url" value="${dataSource2.url}" />  
    108.         <property name="username" value="${dataSource2.username}" />  
    109.         <property name="password" value="${dataSource2.password}" />  
    110.         <property name="connectionProperties" value="${dataSource2.driver}"></property>  
    111.   
    112.         <!-- 配置初始化大小、最小、最大 -->  
    113.         <property name="initialSize" value="1" />  
    114.         <property name="minIdle" value="1" />  
    115.         <property name="maxActive" value="20" />  
    116.   
    117.         <!-- 配置获取连接等待超时的时间 -->  
    118.         <property name="maxWait" value="60000" />  
    119.   
    120.         <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->  
    121.         <property name="timeBetweenEvictionRunsMillis" value="60000" />  
    122.   
    123.         <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->  
    124.         <property name="minEvictableIdleTimeMillis" value="300000" />  
    125.   
    126.         <property name="validationQuery" value="SELECT 'x'" />  
    127.         <property name="testWhileIdle" value="true" />  
    128.         <property name="testOnBorrow" value="false" />  
    129.         <property name="testOnReturn" value="false" />  
    130.   
    131.         <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->  
    132.         <property name="poolPreparedStatements" value="true" />  
    133.         <property name="maxPoolPreparedStatementPerConnectionSize"  
    134.             value="20" />  
    135.   
    136.         <!-- 配置监控统计拦截的filters -->  
    137.         <property name="filters" value="stat" />  
    138.     </bean>  
    139.       
    140.     <bean id="sqlSessionFactory2" class="org.mybatis.spring.SqlSessionFactoryBean">  
    141.         <property name="dataSource" ref="dataSource2" />  
    142.         <property name="typeAliasesPackage" value="org.zhuc.mybatis.domain2" />  
    143.     </bean>  
    144.       
    145.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
    146.         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory2"/>  
    147.         <property name="basePackage" value="org.zhuc.mybatis.mapper2" />  
    148.     </bean>  
    149.       
    150.     <bean id="transactionManager2"  
    151.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
    152.         <property name="dataSource" ref="dataSource2" />  
    153.         <qualifier value="insurance" />  
    154.     </bean>  
    155.   
    156.     <!-- 全注解方式 -->  
    157.     <tx:annotation-driven transaction-manager="transactionManager2" />  
    158.       
    159. </beans>  

          配置文件内容:

    Java代码  收藏代码
    1. #数据库连接池设置  
    2. dataSource.driver=com.mysql.jdbc.Driver  
    3. dataSource.url=jdbc:mysql://localhost:3306/dwz  
    4. dataSource.username=root  
    5. dataSource.password=root  
    6.   
    7. dataSource2.driver=oracle.jdbc.driver.OracleDriver  
    8. dataSource2.url=jdbc:oracle:thin:@192.168.10.43:1521:test  
    9. dataSource2.username=test  
    10. dataSource2.password=test  

          Service类代码:

    Java代码  收藏代码
    1. package org.zhuc.mybatis.service;  
    2.   
    3. import org.springframework.transaction.annotation.Transactional;  
    4.   
    5. /** 
    6.  * 基础mysql Service抽象类 
    7.  * 使用isap数据源及回滚 
    8.  * @author zhuc 
    9.  * @create 2013-3-11 下午4:27:33 
    10.  */  
    11. @Transactional(value = "isap", rollbackFor = Exception.class)  
    12. public abstract class BaseMySqlService {  
    13.   
    14. }  

    Java代码  收藏代码
    1. package org.zhuc.mybatis.service;  
    2.   
    3. import java.util.List;  
    4.   
    5. import org.springframework.beans.factory.annotation.Autowired;  
    6. import org.springframework.stereotype.Service;  
    7. import org.zhuc.mybatis.domain.User;  
    8. import org.zhuc.mybatis.mapper.UserMapper;  
    9.   
    10. /** 
    11.  * @author zhuc 
    12.  * @create 2013-3-11 下午1:19:03 
    13.  */  
    14. @Service("userService")  
    15. //@Transactional(value = "isap", rollbackFor = Exception.class)  
    16. public class UserService extends BaseMySqlService {  
    17.   
    18.     @Autowired  
    19.     private UserMapper userMapper;  
    20.   
    21.     /** 
    22.      * @param id 
    23.      * @return 
    24.      */  
    25.     public User get(Integer id) {  
    26.         return userMapper.get(id);  
    27.     }  
    28.   
    29.     /** 
    30.      * @param id 
    31.      * @return 
    32.      */  
    33.     public User get2(Integer id) {  
    34.         return userMapper.get2(id);  
    35.     }  
    36.   
    37.     /** 
    38.      * @return 
    39.      */  
    40.     public List<User> findAll() {  
    41.         return userMapper.findAll();  
    42.     }  
    43.   
    44.     /** 
    45.      * @param user 
    46.      * @throws Exception  
    47.      */  
    48.     public void insert(User user) throws Exception {  
    49.         userMapper.insert(user);  
    50.         throw new Exception("testException");  
    51.     }  
    52. }  
Java代码  收藏代码
  1. package org.zhuc.mybatis.service;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.stereotype.Service;  
  5. import org.springframework.transaction.annotation.Transactional;  
  6. import org.zhuc.mybatis.domain2.Flow;  
  7. import org.zhuc.mybatis.mapper2.FlowMapper;  
  8.   
  9. /** 
  10.  * @author zhuc 
  11.  * @create 2013-3-11 下午1:19:03 
  12.  */  
  13. @Service("flowService")  
  14. @Transactional(value = "insurance", rollbackFor = Exception.class)  
  15. public class FlowService {  
  16.   
  17.     @Autowired  
  18.     private FlowMapper flowMapper;  
  19.   
  20.     /** 
  21.      * @param id 
  22.      * @return 
  23.      */  
  24.     public Flow get(String id) {  
  25.         return flowMapper.get(id);  
  26.     }  
  27.   
  28. }  

      客户端测试类如下:

Java代码  收藏代码
  1. package spring;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5. import org.zhuc.mybatis.service.FlowService;  
  6. import org.zhuc.mybatis.service.UserService;  
  7.   
  8. /** 
  9.  * @author zhuc 
  10.  * @create 2013-3-11 下午1:47:03 
  11.  */  
  12. public class MultiTest {  
  13.   
  14.     /** 
  15.      * @param args 
  16.      * @throws Exception  
  17.      */  
  18.     public static void main(String[] args) throws Exception {  
  19.         ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");  
  20.         UserService userService = (UserService) ac.getBean("userService");  
  21.         System.out.println(userService.get(1));  
  22.         System.out.println(userService.get2(1));  
  23.   
  24.         FlowService flowService = (FlowService) ac.getBean("flowService");  
  25.         System.out.println(flowService.get("94003d29-a7b0-42f0-839c-fa609b209ff1"));  
  26.   
  27.         //      User user = new User();  
  28.         //      user.setId(100);  
  29.         //      user.setUserName("admin100");  
  30.         //      user.setPassword("password100");  
  31.         //      user.setTrueName("小李4");  
  32.         //      user.setCreateTime(new Date());  
  33.         //  
  34.         //      userService.insert(user);   //受事务管理,抛出Exception时将回滚  (rollbackFor)  
  35.     }  
  36.   
  37. }  

      输出结果:

2013-03-14 11:21:19 [main] DEBUG org.zhuc.mybatis.mapper.UserMapper.get - ooo Using Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@f6d64c5]
2013-03-14 11:21:19 [main] DEBUG org.zhuc.mybatis.mapper.UserMapper.get - ==>  Preparing: SELECT * from T_USER where id = ?
2013-03-14 11:21:19 [main] DEBUG org.zhuc.mybatis.mapper.UserMapper.get - ==> Parameters: 1(Integer)
User [id=1, userName=userName0, password=password0, roleId=3, trueName=姓名0, createTime=Mon Mar 04 14:17:31 CST 2013, modifyTime=Mon Mar 04 14:17:31 CST 2013]
2013-03-14 11:21:19 [main] DEBUG org.zhuc.mybatis.mapper.UserMapper.get2 - ooo Using Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@f6d64c5]
2013-03-14 11:21:19 [main] DEBUG org.zhuc.mybatis.mapper.UserMapper.get2 - ==>  Preparing: SELECT * from T_USER where id = ?
2013-03-14 11:21:19 [main] DEBUG org.zhuc.mybatis.mapper.UserMapper.get2 - ==> Parameters: 1(Integer)
User [id=1, userName=userName0, password=password0, roleId=3, trueName=姓名0, createTime=Mon Mar 04 14:17:31 CST 2013, modifyTime=Mon Mar 04 14:17:31 CST 2013]
2013-03-14 11:21:19 [main] DEBUG org.zhuc.mybatis.mapper2.FlowMapper.get - ooo Using Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@11742dfe]
2013-03-14 11:21:19 [main] DEBUG org.zhuc.mybatis.mapper2.FlowMapper.get - ==>  Preparing: SELECT * from T_FLOW where FLOW_ID = ?
2013-03-14 11:21:19 [main] DEBUG org.zhuc.mybatis.mapper2.FlowMapper.get - ==> Parameters: 94003d29-a7b0-42f0-839c-fa609b209ff1(String)
Flow [id=94003d29-a7b0-42f0-839c-fa609b209ff1, insuranceId=5a08abc4-463e-436d-b249-9954be487cdd, codeId=14, operatingTime=Mon Jan 21 16:47:12 CST 2013]

  • 相关阅读:
    Struts2异常处理配置
    struts2支持的结果类型
    Project facet Java 1.8 is not supported by target runtime Apache Tomcat v7.0.
    net.paoding.analysis.exception.PaodingAnalysisException: dic home should not be a file, but a directory!
    struts.xml路径修改后的web配置
    struts.xml中的配置常量的含义
    Spring实战笔记
    2018第2周日
    新人替代旧人
    Web安全总结摘录
  • 原文地址:https://www.cnblogs.com/jpfss/p/8118271.html
  • Copyright © 2020-2023  润新知