添加夹包 c3p0-0.9.5.jar mchange-commons-java-0.2.8.jar
结构:
根绝结构 不同 注入数据源
db-config.properties
one.jdbc.driverClass=com.mysql.jdbc.Driver one.jdbc.url=jdbc:mysql://localhost:3306/fusionweb?useUnicode=true&characterEncoding=utf-8 one.jdbc.user=root one.jdbc.password=root one.jdbc.initialPoolSize=5 one.jdbc.minPoolSize=5 one.jdbc.maxPoolSize=20 one.jdbc.checkoutTimeout=20000 one.jdbc.idleConnectionTestPeriod=120 one.jdbc.maxIdleTime=60 one.jdbc.maxStatements=100 one.jdbc.testConnectionOnCheckout=false two.jdbc.driverClass=com.mysql.jdbc.Driver two.jdbc.url=jdbc:mysql://localhost:3306/self_help?useUnicode=true&characterEncoding=utf-8 two.jdbc.user=root two.jdbc.password=root two.jdbc.initialPoolSize=5 two.jdbc.minPoolSize=5 two.jdbc.maxPoolSize=20 two.jdbc.checkoutTimeout=20000 two.jdbc.idleConnectionTestPeriod=120 two.jdbc.maxIdleTime=60 two.jdbc.maxStatements=100 two.jdbc.testConnectionOnCheckout=false three.jdbc.driverClass=com.mysql.jdbc.Driver three.jdbc.url=jdbc:mysql://localhost:3306/fusion_prob?useUnicode=true&characterEncoding=utf-8 three.jdbc.user=root three.jdbc.password=root three.jdbc.initialPoolSize=5 three.jdbc.minPoolSize=5 three.jdbc.maxPoolSize=20 three.jdbc.checkoutTimeout=20000 three.jdbc.idleConnectionTestPeriod=120 three.jdbc.maxIdleTime=60 three.jdbc.maxStatements=100 three.jdbc.testConnectionOnCheckout=false
<?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:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:jms="http://www.springframework.org/schema/jms" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:oxm="http://www.springframework.org/schema/oxm" 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-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.1.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd" > <context:annotation-config /> <aop:aspectj-autoproxy /> <context:component-scan base-package="com"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:db-config.properties</value> </list> </property> </bean> <bean id="dataSourceOne" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${one.jdbc.driverClass}" /> <property name="jdbcUrl" value="${one.jdbc.url}" /> <property name="user" value="${one.jdbc.user}" /> <property name="password" value="${one.jdbc.password}" /> <property name="initialPoolSize" value="${one.jdbc.initialPoolSize}" /> <property name="minPoolSize" value="${one.jdbc.minPoolSize}" /> <property name="maxPoolSize" value="${one.jdbc.maxPoolSize}" /> <property name="checkoutTimeout" value="${one.jdbc.checkoutTimeout}" /> <property name="idleConnectionTestPeriod" value="${one.jdbc.idleConnectionTestPeriod}" /> <property name="maxIdleTime" value="${one.jdbc.maxIdleTime}" /> <property name="maxStatements" value="${one.jdbc.maxStatements}" /> <property name="testConnectionOnCheckout" value="${one.jdbc.testConnectionOnCheckout}" /> </bean> <bean id="dataSourceTwo" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${two.jdbc.driverClass}" /> <property name="jdbcUrl" value="${two.jdbc.url}" /> <property name="user" value="${two.jdbc.user}" /> <property name="password" value="${two.jdbc.password}" /> <property name="initialPoolSize" value="${two.jdbc.initialPoolSize}" /> <property name="minPoolSize" value="${two.jdbc.minPoolSize}" /> <property name="maxPoolSize" value="${two.jdbc.maxPoolSize}" /> <property name="checkoutTimeout" value="${two.jdbc.checkoutTimeout}" /> <property name="idleConnectionTestPeriod" value="${two.jdbc.idleConnectionTestPeriod}" /> <property name="maxIdleTime" value="${two.jdbc.maxIdleTime}" /> <property name="maxStatements" value="${two.jdbc.maxStatements}" /> <property name="testConnectionOnCheckout" value="${two.jdbc.testConnectionOnCheckout}" /> </bean> <bean id="dataSourceThree" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${three.jdbc.driverClass}" /> <property name="jdbcUrl" value="${three.jdbc.url}" /> <property name="user" value="${three.jdbc.user}" /> <property name="password" value="${three.jdbc.password}" /> <property name="initialPoolSize" value="${three.jdbc.initialPoolSize}" /> <property name="minPoolSize" value="${three.jdbc.minPoolSize}" /> <property name="maxPoolSize" value="${three.jdbc.maxPoolSize}" /> <property name="checkoutTimeout" value="${three.jdbc.checkoutTimeout}" /> <property name="idleConnectionTestPeriod" value="${three.jdbc.idleConnectionTestPeriod}" /> <property name="maxIdleTime" value="${three.jdbc.maxIdleTime}" /> <property name="maxStatements" value="${three.jdbc.maxStatements}" /> <property name="testConnectionOnCheckout" value="${three.jdbc.testConnectionOnCheckout}" /> </bean> <bean id="dataSourceInterceptor" class="com.commons.dynamictasource.DataSourceInterceptor" /> <aop:config> <aop:aspect id="dataSourceAspect" ref="dataSourceInterceptor"> <aop:pointcut id="daoOne" expression="execution(* com.onweb.*.*(..))" /> <aop:pointcut id="daoTwo" expression="execution(* com.twoweb.*.*(..))" /> <aop:pointcut id="daoThree" expression="execution(* com.threeweb.*.*(..))" /> <aop:before pointcut-ref="daoOne" method="setdataSourceOne" /> <aop:before pointcut-ref="daoTwo" method="setdataSourceTwo" /> <aop:before pointcut-ref="daoThree" method="setdataSourceThree" /> </aop:aspect> </aop:config> <bean id="dataSource" class="com.commons.dynamictasource.DynamicDataSource"> <property name="targetDataSources"> <map key-type="java.lang.String"> <entry value-ref="dataSourceOne" key="dataSourceOne"></entry> <entry value-ref="dataSourceTwo" key="dataSourceTwo"></entry> <entry value-ref="dataSourceThree" key="dataSourceThree"></entry> </map> </property> <property name="defaultTargetDataSource" ref="dataSourceOne"> </property> </bean> </beans>
package com.commons.dynamictasource; public class DatabaseContextHolder { private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>(); public static void setCustomerType(String customerType) { contextHolder.set(customerType); } public static String getCustomerType() { return contextHolder.get(); } public static void clearCustomerType() { contextHolder.remove(); } }
package com.commons.dynamictasource; import org.aspectj.lang.JoinPoint; import org.springframework.stereotype.Component; @Component public class DataSourceInterceptor { public void setdataSourceOne(JoinPoint jp) { DatabaseContextHolder.setCustomerType("dataSourceOne"); } public void setdataSourceTwo(JoinPoint jp) { DatabaseContextHolder.setCustomerType("dataSourceTwo"); } public void setdataSourceThree(JoinPoint jp) { DatabaseContextHolder.setCustomerType("dataSourceThree"); } }
package com.commons.dynamictasource; import java.sql.SQLFeatureNotSupportedException; import java.util.logging.Logger; import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; public class DynamicDataSource extends AbstractRoutingDataSource{ @Override protected Object determineCurrentLookupKey() { return DatabaseContextHolder.getCustomerType(); } @Override public Logger getParentLogger() throws SQLFeatureNotSupportedException { return null; } }
捐助开发者
在兴趣的驱动下,写一个免费
的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。
谢谢您的赞助,我会做的更好!
相关推荐
spring jdbcTemplate 多数据源切换数据库操作,junit测试。
通过SpringAop实现多数据源的动态切换 并采用Druid连接池,可扩展继续增加数据库配置。
spring boot AOP注解方式实现多数据源整合(从零开始搭建框架到配置多数据源实践小例子)
用springboot 集成mybatis多数据源,用aop实现的动态切换,支持事务,不会使aop动态切换失效。注意:此代码不涉及分布式事务,如果需要分布式事务 需要采取其他方案。
spring boot整合druid连接池,采用注解方式实现多数据源动态切换 jdk版本1.8 spring boot版本1.5.14
spring mybatis 多数据源动态切换
java spring 多数据源目前版本为mysql版本 利用spring aop 切面,自动切换数据源。
Spring+SpringMvc+MybatisPlus实现多数据源切换、利用自定义Aop注解,只需要在需要切换数据库的方法上加上注解即可实现、极大避免了代码冗余。
Spring AOP + SpringMVC +Mybatis做动态数据源,实现读写分离Spring AOP + SpringMVC +Mybatis做动态数据源,实现读写分离Spring AOP + SpringMVC +Mybatis做动态数据源,实现读写分离
使用aop进行多数据源切换 springMVC+spring+mybatis增删改查的使用。dk8+tomcat8+mysql+Eclipse+maven。spring+spring mvc+mybatis+bootstrap+jquery
Spring AOP 动态多数据源的实例详解 当项目中使用到读写分离的时候,我们就会遇到多数据源的问题。多数据源让人最头痛的,不是配置多个数据源,而是如何能灵活动态的切换数据源。例如在一个spring和Mybatis的框架的...
SpringBoot使用AOP完成多数据源切换,下载后导入即可使用,JDK1.8
aop(AOP自定义多数据源)、multi-datasource-mybatis(使用Mybatis集成多数据源)、quartz(定时任务)等).zip基于深度学习并实战 spring boot 的项目合集(已集成batis-aop(AOP自定义多数据源)、multi-datasource-...
【资源说明】 1、该资源包括项目的全部源码,...基于深度学习并实战 spring boot 的项目,成功集成batis-aop(AOP自定义多数据源)、multi-datasource-mybatis(使用Mybatis集成多数据源)、quartz(定时任务)、rabbit.zip
主要是利用Spring AOP实现动态数据源,和数据缓存操作。
SpringBoot整合mybatis-plus实现多数据源的动态切换且支持分页查询,案例以postgresql和oracle数据库为数据源,分别使用mybatis-plus分页插件和pagehelper分页插件实现分页查询。
springboot集合mysql基于aop动态数据源切换
springboot-mybatis整合多数据源方式之一aop法,动态切换数据源,在sprintboot 1.5.9下测试通过
基于springboot,多数据源,用druid整合,SpringAop进行数据源切换,包括实现事务切换,内有功能备注