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注解使用 概述事务管理对于企业应用来说是 ...
随机推荐
- libevent和libcurl实现http和https服务器 cJSON使用
前言 libevent和libcurl都是功能强大的开源库:libevent主要实现服务器,包含了select.epoll等高并发的实现:libcurl实现了curl命令的API封装,主要作为客户端. ...
- 版本控制工具——Git常用操作(上)
本文由云+社区发表 作者:工程师小熊 摘要:用了很久的Git和svn,由于总是眼高手低,没能静下心来写这些程序员日常开发最常用的知识点.现在准备开一个专题,专门来总结一下版本控制工具,让我们从git开 ...
- django-restframework之缓存系统
django-restframework之缓存系统 一 前言 一 为什么需要缓存 在动态网站中,用户所有的请求,服务器都会去数据库中进行相应的增.删.查.改,渲染模块,执行业务逻辑,最后生成用户看到的 ...
- 【带着canvas去流浪(4)】绘制散点图
目录 一. 任务说明 二. 重点提示 三. 示例代码 四.散点hover交互效果的实现 4.1 基本算法 4.2 参考代码 4.3 Demo中的小问题 示例代码托管在:http://www.githu ...
- .net core中使用autofac进行IOC
.net Core中自带DI是非常简单轻量化的,但是如果批量注册就得扩展,下面使用反射进行批量注册的 public void AddAssembly(IServiceCollection servic ...
- PostgreSql的Explain命令详解
http://toplchx.iteye.com/blog/2091860 使用EXPLAIN PostgreSQL为每个收到的查询设计一个查询规划.选择正确的匹配查询结构和数据属性的规划对执行效率是 ...
- HTML学习总结&基础篇
何为坚持?一个“勤”,一个“忍”. 年前给自己定的目标,今年一定要坚持多逛园子,多看一些大佬的帖子,然后自己也尽量能够分享自己学习的收获,让自己进步快些,但是多逛园子是做到了,写博客这个东西,今年好像 ...
- Saslauthd服务实现SMTP发信认证
一.SMTP发信认证 通过sasl库中的saslauthd服务实现SMTP认证 二.部署 1.先安装postifx 略 2.生成sasl配置文件,实现使用sasl认证 vim /usr/lib64/s ...
- PHP面向对象和面向过程
编程界不论是PHP.Python都有这么两种编程思想,oop面向对象.pop面向过程,这两种在功能上没有多大区别,但是在代码编写上却有很大区别.今天就记录一下面向对象和面向过程的优点.缺点.定义.实现 ...
- vue学习记录③(路由)
上篇文章我们用vue-cli脚手架工具做了个简单的hello world页面,但是我们破坏了原来的流程,而正常的访问页面应该是通过路由来实现的. 那么什么是路由呢? 路由就是通过不同的url来访问不同 ...