美文网首页
Spring Boot MyBatis配置多数据源

Spring Boot MyBatis配置多数据源

作者: 十毛tenmao | 来源:发表于2019-04-19 23:32 被阅读0次

    项目中有时候需要用到多数据源,但是Spring Boot的自动配置并不支持,所以需要手动做很多工作,其实也不难

    整体架构


    项目架构

    添加依赖


    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.0.1</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.15</version>
    </dependency>
    
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    

    First数据源配置


    • FirstDataSourceConfig
    @Configuration
    @MapperScan(basePackages = "com.tenmao.multiplemybatis.mapper.first",sqlSessionFactoryRef = "firstSqlSessionFactory")
    public class FirstDataSourceConfig {
        @Bean(name = "firstDataSource")
        @ConfigurationProperties("datasource.first")
        public DataSource firstDataSource(){
            return DataSourceBuilder.create().build();
        }
    
        @Bean(name = "firstSqlSessionFactory")
        public SqlSessionFactory sqlSessionFactory(@Qualifier("firstDataSource") DataSource dataSource) throws Exception {
            SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
            sessionFactoryBean.setDataSource(dataSource);
            sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
            .getResources("classpath*:mapper/first/*.xml"));
            return sessionFactoryBean.getObject();
        }
    }
    

    Second数据源配置


    • SecondDataSourceConfig
    @Configuration
    @MapperScan(basePackages = "com.tenmao.multiplemybatis.mapper.second",sqlSessionFactoryRef = "secondSqlSessionFactory")
    public class SecondDataSourceConfig {
        @Bean(name = "secondDataSource")
        @ConfigurationProperties("datasource.second")
        public DataSource secondDataSource(){
            return DataSourceBuilder.create().build();
        }
    
        @Bean(name = "secondSqlSessionFactory")
        public SqlSessionFactory sqlSessionFactory(@Qualifier("secondDataSource") DataSource dataSource) throws Exception {
            SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
            sessionFactoryBean.setDataSource(dataSource);
            sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
                    .getResources("classpath*:mapper/second/*.xml"));
            return sessionFactoryBean.getObject();
        }
    }
    

    配置信息


    • application.properties
    datasource.first.jdbcUrl=jdbc:mysql://127.0.0.1:3306/blog-first?useUnicode=true&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2B8
    datasource.first.driverClassName=com.mysql.cj.jdbc.Driver
    datasource.first.username=tenmao
    datasource.first.password=123456
    
    
    datasource.second.jdbcUrl=jdbc:mysql://127.0.0.1:3306/blog-second?useUnicode=true&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2B8
    datasource.second.driverClassName=com.mysql.cj.jdbc.Driver
    datasource.second.username=tenmao
    datasource.second.password=123456
    

    使用


    @SpringBootApplication
    public class MultipleMybatisApplication {
        @Resource
        private PersonMapper personMapper;
        @Resource
        private CarMapper carMapper;
    
        public static void main(String[] args) {
            SpringApplication.run(MultipleMybatisApplication.class, args);
        }
        @Bean
        public CommandLineRunner commandLineRunner(ApplicationContext context) {
            return args -> {
                Person person = personMapper.selectById(1);
                System.out.println(person);
                Car car = carMapper.selectById(1);
                System.out.println(car);
            };
        }
    }
    

    注意事项

    • datasource.first.jdbcUrl:注意是jdbcUrl而不是url
    • 按照这种配置,发现其实并不需要添加注解@EnableAutoConfiguration
    @SpringBootApplication
    @EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class,DataSourceTransactionManagerAutoConfiguration.class, MybatisAutoConfiguration.class})
    

    参考

    相关文章

      网友评论

          本文标题:Spring Boot MyBatis配置多数据源

          本文链接:https://www.haomeiwen.com/subject/hqphgqtx.html