前言
本文主要介绍使用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.toymoban.com/news/detail-479198.html
文章来源地址https://www.toymoban.com/news/detail-479198.html
到了这里,关于Spring配置动态数据库的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!