spring 多数据源配置
多数据源配置方法:
在配置数据源配置文件中多加一个数据源配置即可:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="${dataSource.initialSize}"></property>
<!-- 连接池最大数量 -->
<property name="maxActive" value="${dataSource.maxActive}"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${dataSource.minIdle}"></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${dataSource.maxWait}"></property>
</bean>
<bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url2}" />
<property name="username" value="${username2}" />
<property name="password" value="${password2}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="${dataSource.initialSize}"></property>
<!-- 连接池最大数量 -->
<property name="maxActive" value="${dataSource.maxActive}"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${dataSource.minIdle}"></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${dataSource.maxWait}"></property>
</bean>
上面是两个数据源;
<!-- 配置 mybatis -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis.xml"/>
<property name="mapperLocations" value="classpath:com/sitech/message/pojo/*.xml"/>
</bean>
<!-- 配置 mybatis2 -->
<bean id="sqlSessionFactory2" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource2"/>
<property name="configLocation" value="classpath:mybatis.xml"/>
<property name="mapperLocations" value="classpath:com/sitech/message/pojo/*.xml"/>
</bean>
<!-- 配置 BaseDao -->
<bean id="baseDao" class="com.sitech.message.dao.impl.BaseDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<!-- 配置 BaseDao2 -->
<bean id="baseDao2" class="com.sitech.message.dao.impl.BaseDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory2"/>
</bean>
想配几个就加几个数据源,
测试类:
@Autowired
private BaseDao baseDao;
@Resource(name="baseDao2")
private BaseDao baseDao2;
@Scheduled(cron = "0/30 * * * * ?")
public void TestTwoDataSource(){
System.out.println("sssssssss");
Map<String,String> data1 = new HashMap<String,String>();
data1= baseDao.selectOne(NameSpace.BS_QUESTION_INFOMapper, "findRouteConfigTest");
System.out.println("dara1:"+data1);
Map<String,String> data2 = new HashMap<String,String>();
data2= baseDao2.selectOne(NameSpace.BS_QUESTION_INFOMapper, "findRouteConfigTest");
System.out.println("data2:"+data2);
}
完整数据源配置文件:
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<tx:annotation-driven />
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:config/env/jdbc.properties" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="${dataSource.initialSize}"></property>
<!-- 连接池最大数量 -->
<property name="maxActive" value="${dataSource.maxActive}"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${dataSource.minIdle}"></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${dataSource.maxWait}"></property>
</bean>
<bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url2}" />
<property name="username" value="${username2}" />
<property name="password" value="${password2}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="${dataSource.initialSize}"></property>
<!-- 连接池最大数量 -->
<property name="maxActive" value="${dataSource.maxActive}"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${dataSource.minIdle}"></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${dataSource.maxWait}"></property>
</bean>
<!-- 配置 mybatis -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis.xml"/>
<property name="mapperLocations" value="classpath:com/sitech/message/pojo/*.xml"/>
</bean>
<!-- 配置 mybatis2 -->
<bean id="sqlSessionFactory2" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource2"/>
<property name="configLocation" value="classpath:mybatis.xml"/>
<property name="mapperLocations" value="classpath:com/sitech/message/pojo/*.xml"/>
</bean>
<!-- 配置 BaseDao -->
<bean id="baseDao" class="com.sitech.message.dao.impl.BaseDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<!-- 配置 BaseDao2 -->
<bean id="baseDao2" class="com.sitech.message.dao.impl.BaseDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory2"/>
</bean>
<!-- Mybatis Dao -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.sitech.message.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--
name:与事务属性关联的方法名。通配符(*)可以用来指定一批关联到相同的事务属性的方法。 如:'get*'、'handle*'、'on*Event'等等。
propagation="REQUIRED":事务传播行为
isolation 默认值 DEFAULT:事务隔离级别
timeout 默认值 -1 事务超时的时间(以秒为单位)
read-only 默认值 false 事务是否只读?
rollback-for 将被触发进行回滚的 Exception(s);以逗号分开。 如:'com.foo.MyBusinessException,ServletException'
no-rollback-for 不被触发进行回滚的 Exception(s);以逗号分开。 如:'com.foo.MyBusinessException
-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="batch*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"/>
<tx:method name="query*" propagation="REQUIRED" read-only="true" />
<tx:method name="find*" propagation="REQUIRED" read-only="true" />
<tx:method name="select*" propagation="REQUIRED" read-only="true" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!--
把事务控制在Service层
第一个 * —— 通配 任意返回值类型
第二个 * —— 通配 包com.polin.omeal.service下的任意class
第三个 * —— 通配 包com.polin.omeal.service下的任意class的任意方法
第四个 .. —— 通配 方法可以有0个或多个参数
综上:包com.polin.omeal.service下的任意class的具有任意返回值类型、任意类、任意名称的方法、任意数目参数和<tx:advice/>有关的设置
-->
<aop:config>
<aop:pointcut id="aop" expression="execution(* com.sitech.message.*.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="aop"/>
</aop:config>
</beans>
spring 多数据源配置的更多相关文章
- 基于xml的Spring多数据源配置和使用
上一篇讲了<基于注解的Spring多数据源配置和使用>,通过在类或者方法上添加@DataSource注解就可以指定某个数据源.这种方式的优点是控制粒度细,也更灵活. 但是当有些时候项目分模 ...
- spring BasicDataSource 数据源配置 sqlserver数据库 oracle数据库 mysql数据jdbc配置
spring BasicDataSource 数据源配置 sqlserver数据库 oracle数据库 mysql数据jdbc配置 jdbc.properties 文件信息如下: ---------- ...
- 基于注解的Spring多数据源配置和使用(非事务)
原文:基于注解的Spring多数据源配置和使用 1.创建DynamicDataSource类,继承AbstractRoutingDataSource package com.rps.dataSourc ...
- spring(16)------spring的数据源配置
在spring中,通过XML的形式实现数据源的注入有三种形式. 一.使用spring自带的DriverManagerDataSource 使用DriverManagerDataSource配置数据源与 ...
- 基于注解的Spring多数据源配置和使用
前一段时间研究了一下spring多数据源的配置和使用,为了后期从多个数据源拉取数据定时进行数据分析和报表统计做准备.由于之前做过的项目都是单数据源的,没有遇到这种场景,所以也一直没有去了解过如何配置多 ...
- Spring jndi数据源配置方法
xml配置: <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverMana ...
- Spring 多数据源配置(转)
转载自:https://www.cnblogs.com/digdeep/p/4512368.html 同一个项目有时会涉及到多个数据库,也就是多数据源.多数据源又可以分为两种情况: 1)两个或多个数据 ...
- Spring boot 数据源配置。
配置文件 : spring boot 配置文件 有两种形式 ,一种是properties文件.一种是yml文件.案列使用properties文件. 数据源的默认配置 : spring boot 约定 ...
- spring多数据源配置
项目中我们经常会遇到多数据源的问题,尤其是数据同步或定时任务等项目更是如此.多数据源让人最头痛的,不是配置多个数据源,而是如何能灵活动态的切换数据源.例如在一个spring和hibernate的框架的 ...
随机推荐
- pytorch-Alexnet 网络
Alexnet网络结构, 相比于LeNet,Alexnet加入了激活层Relu, 以及dropout层 第一层网络结构: 11x11x3x96, 步长为4, padding=2 第二层网络结构: 5x ...
- shiro中接入单点登录功能
最近新建的系统中使用了shiro,而shiro框架中包含登录认证和鉴权的功能,因为我们系统要统一接入公司内部的单点登录(isso)系统,所以通过isso的登录用户,需要在shiro中置为已认证,一下提 ...
- LC 593. Valid Square
Given the coordinates of four points in 2D space, return whether the four points could construct a s ...
- android Api操作SQLite数据库的示例代码
import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.databa ...
- manifest节点
xmlns:android属性——定义命名空间 这个属性定义了这个XML文件所使用的命名空间.如果需要指定特殊的命名空间,就需要手动编写代码,在Android Studio基本格式如下: xmlns: ...
- k8s 网络模型解析之原理
今天研究了一下k8s的网络模型,该解析基于flannel vxlan+ kubeproxy iptables 模式. 一.Docker 首先分析一下Docker层面的网络模型,我们知道容器是基于内核的 ...
- git配置过程中fatal:拒绝合并无关的历史
首先将远程仓库和本地仓库关联起来: git branch --set-upstream-to=origin/master master 然后使用git pull整合远程仓库和本地仓库, git pul ...
- centos7 httpd配置
centos7 httpd配置 标签(空格分隔): 未分类 隐藏server信息 修改httpd.conf 设置,添加如下两行 ServerSignature Off ServerTokens Pro ...
- python-Re模块用法
主要函数:match().search().compile() re.compile compile 函数用于编译正则表达式,生成一个正则表达式( Pattern )对象,供 match() 和 se ...
- superset使用
部署完成后,就可以登陆superset的web页面: http://192.168.56.105:8088 #默认是8080,在配置文件中可以修改 点database可以选择连接不同的数据源,如My ...