SpringBoot多数据源详细配置与使用(包含数据源和事务配置)
SpringBoot项目数据库配置文件中配置多个数据源:
#********* primary jdbc ************************** spring.datasource.druid.primary.url=jdbc:mysql://127.0.0.1:3306/one?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true&useSSL=false spring.datasource.druid.primary.username=test1 spring.datasource.druid.primary.password=test1 spring.datasource.druid.primary.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.druid.primary.test-on-borrow=true spring.datasource.druid.primary.test-while-idle=true #********* primary jdbc ************************** #********* two jdbc ************************** spring.datasource.druid.two.url=jdbc:mysql://127.0.0.2:3306/two?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true&useSSL=false spring.datasource.druid.two.username=test2 spring.datasource.druid.two.password=test2 spring.datasource.druid.two.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.druid.two.test-on-borrow=true spring.datasource.druid.two.test-while-idle=true #********* two jdbc **************************
数据源one(即主数据源)配置:
package com.test.common.config; import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import javax.sql.DataSource; /** * <p>Description: primary数据库配置类 </p>*/ @Configuration @MapperScan(basePackages = {"com.test.one.dao"}, sqlSessionTemplateRef = "sqlSessionTemplate") public class DataSourceConfig { @Bean(name = "dataSource")public DataSource Datasource() { return DruidDataSourceBuilder.create().build(); } /** * 返回primary数据库的会话工厂 * @param ds * @return * @throws Exception */ @Bean(name = "sqlSessionFactory") public SqlSessionFactory SqlSessionFactory(@Qualifier("dataSource") DataSource ds) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(ds); return bean.getObject(); } @Bean(name = "transactionManager") public DataSourceTransactionManager transactionManager(@Qualifier("dataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } /** * 返回primary数据库的会话模板 * @param sessionFactory * @return * @throws Exception */ @Bean(name = "sqlSessionTemplate") public SqlSessionTemplate SqlSessionTemplate(@Qualifier("sqlSessionFactory") SqlSessionFactory sessionFactory) throws Exception { return new SqlSessionTemplate(sessionFactory); } }
@MapperScan(basePackages = {"com.test.one.dao"}, sqlSessionTemplateRef = "sqlSessionTemplate")项目启动扫描
com.test.one.dao路径下的dao使用主数据源
package com.test.common.config; import org.springframework.aop.Advisor; import org.springframework.aop.aspectj.AspectJExpressionPointcut; import org.springframework.aop.support.DefaultPointcutAdvisor; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.TransactionDefinition; import org.springframework.transaction.interceptor.*; import java.util.Collections; import java.util.HashMap; import java.util.Map; /** * <p>Description: primary事务配置 </p>*/ @Configurationpublic class TransactionConfig { /* * 通过AOP切面设置全局事务,拦截service包下面所有方法 * AOP术语:通知(Advice)、连接点(Joinpoint)、切入点(Pointcut)、切面(Aspect)、目标(Target)、代理(Proxy)、织入(Weaving) */ /** * 事务超时时间 */ private static final int TX_METHOD_TIMEOUT = 5; /** * 定义切点变量:拦截指定包下所有类的所有方法,返回值类型任意的方法 */ private static final String AOP_POINTCUT_EXPRESSION = "execution(* com.test.*.*.impl.*.*(..))"; /** * <p>Description: springBoot事务配置</p>*/ @Bean(name="txAdvice") public TransactionInterceptor txAdvice( @Qualifier("transactionManager") PlatformTransactionManager transactionManager){ /*事务管理规则,声明具备事务管理的方法名*/ NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource(); /*只读事物、不做更新删除等 当前存在事务就用当前的事务,当前不存在事务就创建一个新的事务*/ RuleBasedTransactionAttribute readOnlyRule = new RuleBasedTransactionAttribute(); //设置当前事务是否为只读事务,true为只读*/ readOnlyRule.setReadOnly(true); //transactiondefinition 定义事务的隔离级别; // PROPAGATION_NOT_SUPPORTED事务传播级别5,以非事务运行,如果当前存在事务,则把当前事务挂起 readOnlyRule.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED); RuleBasedTransactionAttribute requireRule = new RuleBasedTransactionAttribute(); //抛出异常后执行切点回滚*/ requireRule.setRollbackRules(Collections.singletonList(new RollbackRuleAttribute(Exception.class))); //PROPAGATION_REQUIRED:事务隔离性为1,若当前存在事务,则加入该事务;如果当前没有事务,则创建一个新的事务。这是默认值。 */ requireRule.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); //设置事务失效时间,如果超过5秒,则回滚事务 requireRule.setTimeout(TX_METHOD_TIMEOUT); Map<String, TransactionAttribute> txMap = new HashMap<>(20); txMap.put("add*",requireRule); txMap.put("save*", requireRule); txMap.put("insert*",requireRule); txMap.put("delete*",requireRule); txMap.put("remove*",requireRule); txMap.put("update*",requireRule); txMap.put("modify*",requireRule); txMap.put("get*",readOnlyRule); txMap.put("query*", readOnlyRule); txMap.put("find*", readOnlyRule); txMap.put("select*",readOnlyRule); source.setNameMap(txMap); TransactionInterceptor txAdvice = new TransactionInterceptor(transactionManager, source); return txAdvice; } /** * 利用AspectJExpressionPointcut设置切面=切点+通知(写成内部bean的方式) */ @Bean(name="txAdviceAdvisor") public Advisor txAdviceAdvisor(@Qualifier("txAdvice") TransactionInterceptor txAdvice){ //切面(Aspect):切面就是通知和切入点的结合。通知和切入点共同定义了关于切面的全部内容——它的功能、在何时和何地完成其功能。 AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); //声明和设置需要拦截的方法,用切点语言描写 pointcut.setExpression(AOP_POINTCUT_EXPRESSION); return new DefaultPointcutAdvisor(pointcut, txAdvice); } }
数据源two配置:
package com.test.common.config; import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import javax.sql.DataSource; /** * <p>Description: two数据库配置类 </p>*/ @Configuration @ConditionalOnProperty(prefix = "spring.datasource.druid.two" ,name = "url", matchIfMissing = false) @MapperScan(basePackages = {"com.test.two.dao"}, sqlSessionTemplateRef = "twoSqlSessionTemplate") public class twoDataSourceConfig { @Bean(name = "twoDataSource") @ConfigurationProperties(prefix = "spring.datasource.druid.two") public DataSource twoDatasource() { return DruidDataSourceBuilder.create().build(); } /** * 返回two数据库的会话工厂 * @param ds * @return * @throws Exception */ @Bean(name = "twoSqlSessionFactory") public SqlSessionFactory twoSqlSessionFactory(@Qualifier("twoDataSource") DataSource ds) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(ds); return bean.getObject(); } @Bean(name = "twoTransactionManager") public DataSourceTransactionManager twoTransactionManager(@Qualifier("twoDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } /** * 返回two数据库的会话模板 * @param sessionFactory * @return * @throws Exception */ @Bean(name = "twoSqlSessionTemplate") public SqlSessionTemplate twoSqlSessionTemplate(@Qualifier("twoSqlSessionFactory") SqlSessionFactory sessionFactory) throws Exception { return new SqlSessionTemplate(sessionFactory); } }
@MapperScan(basePackages = {"com.test.two.dao"}, sqlSessionTemplateRef = "twoSqlSessionTemplate")项目启动扫描
com.test.two.dao路径下的dao使用two数据源
package com.test.common.config; import org.springframework.aop.Advisor; import org.springframework.aop.aspectj.AspectJExpressionPointcut; import org.springframework.aop.support.DefaultPointcutAdvisor; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.TransactionDefinition; import org.springframework.transaction.interceptor.*; import java.util.Collections; import java.util.HashMap; import java.util.Map; /** * <p>Description: two事务配置 </p>*/ @Configuration @ConditionalOnProperty(name ="spring.datasource.druid.two.driver-class-name",havingValue="com.mysql.cj.jdbc.Driver") public class TransactionTwoConfig { /* * 通过AOP切面设置全局事务,拦截service包下面所有方法 * AOP术语:通知(Advice)、连接点(Joinpoint)、切入点(Pointcut)、切面(Aspect)、目标(Target)、代理(Proxy)、织入(Weaving) */ /** * 事务超时时间 */ private static final int TX_METHOD_TIMEOUT = 5; /** * 定义切点变量:拦截指定包下所有类的所有方法,返回值类型任意的方法 */ private static final String AOP_POINTCUT_EXPRESSION = "execution(* com.test.*.*.impl.*.*(..))"; /** * <p>Description: springBoot事务配置</p>*/ @Bean(name="txAdvice-two") public TransactionInterceptor txAdvice( @Qualifier("twoTransactionManager") PlatformTransactionManager transactionManager){ /*事务管理规则,声明具备事务管理的方法名*/ NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource(); /*只读事物、不做更新删除等 当前存在事务就用当前的事务,当前不存在事务就创建一个新的事务*/ RuleBasedTransactionAttribute readOnlyRule = new RuleBasedTransactionAttribute(); //设置当前事务是否为只读事务,true为只读*/ readOnlyRule.setReadOnly(true); //transactiondefinition 定义事务的隔离级别; // PROPAGATION_NOT_SUPPORTED事务传播级别5,以非事务运行,如果当前存在事务,则把当前事务挂起 readOnlyRule.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED); RuleBasedTransactionAttribute requireRule = new RuleBasedTransactionAttribute(); //抛出异常后执行切点回滚*/ requireRule.setRollbackRules(Collections.singletonList(new RollbackRuleAttribute(Exception.class))); //PROPAGATION_REQUIRED:事务隔离性为1,若当前存在事务,则加入该事务;如果当前没有事务,则创建一个新的事务。这是默认值。 */ requireRule.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); //设置事务失效时间,如果超过5秒,则回滚事务 requireRule.setTimeout(TX_METHOD_TIMEOUT); Map<String, TransactionAttribute> txMap = new HashMap<>(20); txMap.put("add*",requireRule); txMap.put("save*", requireRule); txMap.put("insert*",requireRule); txMap.put("delete*",requireRule); txMap.put("remove*",requireRule); txMap.put("update*",requireRule); txMap.put("modify*",requireRule); txMap.put("get*",readOnlyRule); txMap.put("query*", readOnlyRule); txMap.put("find*", readOnlyRule); txMap.put("select*",readOnlyRule); source.setNameMap(txMap); TransactionInterceptor txAdvice = new TransactionInterceptor(transactionManager, source); return txAdvice; } /** * 利用AspectJExpressionPointcut设置切面=切点+通知(写成内部bean的方式) */ @Bean(name="txAdviceAdvisor-two") public Advisor txAdviceAdvisor(@Qualifier("txAdvice-two") TransactionInterceptor txAdvice){ //切面(Aspect):切面就是通知和切入点的结合。通知和切入点共同定义了关于切面的全部内容——它的功能、在何时和何地完成其功能。 AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); //声明和设置需要拦截的方法,用切点语言描写 pointcut.setExpression(AOP_POINTCUT_EXPRESSION); return new DefaultPointcutAdvisor(pointcut, txAdvice); } }