SpirngBoot+Mybatis实现多数据源

项目框架图:

yml配置文件:


server:
  port: 8081

db1url: 
db1username:
db1password: 
db1driverclassname: com.mysql.jdbc.Driver
db2url: 
db2username: 
db2password: 
db2driverclassname: com.mysql.jdbc.Driver

 

看启动类 这里要注意要用exclude把自动配置数据库排除

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@ComponentScan("com.baibing.demo")
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

datasourceConfig类 建立两个数据源:

@Configuration
public class DataSourceConfig {

        @Value("${db1url}")
        private String db1url;

        @Value("${db1username}")
        private String db1username;

        @Value("${db1password}")
        private String db1password;

        @Value("${db1driverclassname}")
        private String db1driverclassname;

        @Value("${db2url}")
        private String db2url;

        @Value("${db2username}")
        private String db2username;

        @Value("${db2password}")
        private String db2password;

        @Value("${db2driverclassname}")
        private String db2driverclassname;

        @Bean(name = "db1")
        public DataSource dataSource1() {
            DruidDataSource datasource = new DruidDataSource();
            datasource.setUrl(db1url);
            datasource.setUsername(db1username);
            datasource.setPassword(db1password);
            datasource.setDriverClassName(db1driverclassname);
            return datasource;
        }




        @Bean(name = "db2")
        public DataSource dataSource2() {
            DruidDataSource datasource = new DruidDataSource();
            datasource.setUrl(db2url);
            datasource.setUsername(db2username);
            datasource.setPassword(db2password);
            datasource.setDriverClassName(db2driverclassname);
            return datasource;
        }



}

mybatis1Config :

@Configuration
@MapperScan(basePackages = {"com.baibing.demo.dao1"}, sqlSessionFactoryRef = "sqlSessionFactory1")
public class MybatisDb1Config {
    @Autowired
    @Qualifier("db1")
    private DataSource db1;


    @Bean
    public SqlSessionFactory sqlSessionFactory1() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(db1);
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("com/baibing/demo/mapper1/*.xml")
        );
        return factoryBean.getObject();
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplate1() throws Exception {
        SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory1());
        return template;
    }
}

同理第二个mybatis配置:

@Configuration
@MapperScan(basePackages = {"com.baibing.demo.dao"}, sqlSessionFactoryRef = "sqlSessionFactory2")
public class MybatisDb2Config {
    @Autowired
    @Qualifier("db2")
    private DataSource db2;

    @Bean
    public SqlSessionFactory sqlSessionFactory2() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(db2);
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("com/baibing/demo/mapper/*.xml")
        );
        return factoryBean.getObject();
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplate2() throws Exception {
        SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory2());
        return template;
    }
}

dao和mapper不再详解

Controller:

@Controller
@RequestMapping("/test")
public class TestController {
    
    
    @Resource
    userDao userdao;

    @Resource
    userDao1 userdao1;

    @RequestMapping("/getUser")
    @ResponseBody
    public User  getUser(@RequestBody String params) {
        User userByName = userdao.getUserByName("baibing");
        return userByName;
    }

    @RequestMapping("/getUser1")
    @ResponseBody
    public User  getUser1(@RequestBody String params) {
        User userByName = userdao1.getUserByName("baibing");
        return userByName;
    }

}

 

分别调用两个接口后可以看出初始化了两个数据源

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱打球的白师傅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值