• springboot项目部署双数据源


    首先在说明一下在开发中遇到的场景,项目中需要扫描oracle的数据库,是做的分批次扫描,所以需要将扫描的最后的坐标也就是id或者时间戳之类的存入另一个小型的数据库中,也就是H2中。在下一次扫描时取出坐标,作为oracle查询条件进行扫描

    1、导入maven依赖坐标,分别为Oracle,H2,动态数据源baomidou

            <dependency>
                <groupId>com.oracle</groupId>
                <artifactId>ojdbc6</artifactId>
                <version>11.2.0.3</version>
             </dependency>
            <dependency>
                <groupId>com.h2database</groupId>
                <artifactId>h2</artifactId>
                <version>${h2.version}</version>
            </dependency>
    
            <dependency>
                <groupId>com.h2database</groupId>
                <artifactId>connector</artifactId>
                <version>1.0.0</version>
            </dependency>    
         <!--主从配置依赖-->
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
                <version>2.4.2</version>
            </dependency>

    2、主数据源yml配置

      datasource:
        dynamic:
          primary: master
          datasource:
            master:
              username: system
              password: 123456
              driver-class-name: oracle.jdbc.driver.OracleDriver
              url: jdbc:oracle:thin:@127.0.0.1:1521:orcl

    3、从数据源yml配置

      datasource:
        dynamic:
          datasource:
            slave:
              username: root
              password: root
              driver-class-name: org.h2.Driver
              url: jdbc:h2:~/test

    4、最后在服务层实现中添加@DS注解类区分主从数据源

    @Service
    @DS("master")
    public class OrgServiceImpl implements OrgService {
    
        @Autowired
        DataMapper dataMapper;
    
        public List<Map<String, Object>> selectAllMysql(String initId, String cols,String table,String schema,String pk,String rows) {
            return dataMapper.selectAllMysql(initId, cols,table,schema,pk,rows);
        }
    
        public List<Map<String, Object>> selectAllOrcle(String initId, String cols,String table,String schema,String pk,String rows) {
            return dataMapper.selectAllOrcle(initId,cols,table,schema,pk,rows);
        }
    
    
    
        @Override
        public List<Map<String, Object>> selectMysqlByDateTime(String field, String timeField, String param, String startTime, String endTime, String schema, String table) {
            return dataMapper.selectMysqlByDateTime(field, timeField, param, startTime, endTime, schema, table);
        }
    
        @Override
        public List<Map<String, Object>> selectMysqlByLongTime(String field, String timeField,
                                                               String param, Long startTime, Long endTime, String schema, String table) {
            return dataMapper.selectMysqlByLongTime(field, timeField, param, startTime, endTime, schema, table);
        }
    
        @Override
        public List<Map<String, Object>> selectOracleByTimeStamp(String field, String timeField,
                                                                 String param, String startTime, String endTime, String schema, String table) {
            return dataMapper.selectOracleByTimeStamp(field, timeField, param, startTime, endTime, schema, table);
        }
    
        @Override
        public List<Map<String, Object>> selectOracleByNumber(String cols, String timeField, String initId, Long lastExeTime, Long nowTime, String schema, String table) {
            return dataMapper.selectOracleByNumber(cols, timeField, initId, lastExeTime, nowTime, schema, table);
        }
    
        @Override
        public Map<String, Object> selectById(Long id, String field) {
            return dataMapper.selectById(id, field);
        }
    
    }
    @Service
    @DS("slave")
    public class H2DbServiceImpl implements H2DbService {
    
        @Autowired
        DataMapper dataMapper;
    
        @Override
        public Agent selectAgentAll() {
            return dataMapper.selectAgentAll();
        }
    
        @Override
        public String selectInitId() {
            return dataMapper.selectInitId();
        }
    
        @Override
        public String selectSourceTime() {
            return dataMapper.selectSourceTime();
        }
    
        @Override
        public int updateInitId(String initId) {
            return dataMapper.updateInitId(initId);
        }
    
        @Override
        public int updateSourceTime(String sourceTime) {
            return dataMapper.updateSourceTime(sourceTime);
        }
    
        @Override
        public int insert(String initId) {
            return dataMapper.insertInitId(initId,System.currentTimeMillis());
        }
    }
  • 相关阅读:
    匿名对象
    封装性
    1 Django初探
    8 定制10MINs 3
    7 定制10MINs首页2
    5-1 练习css 总结
    6.定制10MINS首页1
    3-1 练习 HTML 总结
    5. css定位 居中
    4 CSS的20/80个知识点
  • 原文地址:https://www.cnblogs.com/liuzhengkun/p/13360532.html
Copyright © 2020-2023  润新知