本文转自:https://blog.csdn.net/qq_15071263/article/details/96591059
文章目录
Springboot - 使用mybatis 构建多数据源应用
1、初始化2个数据库
3、初始化数据
4、创建一个springboot 应用
5、编写配置文件
6、编写数据库配置
7、编写数据源配置
8、编写实体类
9、编写Mapper 接口
10、编写测试代码进行测试
Springboot - 使用mybatis 构建多数据源应用
1、初始化2个数据库
使用docker 构建2个数据库
docker run --name mysql-1 -p 3306:3306 -d -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7
docker run --name mysql-2 -p 3307:3306 -d -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7
创建完毕
3、初始化数据
我们创建2个数据库,每个库建立一张表,插入一下测试数据
4、创建一个springboot 应用
创建过程省略
5、编写配置文件
spring:
datasource:
rtx:
jdbc-url: jdbc:mysql://localhost:3306/rtx?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password:
titan:
jdbc-url: jdbc:mysql://localhost:3307/titan?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
6、编写数据库配置
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;
/**
* @author Created by 谭健 on 2019/7/19. 星期五. 10:02.
* © All Rights Reserved.
*/
@Configuration
public class DataSourceConfig {
@Bean
@Primary
@ConfigurationProperties("spring.datasource.rtx")
public DataSource rtx(){
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties("spring.datasource.titan")
public DataSource titan(){
return DataSourceBuilder.create().build();
}
}
7、编写数据源配置
数据源一
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import javax.sql.DataSource;
/**
* @author Created by 谭健 on 2019/7/19. 星期五. 10:06.
* © All Rights Reserved.
*/
@Configuration
@MapperScan(basePackages = "com.wretchant.manydb.mapper.rtx",sqlSessionFactoryRef = "sessionFactoryRTX" )
public class RTXMyBatisDataSourceConfig {
@Bean
@Primary
public SqlSessionFactory sessionFactoryRTX(@Qualifier("rtx")DataSource dataSource)throws Exception{
SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource);
return sessionFactoryBean.getObject();
}
@Bean
@Primary
public SqlSessionTemplate sessionTemplateRTX(@Qualifier("sessionFactoryRTX") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}
数据源二
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
/**
* @author Created by 谭健 on 2019/7/19. 星期五. 10:12.
* © All Rights Reserved.
*/
@Configuration
@MapperScan(basePackages = "com.wretchant.manydb.mapper.titan",sqlSessionFactoryRef = "sessionFactoryTitan" )
public class TITANMyBatisDataSourceConfig {
@Bean
public SqlSessionFactory sessionFactoryTitan(@Qualifier("titan") DataSource dataSource)throws Exception{
SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource);
return sessionFactoryBean.getObject();
}
@Bean
public SqlSessionTemplate sessionTemplateTitan(@Qualifier("sessionFactoryTitan")SqlSessionFactory sqlSessionFactory)throws Exception{
return new SqlSessionTemplate(sqlSessionFactory);
}
}
8、编写实体类
/**
* @author Created by 谭健 on 2019/7/19. 星期五. 10:36.
* © All Rights Reserved.
*/
@Data
public class Amd {
private Integer id;
private String name;
}
/**
* @author Created by 谭健 on 2019/7/19. 星期五. 10:36.
* © All Rights Reserved.
*/
@Data
public class Inter {
private Integer id;
private String name;
}
9、编写Mapper 接口
import com.wretchant.manydb.domain.Inter;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @author Created by 谭健 on 2019/7/19. 星期五. 10:37.
* © All Rights Reserved.
*/
@Repository
public interface InterMapper {
@Select("select * from inter")
List<Inter> findAll();
}
import com.wretchant.manydb.domain.Amd;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @author Created by 谭健 on 2019/7/19. 星期五. 10:38.
* © All Rights Reserved.
*/
@Repository
public interface AmdMapper {
@Select("select * from amd")
List<Amd> findAll();
}
10、编写测试代码进行测试
import com.wretchant.manydb.mapper.rtx.InterMapper;
import com.wretchant.manydb.mapper.titan.AmdMapper;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ManyDbApplicationTests {
@Autowired
private InterMapper interMapper;
@Autowired
private AmdMapper amdMapper;
@Test
public void contextLoads() {
Assert.assertEquals(5, interMapper.findAll().size());
Assert.assertEquals(6, amdMapper.findAll().size());
interMapper.findAll().forEach(System.out::println);
amdMapper.findAll().forEach(System.out::println);
}
}
还可参考: