getSqlFiles() {
30 | return Arrays.asList(
31 | "db/user-postgres.sql"
32 | // ,"db/user-data.sql"
33 | );
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/mybatis-mate-sharding/src/main/java/mybatis/mate/sharding/config/ScheduledConfig.java:
--------------------------------------------------------------------------------
1 | package mybatis.mate.sharding.config;
2 |
3 | import lombok.AllArgsConstructor;
4 | import lombok.extern.slf4j.Slf4j;
5 | import mybatis.mate.sharding.ShardingHealthIndicator;
6 | import org.springframework.boot.actuate.health.Health;
7 | import org.springframework.scheduling.annotation.EnableScheduling;
8 | import org.springframework.scheduling.annotation.Scheduled;
9 | import org.springframework.stereotype.Component;
10 |
11 |
12 | /**
13 | * 如果外部未调用 spring boot 的健康检查 ...
14 | *
15 | * 可以如下使用定时任务检查数据库健康状况,这个方法不是必须的
16 | */
17 | @Slf4j
18 | @Component
19 | @EnableScheduling // 这个开启定时任务,注意请勿重复设置
20 | @AllArgsConstructor
21 | public class ScheduledConfig {
22 | private ShardingHealthIndicator shardingHealthIndicator;
23 |
24 | /**
25 | * 每 15 秒执行一次,健康检查
26 | */
27 | @Scheduled(cron = "0/15 * * * * ? ")
28 | public void dbHealthCheck() {
29 | Health health = shardingHealthIndicator.health();
30 | log.debug("Sharding health: {}", health.toString());
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/mybatis-mate-sharding/src/main/java/mybatis/mate/sharding/controller/UserController.java:
--------------------------------------------------------------------------------
1 | package mybatis.mate.sharding.controller;
2 |
3 | import com.baomidou.mybatisplus.extension.ddl.DdlScript;
4 | import lombok.AllArgsConstructor;
5 | import mybatis.mate.sharding.ShardingKey;
6 | import mybatis.mate.sharding.service.UserService;
7 | import org.springframework.web.bind.annotation.GetMapping;
8 | import org.springframework.web.bind.annotation.RestController;
9 |
10 | import java.io.StringReader;
11 |
12 | @RestController
13 | @AllArgsConstructor
14 | public class UserController {
15 | private DdlScript ddlScript;
16 | private UserService userService;
17 |
18 | // 测试访问 http://localhost:8080/test
19 | @GetMapping("/test")
20 | public boolean testSharding() throws Exception {
21 | this.initMysqlT2();
22 | return userService.test();
23 | }
24 |
25 | // 测试访问 http://localhost:8080/test2
26 | @GetMapping("/test2")
27 | public boolean testSharding2() throws Exception {
28 | this.initMysqlT2();
29 | return userService.testRw();
30 | }
31 |
32 | public void initMysqlT2() throws Exception {
33 | // 切换到 mysql 从库,执行 SQL 脚本
34 | ShardingKey.change("mysqlt2");
35 | ddlScript.run(new StringReader("DELETE FROM user;\n" +
36 | "INSERT INTO user (id, username, password, sex, email) VALUES\n" +
37 | "(20, 'Duo', '123456', 0, 'Duo@baomidou.com');"));
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/mybatis-mate-sharding/src/main/java/mybatis/mate/sharding/entity/User.java:
--------------------------------------------------------------------------------
1 | package mybatis.mate.sharding.entity;
2 |
3 | import lombok.Getter;
4 | import lombok.Setter;
5 | import lombok.ToString;
6 |
7 | /**
8 | *
9 | */
10 | @Getter
11 | @Setter
12 | @ToString
13 | public class User {
14 | private Long id;
15 | private String username;
16 | private String password;
17 | private Integer sex;
18 | private String email;
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/mybatis-mate-sharding/src/main/java/mybatis/mate/sharding/mapper/UserMapper.java:
--------------------------------------------------------------------------------
1 | package mybatis.mate.sharding.mapper;
2 |
3 | import com.baomidou.mybatisplus.core.mapper.BaseMapper;
4 | import mybatis.mate.annotation.Sharding;
5 | import mybatis.mate.sharding.config.MyShardingStrategy;
6 | import mybatis.mate.sharding.entity.User;
7 | import org.apache.ibatis.annotations.Mapper;
8 | import org.apache.ibatis.annotations.Select;
9 |
10 | @Mapper
11 | //@Sharding("mysql")
12 | public interface UserMapper extends BaseMapper {
13 |
14 | @Sharding(value = "postgres", strategy = MyShardingStrategy.class)
15 | @Select("select id from \"public\".\"user\" where username=#{username}")
16 | Long selectByUsername(String username);
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/mybatis-mate-sharding/src/main/java/mybatis/mate/sharding/service/UserService.java:
--------------------------------------------------------------------------------
1 | package mybatis.mate.sharding.service;
2 |
3 | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
4 | import com.baomidou.mybatisplus.core.toolkit.IdWorker;
5 | import com.baomidou.mybatisplus.core.toolkit.Wrappers;
6 | import jakarta.annotation.Resource;
7 | import mybatis.mate.sharding.entity.User;
8 | import mybatis.mate.sharding.mapper.UserMapper;
9 | import org.springframework.stereotype.Service;
10 | import org.springframework.transaction.annotation.Transactional;
11 |
12 | @Service
13 | public class UserService {
14 | @Resource
15 | private UserMapper mapper;
16 | final String TEST_USER_NAME = "青苗";
17 |
18 | public boolean test() {
19 |
20 | // 插入 mysql t1 主库
21 | this.insertT1();
22 |
23 | // mysql t2 从库未做同步,这里是查不出结果的
24 | Long count = mapper.selectCount(getWrapperByUsername(TEST_USER_NAME));
25 | if (count > 0) {
26 | System.err.println("不符合执行预期");
27 | return false;
28 | }
29 |
30 | // mysql t2 查初始化 Duo 记录
31 | System.err.println(mapper.selectCount(getWrapperByUsername("Duo")) == 1);
32 |
33 | // 切换数据源 postgres 查询 Tom 记录
34 | Long id = mapper.selectByUsername("Tom");
35 | System.err.println(null != id && id.equals(3L));
36 | return true;
37 | }
38 |
39 | @Transactional
40 | public boolean testRw() {
41 |
42 | // 插入 mysql t1 主库
43 | this.insertT1();
44 |
45 | // 区别 test() 例子,这里是同一个事务不切换数据源,直接查询 t1 的数据库
46 | Long count = mapper.selectCount(getWrapperByUsername(TEST_USER_NAME));
47 | if (count < 1) {
48 | System.err.println("不符合执行预期");
49 | return false;
50 | }
51 |
52 | return true;
53 | }
54 |
55 | private void insertT1() {
56 | // 删除历史数据
57 | mapper.delete(Wrappers.lambdaQuery().eq(User::getUsername, TEST_USER_NAME));
58 | // 插入 mysql t1 主库
59 | User user = new User();
60 | user.setId(IdWorker.getId());
61 | user.setUsername(TEST_USER_NAME);
62 | user.setSex(1);
63 | user.setEmail("jobob@qq.com");
64 | user.setPassword("123");
65 | mapper.insert(user);
66 | }
67 |
68 | protected LambdaQueryWrapper getWrapperByUsername(String username) {
69 | return Wrappers.lambdaQuery().eq(User::getUsername, username);
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/mybatis-mate-sharding/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | # Mybatis Mate 配置
2 | mybatis-mate:
3 | cert:
4 | # 请添加微信wx153666购买授权,不白嫖从我做起! 测试证书会失效,请勿正式环境使用
5 | grant: thisIsTestLicense
6 | license: Qw46ncaiXykSqBByQDTGLFmior/pd8kzu3B4aR1BW3SQkXer4DtwQ0o5f6LNqjnUg3qRSo/3w+UmqYoSXA8ZBrTf16OcXFbLl7V3WRCBLeoRDi1c9BTQjumEzt/8HTzM0n+/hg+GU57OJbDvBZQI40U3abKT84qCW0EqjNA2Kg4fOlaHhXn6w3mEHXc3aMw8H+rBtxWSXyZOYUp83QVcTfh+n9sGDeys3NF7zjOrFxb24wB5DTpg7yirXnjeZznqPFwnyu45mACLPP4T8rFjHlW18+Pv2xoqlUCXx4QRsdBQEUDi26u4z0dba+O2S/vl7QGNfXZvGhbmv59IJK/FLA==
7 | sharding:
8 | health: true # 健康检测数据源需要设置 SQL 检测语句 test-query: select 1
9 | primary: mysql # 默认选择数据源,该项配置必须有否则无法加载启动
10 | datasource:
11 | mysql:
12 | - key: t1
13 | driver-class-name: com.mysql.cj.jdbc.Driver
14 | url: jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
15 | username: root
16 | schema: test
17 | test-query: select 1
18 | - key: t2
19 | cluster: slave # 从库读写分离时候负责 sql 查询操作,主库 master 默认可以不写
20 | driver-class-name: com.mysql.cj.jdbc.Driver
21 | url: jdbc:mysql://localhost:3306/test2?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
22 | username: root
23 | schema: test2
24 | test-query: select 1
25 | hikari:
26 | maxLifetime: 1765000
27 | maximumPoolSize: 10
28 | minimum-idle: 3
29 | postgres:
30 | - key: t1
31 | driver-class-name: org.postgresql.Driver
32 | url: jdbc:postgresql://localhost:5432/test
33 | username: postgres
34 | password: 123456
35 | schema: test
36 | test-query: select 1
37 | hikari:
38 | maxLifetime: 1765000
39 | maximumPoolSize: 10
40 | minimum-idle: 3
41 |
42 | # 允许bean定义重写
43 | spring:
44 | main:
45 | allow-bean-definition-overriding: true
46 |
47 | # 配置健康检查
48 | management:
49 | endpoint:
50 | health:
51 | show-details: always
52 | health:
53 | defaults:
54 | enabled: false
55 | db:
56 | enabled: true
57 |
58 | # Logger Config
59 | logging:
60 | level:
61 | mybatis.mate: debug
62 | org.apache: debug
63 |
--------------------------------------------------------------------------------
/mybatis-mate-sharding/src/main/resources/db/user-mysql.sql:
--------------------------------------------------------------------------------
1 | DROP TABLE IF EXISTS user;
2 |
3 | CREATE TABLE user
4 | (
5 | id BIGINT(20) NOT NULL COMMENT '主键ID',
6 | username VARCHAR(30) NULL DEFAULT NULL COMMENT '用户名',
7 | password VARCHAR(1000) NULL DEFAULT NULL COMMENT '密码',
8 | sex INT(2) NULL DEFAULT NULL COMMENT '性别',
9 | email VARCHAR(1000) NULL DEFAULT NULL COMMENT '邮箱',
10 | PRIMARY KEY (id)
11 | );
12 |
13 | DELETE FROM user;
14 |
15 | INSERT INTO user (id, username, password, sex, email) VALUES
16 | (1, 'Jone', '123456', 1, 'test1@baomidou.com'),
17 | (2, 'Jack', '123456', 0, 'test2@baomidou.com');
--------------------------------------------------------------------------------
/mybatis-mate-sharding/src/main/resources/db/user-postgres.sql:
--------------------------------------------------------------------------------
1 | DROP TABLE IF EXISTS "public"."user";
2 |
3 | CREATE TABLE "public"."user" (
4 | "id" int8 NOT NULL,
5 | "username" varchar(255) NOT NULL,
6 | "password" varchar(255) NOT NULL,
7 | "sex" int2 NOT NULL,
8 | "email" varchar(255) NOT NULL,
9 | PRIMARY KEY ("id")
10 | );
11 |
12 | COMMENT ON COLUMN "public"."user"."id" IS '主键 ID';
13 | COMMENT ON COLUMN "public"."user"."username" IS '用户名';
14 | COMMENT ON COLUMN "public"."user"."password" IS '密码';
15 | COMMENT ON COLUMN "public"."user"."sex" IS '性别';
16 | COMMENT ON COLUMN "public"."user"."email" IS '电子邮箱';
17 |
18 | DELETE FROM "public"."user";
19 |
20 | INSERT INTO "public"."user" (id, username, password, sex, email) VALUES
21 | (3, 'Tom', '123456', 1, 'test3@baomidou.com');
22 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | // MybatisPlus 企业级模块
2 | rootProject.name = 'mybatis-mate-examples'
3 |
4 | include 'mybatis-mate-audit'
5 | include 'mybatis-mate-datascope'
6 | include 'mybatis-mate-ddl-mysql'
7 | include 'mybatis-mate-ddl-postgres'
8 | include 'mybatis-mate-ddl-oracle'
9 | include 'mybatis-mate-dict'
10 | include 'mybatis-mate-encrypt'
11 | include 'mybatis-mate-encrypt-mysql-aes'
12 | include 'mybatis-mate-encrypt-sm2-sm3-sm4'
13 | include 'mybatis-mate-hot-reload'
14 | include 'mybatis-mate-jsonbind'
15 | include 'mybatis-mate-sensitive-jackson'
16 | include 'mybatis-mate-sensitive-words'
17 | include 'mybatis-mate-sharding'
18 | include 'mybatis-mate-sharding-dynamic'
19 | include 'mybatis-mate-sharding-jta-atomikos'
20 |
--------------------------------------------------------------------------------
/国密SM2.3.4算法使用规范/SM2密码算法使用规范.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/baomidou/mybatis-mate-examples/49536203f7240b13a95e5dbf5a6e82314bad515e/国密SM2.3.4算法使用规范/SM2密码算法使用规范.pdf
--------------------------------------------------------------------------------
/国密SM2.3.4算法使用规范/国密算法SM2-1.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/baomidou/mybatis-mate-examples/49536203f7240b13a95e5dbf5a6e82314bad515e/国密SM2.3.4算法使用规范/国密算法SM2-1.pdf
--------------------------------------------------------------------------------
/国密SM2.3.4算法使用规范/国密算法SM2-2.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/baomidou/mybatis-mate-examples/49536203f7240b13a95e5dbf5a6e82314bad515e/国密SM2.3.4算法使用规范/国密算法SM2-2.pdf
--------------------------------------------------------------------------------
/国密SM2.3.4算法使用规范/国密算法SM3.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/baomidou/mybatis-mate-examples/49536203f7240b13a95e5dbf5a6e82314bad515e/国密SM2.3.4算法使用规范/国密算法SM3.pdf
--------------------------------------------------------------------------------
/国密SM2.3.4算法使用规范/国密算法SMS4.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/baomidou/mybatis-mate-examples/49536203f7240b13a95e5dbf5a6e82314bad515e/国密SM2.3.4算法使用规范/国密算法SMS4.pdf
--------------------------------------------------------------------------------