前言 本文主要介紹使用spring boot 配置多個資料庫,即動態資料庫 開始搭建 首先創建一個SpringWeb項目——dynamicdb(spring-boot2.5.7) 然後引入相關依賴lombok、swagger2、mybatis-plus,如下: <?xml version="1.0" ...
前言
本文主要介紹使用spring boot 配置多個資料庫,即動態資料庫開始搭建
首先創建一個SpringWeb項目——dynamicdb(spring-boot2.5.7)
然後引入相關依賴lombok、swagger2、mybatis-plus,如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.7</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>dynamicdb</artifactId> <version>0.0.1-SNAPSHOT</version> <name>dynamicdb</name> <description>dynamicdb</description> <properties> <java.version>8</java.version> </properties> <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> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.28</version> <scope>compile</scope> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> <!-- mybatis-plus 依賴 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- sqlSessionTemplate 依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- spring-boot dynamic 動態資料庫依賴--> <dependency> <groupId>com.baomidou</groupId> <artifactId>dynamic-datasource-spring-boot-starter</artifactId> <version>2.5.6</version> </dependency> <!-- druid 動態簡易使用依賴 @DS("db1")--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.20</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
然後在包dynamicdb下麵創建controller、mapper、dbmodel, 然後在resource下麵創建mapper文件夾及問題,如下圖:
然後配置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<user> normalSql() { //讀取第一個資料庫的值 UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<user> users = mapper.test(); return users; } @GetMapping(value = "/SqlSession/data_from_db1") @ResponseBody @DS("db1") @ApiOperation(value = "從db1資料庫查詢數據", notes = "查詢db1") public List<user> data_from_db1() { //讀取第一個資料庫的值 UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<user> users = mapper.test(); return users; } @GetMapping(value = "/SqlSession/data_from_db2") @ResponseBody @DS("db2") @ApiOperation(value = "從db2資料庫查詢數據", notes = "查詢db2") public List<user> data_from_db2(Integer id) { //讀取第二個資料庫的值 UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<user> 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<user> normalSql() { //讀取第一個資料庫的值 List<user> users = userMapper.test(); return users; } }
----------------------------------------------------------------------------------------------------
到此,Spring配置動態資料庫就已經介紹完了。
Github地址:https://github.com/kiba518/dynamicdb
----------------------------------------------------------------------------------------------------
註:此文章為原創,任何形式的轉載都請聯繫作者獲得授權並註明出處!
若您覺得這篇文章還不錯,請點擊下方的【推薦】,非常感謝!
https://www.cnblogs.com/kiba/p/17473450.html
https://www.cnblogs.com/kiba/