美文网首页
SpringBoot 2.0 多数据源配置

SpringBoot 2.0 多数据源配置

作者: s_j_x | 来源:发表于2018-10-29 16:41 被阅读0次

    项目结构如下图


    image.png

    maven依赖

     <dependencies>
            <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>1.3.2</version>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    

    application.properties配置

    ###datasource1
    spring.datasource.test1.driver-class-name = com.mysql.jdbc.Driver
    spring.datasource.test1.jdbc-url = jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
    spring.datasource.test1.username = root
    spring.datasource.test1.password = root
    ###datasource2
    spring.datasource.test2.driver-class-name = com.mysql.jdbc.Driver
    spring.datasource.test2.jdbc-url = jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=utf-8
    spring.datasource.test2.username = root
    spring.datasource.test2.password = root
    

    数据源1配置

    
    /**
     * @author: sjx
     * @description:
     */
    @Configuration
    @MapperScan(value = "com.mult.source.data1", sqlSessionFactoryRef = "test1SqlSessionFactory")
    public class DataSource1Config {
        @Bean(name = "test1DataSource")
        @ConfigurationProperties(prefix = "spring.datasource.test1")
        @Primary
        public DataSource testDataSource() {
            return DataSourceBuilder.create().build();
        }
    
        @Bean(name = "test1SqlSessionFactory")
        @Primary
        public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource)
                throws Exception {
            SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
            bean.setDataSource(dataSource);
            return bean.getObject();
        }
    
        @Bean(name = "test1TransactionManager")
        @Primary
        public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource") DataSource dataSource) {
            return new DataSourceTransactionManager(dataSource);
        }
    
        @Bean(name = "test1SqlSessionTemplate")
        @Primary
        public SqlSessionTemplate testSqlSessionTemplate(
                @Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
            return new SqlSessionTemplate(sqlSessionFactory);
        }
    }
    

    数据源2配置

    /**
     * @author: sjx
     * @description:
     */
    @Configuration
    @MapperScan(value = "com.mult.source.data2", sqlSessionFactoryRef = "test2SqlSessionFactory")
    public class DataSource2Config {
        @Bean(name = "test2DataSource")
        @ConfigurationProperties(prefix = "spring.datasource.test2")
        public DataSource testDataSource() {
            return DataSourceBuilder.create().build();
        }
    
        @Bean(name = "test2SqlSessionFactory")
        public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource)
                throws Exception {
            SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
            bean.setDataSource(dataSource);
            return bean.getObject();
        }
    
        @Bean(name = "test2TransactionManager")
        public DataSourceTransactionManager testTransactionManager(@Qualifier("test2DataSource") DataSource dataSource) {
            return new DataSourceTransactionManager(dataSource);
        }
    
        @Bean(name = "test2SqlSessionTemplate")
        public SqlSessionTemplate testSqlSessionTemplate(
                @Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
            return new SqlSessionTemplate(sqlSessionFactory);
        }
    }
    

    数据源1mapper

    public interface UserMapperTest01 {
    
        @Insert("INSERT INTO USER(NAME, AGE) VALUES(#{name}, #{age})")
        int insert(@Param("name") String name, @Param("age") Integer age);
    }
    

    数据源2mapper

    public interface UserMapperTest02 {
    
        @Insert("INSERT INTO USER(NAME, AGE) VALUES(#{name}, #{age})")
        int insert(@Param("name") String name, @Param("age") Integer age);
    }
    

    数据源service1

    @Service
    @Slf4j
    public class UserServiceTest01 {
        @Autowired(required = false)
        private UserMapperTest01 userMapperTest01;
    
        public int insertUser(String name, Integer age) {
            int insertUserResult = userMapperTest01.insert(name, age);
            return insertUserResult;
        }
    }
    

    数据源serrvice2

    @Service
    @Slf4j
    public class UserServiceTest02 {
        @Autowired(required = false)
        private UserMapperTest02 userMapperTest02;
    
        @Transactional(transactionManager = "test2TransactionManager")
        public int insertUser(String name, Integer age) {
            int insertUserResult = userMapperTest02.insert(name, age);
            return insertUserResult;
        }
    }
    

    entity

    
    @Data
    public class User {
        private Integer id;
        private Integer age;
        private String name;
    }
    

    测试类

    /**
     * @author: sjx
     * @description:
     */
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class MultDataSourceTest {
        @Autowired
        private UserServiceTest01 serviceTest01;
        @Autowired
        private UserServiceTest02 serviceTest02;
    
        @Test
        public void insertUserTest1() {
            serviceTest01.insertUser("test01", 1);
        }
    
        @Test
        public void insertUserTest2() {
            serviceTest02.insertUser("test02", 2);
        }
    }
    

    测试结果

    数据源1.png 数据源2.png

    在多数据源的情况下,使用@Transactional注解时,应该指定事务管理者
    @Transactional(transactionManager = "test2TransactionManager")

    相关文章

      网友评论

          本文标题:SpringBoot 2.0 多数据源配置

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