Druid多数据源配置

2018-01-29 15:18:57
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
                    http://www.springframework.org/schema/beans 
                    http://www.springframework.org/schema/beans/spring-beans.xsd 
                    http://www.springframework.org/schema/tx 
                    http://www.springframework.org/schema/tx/spring-tx.xsd
                    http://www.springframework.org/schema/aop 
                    http://www.springframework.org/schema/aop/spring-aop.xsd">
 
    <!-- 配置数据源 -->
    <bean name="datasource" class="com.alibaba.druid.pool.DruidDataSource"
        init-method="init" destroy-method="close">
        <!-- <property name="driverClassName" value="${jdbc.driver}" /> --><!-- dirverClassName 可以不加 -->
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>
 
    <!-- 配置数据源1 -->
    <bean name="datasource1" class="com.alibaba.druid.pool.DruidDataSource"
        init-method="init" destroy-method="close">
        <!-- <property name="driverClassName" value="${jdbc.driver}" /> --><!-- dirverClassName 可以不加 -->
        <property name="url" value="${jdbc.url1}"></property>
        <property name="username" value="${jdbc.username1}" />
        <property name="password" value="${jdbc.password1}" />
    </bean>
 
    <bean id="multipleDataSource" class="com.service.manager.util.MultipleDataSource">
        <property name="defaultTargetDataSource" ref="datasource"/>
        <property name="targetDataSources">
            <map>
                <entry key="datasource" value-ref="datasource"/>
                <entry key="datasource1" value-ref="datasource1"/>
            </map>
        </property>
    </bean>
     
    <!--配置sqlSessionFactory 并读取mybatis的一些配置 -->
    <bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="multipleDataSource"></property>
        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>
        <property name="mapperLocations" value="classpath*:/mybatis/*Mapper.xml" />
    </bean>
 
    <!-- 自动扫描 将Mapper接口生成代理注入到Spring -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.service.manager.repository.mybatis" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    </bean>
 
    <!-- 配置事物 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="multipleDataSource"></property>
    </bean>
 
    <!-- <tx:annotation-driven transaction-manager = "transactionManager"/> -->
 
    <!-- 事物的具体内容 -->
    <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="append*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="modify*" propagation="REQUIRED" />
            <tx:method name="edit*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="remove*" propagation="REQUIRED" />
            <tx:method name="repair" propagation="REQUIRED" />
            <tx:method name="delAndRepair" propagation="REQUIRED" />
 
 
            <tx:method name="get*" propagation="SUPPORTS" />
            <tx:method name="find*" propagation="SUPPORTS" />
            <tx:method name="load*" propagation="SUPPORTS" />
            <tx:method name="search*" propagation="SUPPORTS" />
            <tx:method name="datagrid*" propagation="SUPPORTS" />
 
 
            <tx:method name="*" propagation="SUPPORTS" />
        </tx:attributes>
    </tx:advice>
 
    <!-- 定义一个切面,在定义的切面上加入事物 <aop:config> <aop:pointcut id="transactionPointcut" 
        expression="execution(* com.service.manager.service..*Impl.*(..))" /> <aop:advisor 
        pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" /> </aop:config> -->
</beans>

增加MultipleDataSource类(保证spring能扫描到)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.service.manager.util;
 
 
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
 
public class MultipleDataSource extends AbstractRoutingDataSource {
     
    public static final String datasource = "datasource";
    public static final String datasource1 = "datasource1";
     
    private static final ThreadLocal<String> dataSourceKey = new InheritableThreadLocal<String>();
 
    public static void setDataSourceKey(String dataSource) {
        dataSourceKey.set(dataSource);
    }
 
    @Override
    protected Object determineCurrentLookupKey() {
        // TODO Auto-generated method stub
        return dataSourceKey.get();
    }
 
}

增加配置文件:

1
2
3
4
5
6
7
8
9
10
[html] view plain copy
jdbc.driver=com.mysql.jdbc.Driver  
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&autoreconnect=true&zeroDateTimeBehavior=convertToNull  
jdbc.username=root  
jdbc.password=  
   
jdbc.driver1=com.mysql.jdbc.Driver  
jdbc.url1=jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8&autoreconnect=true&zeroDateTimeBehavior=convertToNull  
jdbc.username1=root  
jdbc.password1=

Controller切换数据源:

  1. MultipleDataSource.setDataSourceKey(MultipleDataSource.datasource);  


  • 2019-09-26 21:46:36

    Shiro整合JWT+Token过期刷新,demo,详解

    最近使用SpringBoot集成Shiro,JWT快速搭建了一个后台系统,Shiro前面已经使用过,JWT(JSON Web Tokens)是一种用于安全的传递信息而采用的一种标准。Web系统中,我们使用加密的Json来生成Token在服务端与客户端无状态传输,代替了之前常用的Session。 系统采用Redis作为缓存,解决Token过期更新的问题,同时集成SSO登录,完整过程这里来总结一下。

  • 2019-09-26 21:48:15

    解决UEditor超出最大字数后只提示不限制的问题

    最近项目用到百度额UEditor文本编辑器,今天测试向我提出了一个问题。就是在输入的文字超过默认的最大字数限制之后,虽然提示“字数超过最大范围,服务器可能拒绝保存”,但是仍然可以点击保存按钮进行保存。

  • 2019-09-27 14:49:33

    Google Guava介绍和体检

    JDK提供的String还不够好么?也许还不够友好,至少让我们用起来还不够爽,还得操心!举个栗子,比如String提供的split方法,我们得关心空字符串吧,还得考虑返回的结果中存在null元素吧,只提供了前后trim的方法(如果我想对中间元素进行trim呢)。

  • 2019-09-28 00:03:21

    shiro的session共享,持久化

     shiro的session创建与session的查询、更新、过期、删除中,shiro对session的操作基本都讲到了,但还缺一个session共享没有讲解;session共享的原理其实在自定义session管理一文已经讲过了,本文不讲原理,只看看shiro的session共享的实现。