SpringBoot整合Mybatis多数据源

本文利用MyBatis-Spring-Boot-Starter整合springboot和mybatis,无xml配置,实现多数据源配置。官方文档参考

以下示例代码:

1、数据源1 。指明project.demo.mod_user包下的mappers将使用sqlSessionFactory_mysql1。

@Configuration
@ConfigurationProperties(locations = "classpath:server.yml",prefix = "mybatis.mysql1")
@MapperScan(sqlSessionFactoryRef = "sqlSessionFactory_mysql1",
        basePackages = {"project.demo.mod_user"})
public class DataSourceMysql1Conf {

    private String url;
    private String username;
    private String password;

    @Bean(name = "dataSource_mysql1")
    @Primary
    public DataSource dataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setTestWhileIdle(true);
        dataSource.setValidationQuery("select 1");
        return dataSource;
    }

    /**
     * spring transaction mng
     */
    @Bean(name = "transactionManager_mysql1")
    @Primary
    public DataSourceTransactionManager transactionManager(@Qualifier("dataSource_mysql1") DataSource rdsDataSource) {
        return new DataSourceTransactionManager(rdsDataSource);
    }

    /**
     * mybatis session factory
     */
    @Bean(name = "sqlSessionFactory_mysql1")
    @Primary
    public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource_mysql1")DataSource rdsDataSource) throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(rdsDataSource);
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setCacheEnabled(true);
        sessionFactory.setConfiguration(configuration);
        return sessionFactory.getObject();
    }

    // setter/getter ...
}

2、数据源2 。

@Configuration
@ConfigurationProperties(locations = "classpath:server.yml",prefix = "mybatis.mysql2")
@MapperScan(sqlSessionFactoryRef = "sqlSessionFactory_mysql2",
        basePackages = {"project.demo.mod_other"})
public class DataSourceMysql2Conf {

    private String url;
    private String username;
    private String password;

    @Bean(name = "dataSource_mysql2")
    public DataSource dataSource2(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setTestWhileIdle(true);
        dataSource.setValidationQuery("select 1");
        return dataSource;
    }

    /**
     * spring transaction mng
     */
    @Bean(name = "transactionManager_mysql2")
    public DataSourceTransactionManager transactionManager(@Qualifier("dataSource_mysql2") DataSource rdsDataSource) {
        return new DataSourceTransactionManager(rdsDataSource);
    }

    @Bean(name = "sqlSessionFactory_mysql2")
    public SqlSessionFactory sqlSessionFactory2(@Qualifier("dataSource_mysql2")DataSource rdsDataSource) throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(rdsDataSource);
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setCacheEnabled(true);
        sessionFactory.setConfiguration(configuration);
        return sessionFactory.getObject();
    }
    
    // setter/getter ...
}

3、mapper

@Mapper
public interface UserMapper {
    @Select("select * from user where id=#{param1}")
    User findById(int id);
}

4、执行

@Component
@Transactional(transactionManager = "transactionManager_mysql1",rollbackFor = Exception.class)
public class InitRunner implements CommandLineRunner {

    @Autowired
    private UserMapper userMapper;

    @Override
    public void run(String... args) throws Exception {
        /** userMapper使用的是mysql1的数据源 */
        System.out.println(userMapper.findById(10));
    }

}

5、依赖(gradle)

    compile("org.springframework.boot:spring-boot-starter-web")
    compile 'org.mybatis:mybatis:3.4.2'
    compile 'mysql:mysql-connector-java:5.1.30'
    compile group: 'com.alibaba', name: 'druid', version: '1.0.26'
    compile group: 'org.mybatis.spring.boot', name: 'mybatis-spring-boot-starter', version: '1.2.0'
    原文作者:水木酱
    原文地址: https://segmentfault.com/a/1190000008398069
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞