Spring Mybatis多数据源配置范例
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 框架 -->
<bean id="messageFactory" class="catf.core.message.factory.MessageFactory">
<constructor-arg index="0" name="resource" value="catf/resource/mapper/msg/catf_messages_mapper.xml"/>
</bean> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:spring/handler.properties</value>
<value>classpath*:/config/扩展api/expand-base-config.properties</value>
<value>classpath:spring/ffmpeg.properties</value>
<value>classpath:公共数据/mysql配置.properties</value>
</list>
</property>
<property name="fileEncoding" value="utf-8" />
</bean> <bean id="dataSourceParent" abstract="true" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="filters" value="stat" />
<property name="maxActive" value="20" />
<property name="initialSize" value="1" />
<property name="maxWait" value="60000" />
<property name="minIdle" value="1" />
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="poolPreparedStatements" value="true" />
<property name="maxOpenPreparedStatements" value="20" />
<property name="asyncInit" value="true" />
</bean> <bean id="dataSourceMaserati" parent="dataSourceParent">
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean> <bean id="dataSourceBentley" parent="dataSourceParent">
<property name="url" value="${jdbc2.url}" />
<property name="username" value="${jdbc2.username}" />
<property name="password" value="${jdbc2.password}" />
</bean> <bean id="dataSourceSwitcher" class="catf.component.mybatis.manager.ThreadLocalRountingDataSource">
<property name="targetDataSources">
<map>
<entry key="dataSourceMarserati" value-ref="dataSourceMaserati"/>
<entry key="dataSourceBentley" value-ref="dataSourceBentley"/>
</map>
</property>
<property name="defaultTargetDataSource" ref="dataSourceMaserati"/>
</bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSourceSwitcher"/>
<property name="typeAliasesPackage" value="catf.component.mybatis"/>
<property name="mapperLocations" value="classpath:catf/component/mybatis/mapper/mybatisMapper.xml"/>
</bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
p:basePackage="catf.component.mybatis.dao"
p:sqlSessionFactoryBeanName="sqlSessionFactory">
</bean> <!-- 4. 事务管理 : DataSourceTransactionManager -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSourceSwitcher"/>
</bean> <!-- 5. 使用声明式事务 -->
<tx:annotation-driven transaction-manager="txManager" /> <bean id="handlerStart" class="catf.core.handler.frame.spring.HandlerStart">
<property name="frameMessageHandler" value="${handler.message.frame}"/>
<property name="tstExecMessageHandler" value="${handler.message.tst.exec}"/>
<property name="interpreterHandler" value="${handler.interpreter}"/>
<property name="loaderHandler" value="${handler.loader}"/>
<property name="fileOperateHandler" value="${handler.file.operate}"/>
</bean>
<bean id="ffmpegConfig" class="catf.executor.record.model.FFmpegConfig">
<property name="windowsCmd" value="${ffmpeg.win}"/>
<property name="linuxCmd" value="${ffmpeg.lx}"/>
<property name="macOSCmd" value="${ffmpeg.mac}"/>
</bean> <context:component-scan
base-package="catf.core.thread.pool,catf.core.handler.frame.spring,catf.core.message.service,catf.core.xml.service">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
<context:exclude-filter type="regex" expression="catf.core.handler.frame.spring.HandlerStart"/>
</context:component-scan>
<context:component-scan
base-package="catf.interpreter, catf.loader, catf.executor">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
<context:exclude-filter type="regex" expression="catf.executor.record.model.FFmpegConfig"/>
</context:component-scan> <!-- component -->
<context:component-scan
base-package="catf.component">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
<context:exclude-filter type="regex" expression="catf.executor.record.model.FFmpegConfig"/>
</context:component-scan>
<aop:aspectj-autoproxy proxy-target-class="true" /> <!-- 扩展组件 -->
<context:component-scan
base-package="catf.expand.component.base">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
<context:component-scan
base-package="catf.expand">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
<context:component-scan
base-package="catf.expand.component.web">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
<context:component-scan
base-package="weshare.token">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan> </beans>
mysql配置.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://20.1.1.11:16306/maserati?tinyInt1isBit=false&autoReconnect=true
jdbc.username=root
jdbc.password=root jdbc2.driver=com.mysql.cj.jdbc.Driver
jdbc2.url=jdbc:mysql://20.1.1.11:16306/bentley?tinyInt1isBit=false&autoReconnect=true
jdbc2.username=root
jdbc2.password=root
ThreadLocalRountingDataSource.class
package catf.component.mybatis.manager; import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; public class ThreadLocalRountingDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DataSourcesTypeManager.get();
} }
DataSourcesTypeManager .class
package catf.component.mybatis.manager; public class DataSourcesTypeManager { private static final ThreadLocal<String> dataSourceTypes = new ThreadLocal<>(); public static String get() {
return dataSourceTypes.get();
} /**
* 设置数据源
*/
public static void set(String dataSourceType) {
dataSourceTypes.set(dataSourceType);
} /**
* 清除dataSourceKey的值
*/
public static void remove() {
dataSourceTypes.remove();
} }
Spring Mybatis多数据源配置范例的更多相关文章
- Spring+MyBatis多数据源配置实现
最近用到了MyBatis配置多数据源,原以为简单配置下就行了,实际操作后发现还是要费些事的,这里记录下,以作备忘 不多废话,直接上代码,后面会有简单的实现介绍 jdbc和log4j的配置 #定义输出格 ...
- spring mybatis 多数据源配置
1.创建好数据库的配置文件 mysql.url=jdbc:mysql://***/***?useUnicode=true&characterEncoding=UTF-8 mysql.usern ...
- springMVC+spring+mybatis多数据源配置
1.application.properties配置 <?xml version="1.0" encoding="UTF-8"?> <bean ...
- Spring+Mybatis多数据源配置
一.配置文件 properties ds1.driverClassName=com.mysql.jdbc.Driver ds1.url=jdbc:mysql://192.168.200.130:330 ...
- Spring实现多数据源配置
一.前言 对于小型项目,服务器与数据库是可以在同一台机子上的,但随着业务的庞大与负责,数据库和服务器就会分离开来.同时随着数据量的增大,数据库也要分开部署到多台机子上. 二.Spring配置文件修改 ...
- Spring Boot 2.X(五):MyBatis 多数据源配置
前言 MyBatis 多数据源配置,最近在项目建设中,需要在原有系统上扩展一个新的业务模块,特意将数据库分库,以便减少复杂度.本文直接以简单的代码示例,如何对 MyBatis 多数据源配置. 准备 创 ...
- MyBatis多数据源配置(读写分离)
原文:http://blog.csdn.net/isea533/article/details/46815385 MyBatis多数据源配置(读写分离) 首先说明,本文的配置使用的最直接的方式,实际用 ...
- spring,mybatis事务管理配置与@Transactional注解使用[转]
spring,mybatis事务管理配置与@Transactional注解使用[转] spring,mybatis事务管理配置与@Transactional注解使用 概述事务管理对于企业应用来说是至关 ...
- spring,mybatis事务管理配置与@Transactional注解使用
spring,mybatis事务管理配置与@Transactional注解使用[转] spring,mybatis事务管理配置与@Transactional注解使用 概述事务管理对于企业应用来说是 ...
随机推荐
- 网络学习笔记(二):TCP可靠传输原理
TCP数据段作为IP数据报的数据部分来传输的,IP层提供尽最大努力服务,却不保证数据可靠传输.TCP想要提供可靠传输,需要采取一定的措施来让不可靠的传输信道提供可靠传输服务.比如:出现差错时,让发 ...
- 一套代码小程序&Web&Native运行的探索02
接上文:一套代码小程序&Web&Native运行的探索01,本文都是一些探索性为目的的研究学习,在最终版输出前,内中的内容可能会有点乱 参考: https://github.com/f ...
- 说说我为什么看好Spring Cloud Alibaba
最近对<Spring Cloud Alibaba基础教程>系列的催更比较多,说一下最近的近况:因为打算Spring Boot 2.x一起更新.所以一直在改博客Spring Boot专题页和 ...
- Spring Cloud 系列之 Eureka 实现服务注册与发现
如果你对 Spring Cloud 体系还不是很了解,可以先读一下 Spring Cloud 都有哪些模块 Eureka 是 Netflix 开源的服务注册发现组件,服务发现可以说是微服务架构的核心功 ...
- centOS7下Spark安装配置
环境说明: 操作系统: centos7 64位 3台 centos7-1 192.168.190.130 master centos7-2 192.168.190.129 slave1 centos7 ...
- 安装centos5.x的基本优化配置
1.添加账号
- 47.Odoo产品分析 (五) – 定制板块(2) – 为业务自定义odoo(2)
查看Odoo产品分析系列--目录 Odoo产品分析 (五) – 定制板块(2) – 为业务自定义odoo(1) 4 添加自定义字段 定制odoo的最普通的原因就是指定到公司的附加信息.如果您正在运行一 ...
- QT之setstylesheet防止子窗体继承父窗体样式
/* 1.这里的#号表示,主控件不会影响子控件 2.设置多个样式,可以用双引号和分号 */ ui->groupBox_1->setStyleSheet("#groupBox_1{ ...
- Ngnix负载均衡安装及配置
1.ngnix概念 Nginx是一款高性能的http 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器.由俄罗斯的程序设计师Igor Sysoev所开发,官方测试nginx能够支支撑5 ...
- Django-1- urls的路由配置, 命名, 重定向, path及re_path
1-项目目录下配置(path方法) 这里的访问路径可以是127.0.0.1:8000/index/ 2-APP目录下配置(path方法) 这里的访问路径可以是127.0.0.1:8000/t ...