springboot配置多数据源

配置方式实现多数据源:

application.properties:

#database1
spring.datasource.test1.url = jdbc:mysql://127.0.0.1:3306/jqapt?serverTimezone=UTC&characterEncoding=utf-8
spring.datasource.test1.username = root
spring.datasource.test1.password = root
spring.datasource.test1.driver-class-name = com.mysql.jdbc.Driver
#database2
spring.datasource.test2.url = jdbc:mysql://127.0.0.1:3306/crm?serverTimezone=UTC&characterEncoding=utf-8
spring.datasource.test2.username = root
spring.datasource.test2.password = root
spring.datasource.test2.driver-class-name = com.mysql.jdbc.Driver

DataSourceConfig1:

//表示这个类为一个配置类
@Configuration
// 配置mybatis的接口类放的地方
@MapperScan(basePackages = "com.test.dao.db1", sqlSessionFactoryRef = "db1SqlSessionFactory")
public class DataSourceConfig1 {
    // 将这个对象放入Spring容器中
    @Bean(name = "db1DataSource")
    // 表示这个数据源是默认数据源
    @Primary
    // 读取application.properties中的配置参数映射成为一个对象
    // prefix表示参数的前缀
    @ConfigurationProperties(prefix = "spring.datasource.test1")
    public DataSource getDateSource1() {
        return DataSourceBuilder.create().build();
    }
    @Bean(name = "db1SqlSessionFactory")
    // 表示这个数据源是默认数据源
    @Primary
    // @Qualifier表示查找Spring容器中名字为db1DataSource的对象
    public SqlSessionFactory test1SqlSessionFactory(@Qualifier("db1DataSource") DataSource datasource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        bean.setMapperLocations(
                // 设置mybatis的xml所在位置
                new PathMatchingResourcePatternResolver().getResources("classpath*:mapping/db1/*.xml"));
        return bean.getObject();
    }
    @Bean("db1SqlSessionTemplate")
    // 表示这个数据源是默认数据源
    @Primary
    public SqlSessionTemplate test1sqlsessiontemplate(
            @Qualifier("db1SqlSessionFactory") SqlSessionFactory sessionfactory) {
        return new SqlSessionTemplate(sessionfactory);
    }
}

DataSourceConfig2:

//表示这个类为一个配置类
@Configuration
// 配置mybatis的接口类放的地方
@MapperScan(basePackages = "com.test.dao.db2", sqlSessionFactoryRef = "db2SqlSessionFactory")
public class DataSourceConfig2 {
    // 将这个对象放入Spring容器中
    @Bean(name = "db2DataSource")
    // 读取application.properties中的配置参数映射成为一个对象
    // prefix表示参数的前缀
    @ConfigurationProperties(prefix = "spring.datasource.test2")
    public DataSource getDateSource2() {
        return DataSourceBuilder.create().build();
    }
    @Bean(name = "db2SqlSessionFactory")
    // @Qualifier表示查找Spring容器中名字为db2DataSource的对象
    public SqlSessionFactory test1SqlSessionFactory(@Qualifier("db2DataSource") DataSource datasource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        bean.setMapperLocations(
                // 设置mybatis的xml所在位置
                new PathMatchingResourcePatternResolver().getResources("classpath*:mapping/db2/*.xml"));
        return bean.getObject();
    }
    @Bean("db2SqlSessionTemplate")
    public SqlSessionTemplate test1sqlsessiontemplate(
            @Qualifier("db2SqlSessionFactory") SqlSessionFactory sessionfactory) {
        return new SqlSessionTemplate(sessionfactory);
    }
}

TestController:

@RestController
@RequestMapping("/test")
public class TestController {
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private ContactMapper contactMapper;
    @RequestMapping("/test")
    public String test(){
        return "访问成功!";
    }
    @RequestMapping("/db1")
    public String db1(){
        CustomerExample example = new CustomerExample();
        List<Customer> customerList = customerMapper.selectByExample(example);
        String s = JSON.toJSONString(customerList);
        return "DB1 : "+s;
    }
    @RequestMapping("/db2")
    public String db2(){
        ContactExample example = new ContactExample();
        List<Contact> contactList = contactMapper.selectByExample(example);
        String s = JSON.toJSONString(contactList);
        return "DB2 : "+s;
    }
}

SpringbootStart:

@SpringBootApplication
@MapperScan(value = "com.test.dao")
public class SpringbootStart extends SpringBootServletInitializer {
    public static void main(String[] args){
        SpringApplication.run(SpringbootStart.class,args);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值