Spring配置动态数据库

首先创建一个SpringWeb项目——dynamicdb(spring-boot2.5.7)

然后引入相关依赖lombok、swagger2、mybatis-plus,如下:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.5.7
         
    
    com.example
    dynamicdb
    0.0.1-SNAPSHOT
    dynamicdb
    dynamicdb
    
        8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.projectlombok
            lombok
            1.18.28
            compile
        
        
            io.springfox
            springfox-swagger2
            2.9.2
        
        
            io.springfox
            springfox-swagger-ui
            2.9.2
        
 
        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.4.2
        
        
            mysql
mysql-connector-java
runtime



org.springframework.boot
spring-boot-starter-jdbc



com.baomidou
dynamic-datasource-spring-boot-starter
2.5.6



com.alibaba
druid-spring-boot-starter
1.1.20
  



org.springframework.boot
spring-boot-maven-plugin


然后在包dynamicdb下面创建controller、mapper、dbmodel, 然后在resource下面创建mapper文件夹及问题,如下图:

Spring配置动态数据库_第1张图片

然后配置application,启用swagger2和添加mapper扫描,如下:

@EnableSwagger2 @Log4j2 @SpringBootApplication @MapperScan("com.example.dynamicdb.mapper") public class DynamicdbApplication { ​ public static void main(String[] args) { SpringApplication.run(DynamicdbApplication.class, args); } }

然后编辑resources下的application.yml,如下:

server:
  port: 8082
  servlet:
    context-path: /api
spring:
  servlet:
    multipart:
      max-file-size: 50MB
      max-request-size: 50MB
  datasource:
    dynamic: #使用动态数据库配置
      primary: db1 # 配置默认数据库
      datasource:
        db1: # 数据源1
          driver-class-name: com.mysql.cj.jdbc.Driver
          url: jdbc:mysql://localhost/db1
          username: root
          password: 1qaz!QAZ
        db2: # 数据源2
          driver-class-name: com.mysql.cj.jdbc.Driver
          url: jdbc:mysql://localhost/db2
          username: root
          password: 1qaz!QAZ
      druid:
        initial-size: 1
        max-active: 20
        min-idle: 1
        max-wait: 60000
  autoconfigure:
    exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure # 去除druid配置 
mybatis-plus:
  mapper-locations: classpath:mapper/*.xml #指定resources下的mapper文件夹下是存储映射XML的文件夹然后创建SqlSessionController,使用SqlSession来加载Mapper,代码如下:

@RestController
public class SqlSessionController {
​
​
    @Autowired
    private SqlSession sqlSession;
​
    @GetMapping(value = "/SqlSession/normalSql")
    @ResponseBody
    @ApiOperation(value = "默认查询,从主数据库查询", notes = "当前配置的主数据是db1-primary: db1")
    public List normalSql() {
        //读取第一个数据库的值
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List users = mapper.test();
        return users;
    }
    @GetMapping(value = "/SqlSession/data_from_db1")
    @ResponseBody
    @DS("db1")
    @ApiOperation(value = "从db1数据库查询数据", notes = "查询db1")
    public List data_from_db1() {
        //读取第一个数据库的值
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List users = mapper.test();
        return users;
    }
​
    @GetMapping(value = "/SqlSession/data_from_db2")
    @ResponseBody
    @DS("db2")
    @ApiOperation(value = "从db2数据库查询数据", notes = "查询db2")
    public List data_from_db2(Integer id) {
        //读取第二个数据库的值
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List users = mapper.test();
        return users;
    }
}

代码中使用 @DS("db1")来指定该接口内使用的数据库。

再创建一个MapperController,测试使用Mapper直接访问数据库,如下

@RestController
public class MapperController {
​
    @Autowired
    private UserMapper userMapper;
​
    @GetMapping(value = "/Mapper/normalSql")
    @ResponseBody
    @ApiOperation(value = "使用Mapper查询,从主数据库查询", notes = "当前配置的主数据是db1-primary: db1")
    public List normalSql() {
        //读取第一个数据库的值
        List users = userMapper.test();
        return users;
    }
}

你可能感兴趣的:(spring,数据库,java)