Spring中mybatis多数据源写法
来源:互联网 发布:乐乎的拼音 编辑:程序博客网 时间:2024/05/24 05:42
xml
<?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:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"xmlns:task="http://www.springframework.org/schema/task"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"><context:annotation-config /><context:component-scan base-package="com.mx.*" /><!--计划任务 --><task:annotation-driven scheduler="Scheduler" executor="executor" mode="proxy"/><task:scheduler id="Scheduler" pool-size="5" /><task:executor id="executor" keep-alive="300" pool-size="10" queue-capacity="5" rejection-policy="CALLER_RUNS" /> <!--配置数据源属性文件 --><bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="location"><value>/WEB-INF/configs/sqlServer.properties</value></property></bean><bean id="conf" class="com.mx.server.common.config.ConfigLoader" /><!--配置数据源 --><bean id="parentDataSource" class="org.apache.commons.dbcp.BasicDataSource"></bean><!--配置数据源1 --><bean id="dataSource1" parent="parentDataSource"><property name="driverClassName"><value>${test1.driver}</value></property><property name="url"><value>${test1.url}</value></property><property name="username"><value>${test1.user}</value></property><property name="password"><value>${test1.pwd}</value></property><!--初始化连接数 --><property name="initialSize" value="10" /><!-- 最大连接数 --><property name="maxActive" value="20" /><!-- 最大空闲连接数 --><property name="maxIdle" value="15" /><!-- 最大建立连接等待时间。如果超过此时间将接到异常。设为-1 表示无限制 10秒连接等待 --><property name="maxWait" value="10000" /><!-- 是否回收被遗弃的(一般是忘了释放的)数据库连接到连接池中 --><property name="removeAbandoned" value="true" /><!--数据库连接过多长时间不用将被视为被遗弃而收回连接池中 60秒不工作回收 --><property name="removeAbandonedTimeout" value="60000" /><property name="testOnBorrow"><value>true</value></property><property name="validationQuery"><value>SELECT 1 FROM DUAL</value></property></bean><!--配置数据源2 --><bean id="dataSource2" parent="parentDataSource"><property name="driverClassName"><value>${test2.driver}</value></property><property name="url"><value>${test2.url}</value></property><property name="username"><value>${test2.user}</value></property><property name="password"><value>${test2.pwd}</value></property><!--初始化连接数 --><property name="initialSize" value="10" /><!-- 最大连接数 --><property name="maxActive" value="20" /><!-- 最大空闲连接数 --><property name="maxIdle" value="15" /><!-- 最大建立连接等待时间。如果超过此时间将接到异常。设为-1 表示无限制 10秒连接等待 --><property name="maxWait" value="10000" /><!-- 是否回收被遗弃的(一般是忘了释放的)数据库连接到连接池中 --><property name="removeAbandoned" value="true" /><!--数据库连接过多长时间不用将被视为被遗弃而收回连接池中 60秒不工作回收 --><property name="removeAbandonedTimeout" value="60000" /></bean><bean id="dataSource" class="com.mx.etl.common.util.dbChanges.DataSources"><property name="targetDataSources"><map key-type="java.lang.String"><entry value-ref="dataSource1" key="TEST1"></entry><entry value-ref="dataSource2" key="TEST2"></entry></map></property><property name="defaultTargetDataSource" ref="dataSource1"></property></bean><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="configLocation" value="classpath:mybatis_config.xml" /><property name="dataSource" ref="dataSource" /></bean><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" autowire="byName"><property name="basePackage" value="com.mx.*" /><property name="sqlSessionFactory" ref="sqlSessionFactory" /></bean> <tx:annotation-driven transaction-manager="transactionManager" /> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"> <ref bean="dataSource" /> </property> </bean></beans>DataSourceInstances。java
package com.mx.etl.common.util.dbChanges;public class DataSourceInstances { //定义数据源的标识, 和applicationContext.xml中 DataSources 的 targetDataSources 的key对应 public static final String TEST1="TEST1"; public static final String TEST2="TEST2"; }DataSources。java
package com.mx.etl.common.util.dbChanges;import java.sql.SQLFeatureNotSupportedException;import java.util.logging.Logger;import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;//配置于applicationContext 中,线程局部变量ThreadLocal contextHolder 保存当前需要的数据源类型,//当 DataSourceSwitch.setDataSourceType(DataSourceInstances.XXX),//保存当前需要的数据源类型的时候,DataSources 会从当前线程中查找线程变量的数据源类型,从而决定使用何种数据源 public class DataSources extends AbstractRoutingDataSource{ @Override protected Object determineCurrentLookupKey() { return DataSourceSwitch.getDataSourceType(); } @Override public Logger getParentLogger() throws SQLFeatureNotSupportedException { // TODO Auto-generated method stub return null; } }
DataSourceSwitch.java
package com.mx.etl.common.util.dbChanges;public class DataSourceSwitch{ @SuppressWarnings("rawtypes") private static final ThreadLocal contextHolder=new ThreadLocal(); @SuppressWarnings("unchecked") public static void setDataSourceType(String dataSourceType){ contextHolder.set(dataSourceType); } public static String getDataSourceType(){ return (String) contextHolder.get(); } public static void clearDataSourceType(){ contextHolder.remove(); } }
sqlServer.properties
#数据库1test1.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=truetest1.driver=com.mysql.jdbc.Drivertest1.user=roottest1.pwd=root#数据库2test2.url=jdbc:mysql://192.168.1.2:3306/test1?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=truetest2.driver=com.mysql.jdbc.Drivertest2.user=roottest2.pwd=root
用法:
AuthTask.java
@Componentpublic class AuthTask { @Autowired @Qualifier("ProjectServiceImpl") private ProjectService projectService; @Scheduled(cron = "0/1 * * * * ? ")// 1、计划任务执行时,action中进来的方法也可以执行,需要flag进行判断。// 2、前一个时间没有执行完成,时间超过下一个“指定”的时间,计划任务跳过该时间。可以使用每隔多少分的写法来进行下个任务的执行 // 每5秒执行一次 private void executAuth() throws InterruptedException { System.out.println("AuthTask在执行"+Thread.currentThread().getName()); DataSourceSwitch.setDataSourceType(DataSourceInstances.TEST1); List<ProjectModel> list1 = projectService.selectAll(); System.out.println("********************************************************:" + list1.get(0).getProjectName());<pre name="code" class="java"> DataSourceSwitch.setDataSourceType(DataSourceInstances.TEST2); List<ProjectModel> list2 = projectService.selectAll(); System.out.println("********************************************************:" + list2.get(0).getProjectName());}}
0 0
- Spring中mybatis多数据源写法
- spring mybatis 多数据源
- spring Mybatis 多数据源
- spring boot中使用druid多数据源配置Mybatis
- spring boot 中 Mybatis plus 多数据源的配置
- spring(spring-boot) + mybatis多数据源
- spring + mybatis多数据源配置
- Mybatis+Proxool+Spring多数据源切换
- spring+mybatis 多数据源整合
- Spring+mybatis 多数据源整合[备用]
- spring mybatis多数据源实践
- spring+mybatis 多数据源整合
- spring+mybatis 多数据源整合
- spring+mybatis 多数据源整合
- spring MVC+MyBatis 多数据源配置
- Spring下mybatis 多数据源配置
- Spring - 集成多数据源的mybatis
- Spring下mybatis配置多数据源
- lua 中table 基本用法
- R-CNN学习笔记5:Faster R-CNN
- Mpg123源代码详解
- JSP三大编译指令 page指令 include指令 taglib指令
- 一个不错的正则表达式学习网站
- Spring中mybatis多数据源写法
- redis在linux环境中安装
- 【机器学习基础概念】VC维的来龙去脉
- 漫谈兼容内核之十二:Windows的APC机制
- 看懂源码之注释规范
- 深入剖析 redis 事件驱动
- 3.3 Git 分支 - 分支管理
- java bean 与 表单自动匹配
- 数据结构List之Vector