# spring-boot-demo-multi-datasource-mybatis
> 此 demo 主要演示了 Spring Boot 如何集成 Mybatis 的多数据源。可以自己基于AOP实现多数据源,这里基于 Mybatis-Plus 提供的一个优雅的开源的解决方案来实现。
## 准备工作
准备两个数据源,分别执行如下建表语句
```mysql
DROP TABLE IF EXISTS `multi_user`;
CREATE TABLE `multi_user`(
`id` bigint(64) NOT NULL,
`name` varchar(50) DEFAULT NULL,
`age` int(30) DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB
AUTO_INCREMENT = 1
CHARACTER SET = utf8
COLLATE = utf8_general_ci;
```
## 导入依赖
```xml
* User实体类 *
* * @package: com.xkcoding.multi.datasource.mybatis.model * @description: User实体类 * @author: yangkai.shen * @date: Created in 2019-01-21 14:19 * @copyright: Copyright (c) 2019 * @version: V1.0 * @modified: yangkai.shen */ @Data @TableName("multi_user") @NoArgsConstructor @AllArgsConstructor @Builder public class User implements Serializable { private static final long serialVersionUID = -1923859222295750467L; /** * 主键 */ @TableId(type = IdType.ID_WORKER) private Long id; /** * 姓名 */ private String name; /** * 年龄 */ private Integer age; } ``` ## 数据访问层 `UserMapper.java` > 不需要建对应的xml,只需要继承 BaseMapper 就拥有了大部分单表操作的方法了。 ```java /** ** 数据访问层 *
* * @package: com.xkcoding.multi.datasource.mybatis.mapper * @description: 数据访问层 * @author: yangkai.shen * @date: Created in 2019-01-21 14:28 * @copyright: Copyright (c) 2019 * @version: V1.0 * @modified: yangkai.shen */ public interface UserMapper extends BaseMapper* 数据服务层 *
* * @package: com.xkcoding.multi.datasource.mybatis.service * @description: 数据服务层 * @author: yangkai.shen * @date: Created in 2019-01-21 14:31 * @copyright: Copyright (c) 2019 * @version: V1.0 * @modified: yangkai.shen */ public interface UserService extends IService* 数据服务层 实现 *
* * @package: com.xkcoding.multi.datasource.mybatis.service.impl * @description: 数据服务层 实现 * @author: yangkai.shen * @date: Created in 2019-01-21 14:37 * @copyright: Copyright (c) 2019 * @version: V1.0 * @modified: yangkai.shen */ @Service @DS("slave") public class UserServiceImpl extends ServiceImpl* 启动器 *
* * @package: com.xkcoding.multi.datasource.mybatis * @description: 启动器 * @author: yangkai.shen * @date: Created in 2019-01-21 14:19 * @copyright: Copyright (c) 2019 * @version: V1.0 * @modified: yangkai.shen */ @SpringBootApplication @MapperScan(basePackages = "com.xkcoding.multi.datasource.mybatis.mapper") public class SpringBootDemoMultiDatasourceMybatisApplication { public static void main(String[] args) { SpringApplication.run(SpringBootDemoMultiDatasourceMybatisApplication.class, args); } } ``` ## 配置文件 `application.yml` ```yaml spring: datasource: dynamic: datasource: master: username: root password: root url: jdbc:mysql://127.0.0.1:3306/spring-boot-demo?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=GMT%2B8 driver-class-name: com.mysql.cj.jdbc.Driver slave: username: root password: root url: jdbc:mysql://127.0.0.1:3306/spring-boot-demo-2?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=GMT%2B8 driver-class-name: com.mysql.cj.jdbc.Driver mp-enabled: true logging: level: com.xkcoding.multi.datasource.mybatis: debug ``` ## 测试类 ```java /** ** 测试主从数据源 *
* * @package: com.xkcoding.multi.datasource.mybatis.service.impl * @description: 测试主从数据源 * @author: yangkai.shen * @date: Created in 2019-01-21 14:45 * @copyright: Copyright (c) 2019 * @version: V1.0 * @modified: yangkai.shen */ @Slf4j public class UserServiceImplTest extends SpringBootDemoMultiDatasourceMybatisApplicationTests { @Autowired private UserService userService; /** * 主从库添加 */ @Test public void addUser() { User userMaster = User.builder().name("主库添加").age(20).build(); userService.addUser(userMaster); User userSlave = User.builder().name("从库添加").age(20).build(); userService.save(userSlave); } /** * 从库查询 */ @Test public void testListUser() { List