From ccd994aea04603adac43d86572bba8a4d03c816b Mon Sep 17 00:00:00 2001 From: "Yangkai.Shen" <237497819@qq.com> Date: Sat, 19 Jan 2019 16:30:08 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20spring-boot-demo-multi-datasource-?= =?UTF-8?q?mybatis=20=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TODO.md | 4 +- .../pom.xml | 33 ++++++++++++ .../sql/db.sql | 10 ++++ ...DemoMultiDatasourceMybatisApplication.java | 15 ++++++ .../datasource/mybatis/mapper/UserMapper.java | 20 +++++++ .../multi/datasource/mybatis/model/User.java | 49 +++++++++++++++++ .../mybatis/service/UserService.java | 27 ++++++++++ .../mybatis/service/impl/UserServiceImpl.java | 39 ++++++++++++++ .../src/main/resources/application.properties | 0 .../src/main/resources/application.yml | 18 +++++++ .../service/impl/UserServiceImplTest.java | 52 +++++++++++++++++++ 11 files changed, 265 insertions(+), 2 deletions(-) create mode 100644 spring-boot-demo-multi-datasource-mybatis/sql/db.sql create mode 100644 spring-boot-demo-multi-datasource-mybatis/src/main/java/com/xkcoding/multi/datasource/mybatis/mapper/UserMapper.java create mode 100644 spring-boot-demo-multi-datasource-mybatis/src/main/java/com/xkcoding/multi/datasource/mybatis/model/User.java create mode 100644 spring-boot-demo-multi-datasource-mybatis/src/main/java/com/xkcoding/multi/datasource/mybatis/service/UserService.java create mode 100644 spring-boot-demo-multi-datasource-mybatis/src/main/java/com/xkcoding/multi/datasource/mybatis/service/impl/UserServiceImpl.java delete mode 100644 spring-boot-demo-multi-datasource-mybatis/src/main/resources/application.properties create mode 100644 spring-boot-demo-multi-datasource-mybatis/src/main/resources/application.yml create mode 100644 spring-boot-demo-multi-datasource-mybatis/src/test/java/com/xkcoding/multi/datasource/mybatis/service/impl/UserServiceImplTest.java diff --git a/TODO.md b/TODO.md index 6872f5a..82c5fa1 100644 --- a/TODO.md +++ b/TODO.md @@ -1,5 +1,5 @@ # spring-boot-demo 项目 todo-list -## 模块计划(已完成:40 / 51) +## 模块计划(已完成:41 / 51) - [x] ~~spring-boot-demo-helloworld(Helloworld 示例)~~ - [x] ~~spring-boot-demo-properties(读取配置文件信息)~~ @@ -49,7 +49,7 @@ - [x] ~~spring-boot-demo-mongodb(集成 MongoDb)~~ - [x] ~~spring-boot-demo-neo4j(集成 neo4j 图数据库)~~ - [x] ~~spring-boot-demo-docker(打包成 docker 镜像)~~ -- [ ] spring-boot-demo-multi-datasource-jpa(集成JPA多数据源) +- [x] ~~spring-boot-demo-multi-datasource-jpa(集成JPA多数据源)~~ - [ ] spring-boot-demo-multi-datasource-mybatis(集成mybatis多数据源) - [ ] spring-boot-demo-sharding-jdbc(集成 sharding-jdbc 实现分库分表) diff --git a/spring-boot-demo-multi-datasource-mybatis/pom.xml b/spring-boot-demo-multi-datasource-mybatis/pom.xml index c7634b4..92c39dd 100644 --- a/spring-boot-demo-multi-datasource-mybatis/pom.xml +++ b/spring-boot-demo-multi-datasource-mybatis/pom.xml @@ -33,6 +33,39 @@ spring-boot-starter-test test + + + mysql + mysql-connector-java + + + + com.baomidou + dynamic-datasource-spring-boot-starter + 2.5.0 + + + + com.baomidou + mybatis-plus-boot-starter + 3.0.7.1 + + + + org.projectlombok + lombok + true + + + + cn.hutool + hutool-all + + + + com.google.guava + guava + diff --git a/spring-boot-demo-multi-datasource-mybatis/sql/db.sql b/spring-boot-demo-multi-datasource-mybatis/sql/db.sql new file mode 100644 index 0000000..b3f4dd0 --- /dev/null +++ b/spring-boot-demo-multi-datasource-mybatis/sql/db.sql @@ -0,0 +1,10 @@ +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; diff --git a/spring-boot-demo-multi-datasource-mybatis/src/main/java/com/xkcoding/multi/datasource/mybatis/SpringBootDemoMultiDatasourceMybatisApplication.java b/spring-boot-demo-multi-datasource-mybatis/src/main/java/com/xkcoding/multi/datasource/mybatis/SpringBootDemoMultiDatasourceMybatisApplication.java index aea77c8..5fbc29c 100644 --- a/spring-boot-demo-multi-datasource-mybatis/src/main/java/com/xkcoding/multi/datasource/mybatis/SpringBootDemoMultiDatasourceMybatisApplication.java +++ b/spring-boot-demo-multi-datasource-mybatis/src/main/java/com/xkcoding/multi/datasource/mybatis/SpringBootDemoMultiDatasourceMybatisApplication.java @@ -1,9 +1,24 @@ package com.xkcoding.multi.datasource.mybatis; +import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +/** + *

+ * 启动器 + *

+ * + * @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) { diff --git a/spring-boot-demo-multi-datasource-mybatis/src/main/java/com/xkcoding/multi/datasource/mybatis/mapper/UserMapper.java b/spring-boot-demo-multi-datasource-mybatis/src/main/java/com/xkcoding/multi/datasource/mybatis/mapper/UserMapper.java new file mode 100644 index 0000000..51a6dd5 --- /dev/null +++ b/spring-boot-demo-multi-datasource-mybatis/src/main/java/com/xkcoding/multi/datasource/mybatis/mapper/UserMapper.java @@ -0,0 +1,20 @@ +package com.xkcoding.multi.datasource.mybatis.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.xkcoding.multi.datasource.mybatis.model.User; + +/** + *

+ * 数据访问层 + *

+ * + * @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 { +} diff --git a/spring-boot-demo-multi-datasource-mybatis/src/main/java/com/xkcoding/multi/datasource/mybatis/model/User.java b/spring-boot-demo-multi-datasource-mybatis/src/main/java/com/xkcoding/multi/datasource/mybatis/model/User.java new file mode 100644 index 0000000..f895d3c --- /dev/null +++ b/spring-boot-demo-multi-datasource-mybatis/src/main/java/com/xkcoding/multi/datasource/mybatis/model/User.java @@ -0,0 +1,49 @@ +package com.xkcoding.multi.datasource.mybatis.model; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; + +/** + *

+ * 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; +} \ No newline at end of file diff --git a/spring-boot-demo-multi-datasource-mybatis/src/main/java/com/xkcoding/multi/datasource/mybatis/service/UserService.java b/spring-boot-demo-multi-datasource-mybatis/src/main/java/com/xkcoding/multi/datasource/mybatis/service/UserService.java new file mode 100644 index 0000000..f9d84fd --- /dev/null +++ b/spring-boot-demo-multi-datasource-mybatis/src/main/java/com/xkcoding/multi/datasource/mybatis/service/UserService.java @@ -0,0 +1,27 @@ +package com.xkcoding.multi.datasource.mybatis.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import com.xkcoding.multi.datasource.mybatis.model.User; + +/** + *

+ * 数据服务层 + *

+ * + * @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 { + + /** + * 添加 User + * + * @param user 用户 + */ + void addUser(User user); +} diff --git a/spring-boot-demo-multi-datasource-mybatis/src/main/java/com/xkcoding/multi/datasource/mybatis/service/impl/UserServiceImpl.java b/spring-boot-demo-multi-datasource-mybatis/src/main/java/com/xkcoding/multi/datasource/mybatis/service/impl/UserServiceImpl.java new file mode 100644 index 0000000..bef1f77 --- /dev/null +++ b/spring-boot-demo-multi-datasource-mybatis/src/main/java/com/xkcoding/multi/datasource/mybatis/service/impl/UserServiceImpl.java @@ -0,0 +1,39 @@ +package com.xkcoding.multi.datasource.mybatis.service.impl; + +import com.baomidou.dynamic.datasource.annotation.DS; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.xkcoding.multi.datasource.mybatis.mapper.UserMapper; +import com.xkcoding.multi.datasource.mybatis.model.User; +import com.xkcoding.multi.datasource.mybatis.service.UserService; +import org.springframework.stereotype.Service; + +import java.util.List; + +/** + *

+ * TODO + *

+ * + * @package: com.xkcoding.multi.datasource.mybatis.service.impl + * @description: TODO + * @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 implements UserService { + + /** + * 类上 {@code @DS("slave")} 代表默认从库,在方法上写 {@code @DS("master")} 代表默认主库 + * + * @param user 用户 + */ + @DS("master") + @Override + public void addUser(User user) { + baseMapper.insert(user); + } +} diff --git a/spring-boot-demo-multi-datasource-mybatis/src/main/resources/application.properties b/spring-boot-demo-multi-datasource-mybatis/src/main/resources/application.properties deleted file mode 100644 index e69de29..0000000 diff --git a/spring-boot-demo-multi-datasource-mybatis/src/main/resources/application.yml b/spring-boot-demo-multi-datasource-mybatis/src/main/resources/application.yml new file mode 100644 index 0000000..91dcf70 --- /dev/null +++ b/spring-boot-demo-multi-datasource-mybatis/src/main/resources/application.yml @@ -0,0 +1,18 @@ +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 \ No newline at end of file diff --git a/spring-boot-demo-multi-datasource-mybatis/src/test/java/com/xkcoding/multi/datasource/mybatis/service/impl/UserServiceImplTest.java b/spring-boot-demo-multi-datasource-mybatis/src/test/java/com/xkcoding/multi/datasource/mybatis/service/impl/UserServiceImplTest.java new file mode 100644 index 0000000..a57b732 --- /dev/null +++ b/spring-boot-demo-multi-datasource-mybatis/src/test/java/com/xkcoding/multi/datasource/mybatis/service/impl/UserServiceImplTest.java @@ -0,0 +1,52 @@ +package com.xkcoding.multi.datasource.mybatis.service.impl; + +import cn.hutool.json.JSONUtil; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.xkcoding.multi.datasource.mybatis.SpringBootDemoMultiDatasourceMybatisApplicationTests; +import com.xkcoding.multi.datasource.mybatis.model.User; +import com.xkcoding.multi.datasource.mybatis.service.UserService; +import lombok.extern.slf4j.Slf4j; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.List; + +/** + *

+ * 测试主从数据源 + *

+ * + * @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 list = userService.list(new QueryWrapper<>()); + log.info("【list】= {}", JSONUtil.toJsonStr(list)); + } +} \ No newline at end of file