• SpringBoot 分包方式多数据源


    1、引入依赖

    <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-parent</artifactId>
            <version>2.0.6.RELEASE</version>
        </parent>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
            </dependency>
            <!--mybatis起步依赖-->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>1.1.1</version>
            </dependency>
            <!--mysql依赖-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
            </dependency>
        </dependencies>

    2、配置application.properties中配置多个数据源

    #slave dataSource数据源
    spring.datasource.slave.driver-class-name =com.mysql.jdbc.Driver
    spring.datasource.slave.jdbc-url =jdbc:mysql://localhost:3306/study?useUnicode=true&characterEncoding=utf-8
    spring.datasource.slave.username =root
    spring.datasource.slave.password =root
    #master  dataSource数据源
    spring.datasource.master.driver-class-name =com.mysql.jdbc.Driver
    spring.datasource.master.jdbc-url =jdbc:mysql://localhost:3306/study01?useUnicode=true&characterEncoding=utf-8
    spring.datasource.master.username =root
    spring.datasource.master.password =root

     3、创建多个配置类

       数据源1;

    @Configuration
    @MapperScan(basePackages =DataSourceMaterConfig.PACKAGE,sqlSessionFactoryRef = "masterSqlSessionFactory")
    public class DataSourceMaterConfig {
        // master dao所在的包
        public static final String PACKAGE = "com.yehui.mapper.master";
        //配置文件所在目录
        //private static final String MAPPER_LOCATION = "classpath:mapper/master/*.xml";
    
        @Bean(name = "mapper/master")
        @Primary //默认访问
        @ConfigurationProperties(prefix = "spring.datasource.master")
        public DataSource msaterDataSource() {
            return DataSourceBuilder.create().build();
        }
    
        // 创建Session
        @Bean(name = "masterSqlSessionFactory")
        @Primary //默认访问
        public SqlSessionFactory sqlSessionFactory() throws Exception {
            final SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
            sqlSessionFactoryBean.setDataSource(msaterDataSource());
           /* 使用配置文件是加入
           Resource[] mapperLocations = new PathMatchingResourcePatternResolver()
                    .getResources(DataSourceMaterConfig.MAPPER_LOCATION);
            sqlSessionFactoryBean.setMapperLocations(mapperLocations);*/
            return sqlSessionFactoryBean.getObject();
        }
    
        // 数据源事务管理器
        @Bean(name = "masterDataSourceTransactionManager")
        @Primary //默认访问
        public DataSourceTransactionManager masterDataSourceTransactionManager() {
            return new DataSourceTransactionManager(msaterDataSource());
        }
    }

    数据源2:

    @Configuration
    @MapperScan(basePackages =DataSourceSalveConfig.PACKAGE,sqlSessionFactoryRef = "slaveSqlSessionFactory")
    public class DataSourceSalveConfig {
        // slave dao所在的包
        public static final String PACKAGE = "com.yehui.mapper.slave";
        //配置文件所在目录
        private static final String MAPPER_LOCATION = "classpath:mapper/slave/*.xml";
    
        @Bean(name = "mapper/slave")
        @ConfigurationProperties(prefix = "spring.datasource.slave")
        public DataSource slaveDataSource() {
            return DataSourceBuilder.create().build();
        }
    
        // 创建Session
        @Bean(name = "slaveSqlSessionFactory")
        public SqlSessionFactory sqlSessionFactory() throws Exception {
            final SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
            sqlSessionFactoryBean.setDataSource(slaveDataSource());
            /*使用配置文件时加入
    
            Resource[] mapperLocations = new PathMatchingResourcePatternResolver()
                    .getResources(DataSourceSalveConfig.MAPPER_LOCATION);
            sqlSessionFactoryBean.setMapperLocations(mapperLocations);*/
            return sqlSessionFactoryBean.getObject();
        }
    
        // 数据源事务管理器
        @Bean(name = "slaveDataSourceTransactionManager")
        public DataSourceTransactionManager slaveDataSourceTransactionManager() {
            return new DataSourceTransactionManager(slaveDataSource());
        }
    }

    4、创建多个包mapper

    数据源1:

    package com.yehui.mapper.master;
    
    import org.apache.ibatis.annotations.Select;
    
    import java.util.List;
    import java.util.Map;
    
    public interface MasterMapper {
    
        @Select("SELECT * FROM tb_user")
        public List<Map<String,Object>> selectList();
    }

    数据源2:

    package com.yehui.mapper.slave;
    
    import org.apache.ibatis.annotations.Select;
    
    import java.util.List;
    import java.util.Map;
    
    public interface SlaveMapper {
        @Select("SELECT * FROM tb_user")
        public List<Map<String,Object>> selectList();
    }

    5、创建service类

    @Service
    public class UserService {
    
        @Autowired
        private MasterMapper masterMapper;
    
        @Autowired
        private SlaveMapper savlesMapper;
    
        public List<Map<String,Object>> selectList1(){
            return masterMapper.selectList();
        }
    
        public List<Map<String,Object>> selectList2(){
            return savlesMapper.selectList();
        }
    }

    6、测试类

    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class DataSourceTest {
        @Autowired
        private UserService userService;
        @Test
        public void  test1(){
            List<Map<String, Object>> mapList = userService.selectList1();
            System.out.println(mapList);
        }
        @Test
        public void  test2(){
            List<Map<String, Object>> mapList = userService.selectList2();
            System.out.println(mapList);
        }
    }

     注意配置过程中如果报此错误

    No qualifying bean of type 'javax.sql.DataSource' available: more than one 'primary' bean found amon

    导致原因@Primary这个注解只能在一个包下存在,不能在其他存在

     

  • 相关阅读:
    404页面应该怎么做?
    网站被百度和google封了,怎么办?
    如何下载没有提供下载按钮的网页视频
    新手学习.net编程计划-1
    windows7旗舰版下载出现蓝屏代码50怎么办?
    微软已于10月底停止销售预装Windows 7/8.1的电脑
    Html代码seo优化最佳布局实例讲解
    下载类网站的SEO优化方面技巧
    命名空间“System.Windows.Forms”中不存在类型或命名空间名称“DataVisualization”。是否缺少程序集引用?
    SQL、LINQ、Lambda 三种用法(转)
  • 原文地址:https://www.cnblogs.com/cxyyh/p/10630476.html
Copyright © 2020-2023  润新知