text:
<file_sep>/*
SQLyog Ultimate v12.3.1 (64 bit)
MySQL - 5.7.21-log
*********************************************************************
*/
/*!40101 SET NAMES utf8 */;

create table `student` (
	`id` tinyint (4),
	`name` varchar (30),
	`sex` char (3),
	`city` varchar (60),
	`age` tinyint (4)
);
insert into `student` (`id`, `name`, `sex`, `city`, `age`) values('1','小明','男','readA','10');
insert into `student` (`id`, `name`, `sex`, `city`, `age`) values('2','小花','女','readA','17');
insert into `student` (`id`, `name`, `sex`, `city`, `age`) values('3','小明','女','readA','11');
<file_sep>package com.example.SBdemo.dao;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;

import com.example.SBdemo.model.Student;

@Mapper
public interface StudentDao {

	List<Student> selectStudentByName(String name);

	void delStduByName(String name);

	List<Student> readStudentBySex(String sex);

}
<file_sep>package com.example.SBdemo.configuration.datasource;

import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;

@Configuration
public class DataSourceConfig {

	/**
	 * @Primary 注解用于标识默认使用的 DataSource Bean， 但不能用于 dynamicDataSource Bean, 否则会产生循环调用
	 *
	 * @return data source
	 */
	@Bean("datasourceWrite")
	@Primary
	@ConfigurationProperties(prefix = "spring.datasource.datasource-write")
	public DataSource datasourceWrite() {
		return DruidDataSourceBuilder.create().build();
	}

	@Bean("datasourceReadA")
	@ConfigurationProperties(prefix = "spring.datasource.datasource-read-a")
	public DataSource datasourceReadA() {
		return DruidDataSourceBuilder.create().build();
	}

	@Bean("datasourceReadB")
	@ConfigurationProperties(prefix = "spring.datasource.datasource-read-b")
	public DataSource datasourceReadB() {
		return DruidDataSourceBuilder.create().build();
	}

	@Bean("dynamicDataSource")
	public DataSource dynamicDataSource() {
		DynamicRoutingDataSource dynamicRoutingDataSource = new DynamicRoutingDataSource();
		Map<Object, Object> dataSourceMap = new HashMap<>();
		dataSourceMap.put(DataSourceKey.write.name(), datasourceWrite());
		dataSourceMap.put(DataSourceKey.readA.name(), datasourceReadA());
		dataSourceMap.put(DataSourceKey.readB.name(), datasourceReadB());

		// 将 write 数据源作为默认指定的数据源
		dynamicRoutingDataSource.setDefaultTargetDataSource(datasourceWrite());
		// 将 write 和 readA,readB 数据源作为指定的数据源
		dynamicRoutingDataSource.setTargetDataSources(dataSourceMap);

		// 将数据源的 key 放到数据源上下文的 key 集合中，用于切换时判断数据源是否有效
		DynamicDataSourceContextHolder.dataSourceKeys.addAll(dataSourceMap.keySet());

		// 将 read 数据源的 key 放在集合中，用于轮循
		DynamicDataSourceContextHolder.readDataSourceKeys.addAll(dataSourceMap.keySet());
		DynamicDataSourceContextHolder.readDataSourceKeys.remove(DataSourceKey.write.name());
		return dynamicRoutingDataSource;
	}

	@Bean
	@ConfigurationProperties(prefix = "mybatis")
	public SqlSessionFactoryBean sqlSessionFactoryBean() {
		SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
		// Here is very important, if don't config this, will can't switch datasource
		// put all datasource into SqlSessionFactoryBean, then will autoconfig
		// SqlSessionFactory
		sqlSessionFactoryBean.setDataSource(dynamicDataSource());
		return sqlSessionFactoryBean;
	}

	/**
	 * Transaction manager platform transaction manager.
	 *
	 * @return the platform transaction manager
	 */
	@Bean
	public PlatformTransactionManager transactionManager() {
		return new DataSourceTransactionManager(dynamicDataSource());
	}

}

languages: Java SQL

repo_name: teaten/SBdemo