spring boot多数据源配置

一、maven依赖

spring-boot-starter-web 、mybatis-spring-boot-starter、mysql、mybatis-generator-core
spring-boot-starter-aop、mysql-connector-java

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.4.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.learn</groupId>
	<artifactId>spring-boot-shiro</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>spring-boot-shiro</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
		<mybatis.generator.version>1.3.7</mybatis.generator.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.0.1</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
		<dependency>
			<groupId>org.mybatis.generator</groupId>
			<artifactId>mybatis-generator-core</artifactId>
			<version>${mybatis.generator.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-aop -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-aop</artifactId>
			<scope>runtime</scope>
		</dependency>

	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<!-- <plugin> https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-maven-plugin 
				<groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> 
				<version>${mybatis.generator.version}</version> <configuration> <overwrite>true</overwrite> 
				</configuration> </plugin> -->
		</plugins>
	</build>

</project>

二、多数据源yml配置

spring:
  datasource:
    primary:
      jdbc-url: jdbc:mysql://localhost:3306/shiro?serverTimezone=GMT&useUnicode=true&characterEncoding=UTF8&autoReconnect=true&useAffectedRows=true&useSSL=false
      driver-class-name: com.mysql.jdbc.Driver
      username: root
      password: root
    secondary:
      jdbc-url: jdbc:mysql://localhost:3306/shiro?serverTimezone=GMT&useUnicode=true&characterEncoding=UTF8&autoReconnect=true&useAffectedRows=true&useSSL=false
      driver-class-name: com.mysql.jdbc.Driver
      username: root
      password: root

mybatis:
  mapper-locations:
  - classpath:mapper/*.xml
  
logging:
  level:
    com.learn.shiro.service.impl:
      debug

注意问题:1. datasource 中的jdbc-url 必须为此(不能用url代替),否则报错,
2. driver-class-name username password
不能用data-password: 或者data-username等形式
3. mybatis 配置
3.1 配置yml,mapper所在位置

mybatis:
  mapper-locations:
  - classpath:mapper/*.xml

3.2 多数据源配置

@Configuration
@MapperScan(basePackages = "com.learn.shiro.mapper")
public class DatasourceConfig {
	// 配置数据源
	@Bean
	@ConfigurationProperties(prefix = "spring.datasource.primary")
	@Qualifier("primaryDataSource")
	@Primary
	public DataSource primaryDataSource() {
		return DataSourceBuilder.create().build();
	}

	@Bean
	@ConfigurationProperties(prefix = "spring.datasource.secondary")
	@Qualifier("secondaryDataSource")
	public DataSource secondaryDataSource() {
		return DataSourceBuilder.create().build();
	}

	// 声明mybatis sqlsessionfactory
	@Bean
	public SqlSessionFactory sqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource) throws Exception {
		SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
		sqlSessionFactoryBean.setDataSource(dataSource);
		sqlSessionFactoryBean
				.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
		return sqlSessionFactoryBean.getObject();
	}

	// 事物管理
	@Bean
	public PlatformTransactionManager platformTransactionManager(
			@Qualifier("primaryDataSource") DataSource dataSource) {
		return new DataSourceTransactionManager(dataSource);
	}

	@Bean
	public TransactionInterceptor txAdvice() {

		NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource();
		TransactionInterceptor transactionInterceptor = new TransactionInterceptor();
		transactionInterceptor.setTransactionManager(platformTransactionManager(primaryDataSource()));
		RuleBasedTransactionAttribute readOnlyTx = new RuleBasedTransactionAttribute();
		readOnlyTx.setReadOnly(false);
		readOnlyTx.setIsolationLevel(TransactionDefinition.ISOLATION_REPEATABLE_READ);
		readOnlyTx.setRollbackRules(Collections.singletonList(new RollbackRuleAttribute(BusinessException.class)));
		readOnlyTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);

		Map<String, TransactionAttribute> nameMap = new HashMap<>();
		nameMap.put("add*", readOnlyTx);
		nameMap.put("save*", readOnlyTx);
		nameMap.put("insert*", readOnlyTx);
		nameMap.put("create*", readOnlyTx);
		nameMap.put("update*", readOnlyTx);
		nameMap.put("edit*", readOnlyTx);
		nameMap.put("delete*", readOnlyTx);
		nameMap.put("remove*", readOnlyTx);
		nameMap.put("process*", readOnlyTx);
		source.setNameMap(nameMap);
		transactionInterceptor.setTransactionAttributeSources(source);
		return transactionInterceptor;
	}

	@Bean
	public Advisor txAdviceAdvisor() {
		AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
		pointcut.setExpression("execution(* com.learn.shiro..*.impl..*.(..))");
		return new DefaultPointcutAdvisor(pointcut, txAdvice());
	}

}

mybatis 配置线:配置数据源-》配置SqlsessionFactory->@MapperScan(basePackages = “com.learn.shiro.mapper”)
mybatis事务配置:
数据源配置-》PlatformTransactionManager-》TransactionInterceptor(txAdvice)->Advisor(txAdviceAdvisor)

切点表达式配置也很重要,此处配置为* com.learn.shiro….serviceable.impl….(…)) 出现不能回滚的情况

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页