一、配置文件
spring:
# datasource:
# username: root
# password: 123456
# url: jdbc:mysql://127.0.0.1:3306/jun01?characterEncoding=utf-8&serverTimezone=UTC
# driver-class-name: com.mysql.cj.jdbc.Driverdatasource:# 數據源1onedata:jdbc-url: jdbc:mysql://127.0.0.1:3306/jun01?useUnicode=true&characterEncoding=utf-8username: rootpassword: 123456driver-class-name: com.mysql.cj.jdbc.Driver# 數據源2twodata:jdbc-url: jdbc:mysql://127.0.0.1:3306/jun02?useUnicode=true&characterEncoding=utf-8username: rootpassword: 123456driver-class-name: com.mysql.cj.jdbc.Driver
二、數據源配置類
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;import javax.sql.DataSource;@Configuration
public class DataSourceConfiguration {// Primary 注解是在沒有指明使用哪個數據源的時候指定默認使用的主數據源@Primary@Bean("oneDataSource")@ConfigurationProperties(prefix = "spring.datasource.onedata")public DataSource primaryDataSource() {return DataSourceBuilder.create().build();}@Bean("twoDataSource")@ConfigurationProperties(prefix = "spring.datasource.twodata")public DataSource secondaryDataSource() {return DataSourceBuilder.create().build();}
}
三、數據源與 Mybatis 配置
1、數據源一
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;import javax.annotation.Resource;
import javax.sql.DataSource;@Configuration
// 指定該數據源掃描指定包下面的Mapper接口 與 *.xml文件
@MapperScan(basePackages = "com.south.mapper1",sqlSessionFactoryRef = "sqlSessionFactoryOne",sqlSessionTemplateRef = "sqlSessionTemplateOne")
public class DataSourceOneConfig {// 注入數據源1@Resourceprivate DataSource oneDataSource;@Bean@Primarypublic SqlSessionFactory sqlSessionFactoryOne() throws Exception {SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();sqlSessionFactoryBean.setDataSource(oneDataSource);return sqlSessionFactoryBean.getObject();}@Bean@Primarypublic SqlSessionTemplate sqlSessionTemplateOne() throws Exception {return new SqlSessionTemplate(sqlSessionFactoryOne());}
}
2、數據源二(兩種不同的配置方式,都能實現多數據源的效果)
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;import javax.sql.DataSource;@Configuration
@MapperScan(basePackages = "com.example.mapper2",sqlSessionFactoryRef = "sqlSessionFactoryTwo")
public class DataSourceTwoConfig {// mapper 掃描 xml 文件的路徑private static final String MAPPER_LOCATION = "classpath:mapper2/*.xml";private DataSource twoDataSource;// 通過構造方法進行注入public DataSourceTwoConfig(DataSource dataSource) {this.twoDataSource = dataSource;}@Beanpublic SqlSessionFactory sqlSessionFactoryTwo() throws Exception {SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();// 指定數據源sqlSessionFactoryBean.setDataSource(twoDataSource);/** 獲取xml文件資源對象* 當Mapper接口所對應的.xml文件與Mapper接口文件分離,存儲在 resources* 文件夾下的時候,需要手動指定.xml文件所在的路徑*/Resource[] resources = new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION);sqlSessionFactoryBean.setMapperLocations(resources);return sqlSessionFactoryBean.getObject();}@Beanpublic DataSourceTransactionManager SecondaryDataSourceManager() {return new DataSourceTransactionManager(twoDataSource);}
}
四、使用案例
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;import javax.annotation.Resource;
import java.util.List;@Controller
@RequestMapping("/testController")
public class TestController {// 數據源1Mapper注入@Resourceprivate OneMapper oneMapper;// 數據源2Mapper注入@Resourceprivate TwoMapper twoMapper;@GetMapping("/selectManyDataSouroneMapperceData")@ResponseBodypublic void selectManyDataSourceDatwoMapperta() {// 查詢數據源1中的數據List<String> message = oneMapper.getMessage();System.out.println(message);// 查詢數據源2中的數據List<String> tag = twoMapper.getTag();System.out.println(tag);}
}
?