-
使用场景
在一个项目内同时要对多个数据库进行CRUD操作,或者项目需要对主从库进行读写分离。
-
整合目标
代码内通过注解的形式即可动态切换数据源。
-
前置条件
Springboot整合Mytatis通用mapper,参考:
http://note.youdao.com/noteshare?id=9332ec957e2b61ec9706d53870b25204
-
pom依赖
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <!-- javax扩展包 --> <javax.version>3.0.1</javax.version> <!-- mybatis通用mapper --> <tkmybatis.version>1.1.0</tkmybatis.version> <mybatis.version>1.3.0</mybatis.version> <sfjson.version>2.4</sfjson.version> <!-- 连接池 --> <druid.version>1.1.9</druid.version> </properties> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <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.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <!-- javax扩展包 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${javax.version}</version> </dependency> <!-- mybatis通用mapper --> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>${tkmybatis.version}</version> </dependency> <!-- mybatis分页插件pagehelper --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>${tkmybatis.version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>${sfjson.version}</version> <classifier>jdk15</classifier> </dependency> <!-- Druid数据源 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</a
Springboot动态切换数据源并整合Mybatis通用mapper
最新推荐文章于 2024-07-23 11:21:09 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)