SpringBoot+Sharding-JDBC operation sub-database and table

What is Sharding-JDBC? What is sub-database sub-table? Why sub-database and sub-table?

You can view this blog:

Apache-ShardingSphere (distributed database middleware, operation tool for sub-database and table)

Sharding-JDBC operation level sub-table

1. Build the environment

  1. Basic environment: SpringBoot2.2.1 + MybatisPlus + Sharding-JDBC + Druid connection pool
  2. Create SpringBoot project
     

  3. Modify the SpringBoot project version to 2.2.1
     

  4. Introduce related dependencies
     
    <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <!-- Druid连接池 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.1.20</version>
            </dependency>
            <!-- Mysql驱动依赖 -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
            <!-- MybatisPlus -->
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>3.0.5</version>
            </dependency>
            <!-- Sharding-JDBC -->
            <dependency>
                <groupId>org.apache.shardingsphere</groupId>
                <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
                <version>4.0.0-RC1</version>
            </dependency>
    
            <!-- lombok -->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
            </dependency>
    
        </dependencies>

     

Second, create databases and database tables in the way of horizontal sub-tables

  1. Create a database goods_db;
  2. Create tables goods_1, goods_2 in goods_db;
  3. The agreed rule is to add the data to goods_1 if the added product id is an even number, and add the data to goods_2 if it is an even number;
  4. Structure display
     

Three, write code

Create Goods entity class

@Data
public class Goods {
    private Long gid;
    private String gname;
    private Long userId;
    private String gstatus;
}

 The content generated by the @Data annotation is as follows:

Create GoodsMapper

@Repository
public interface GoodsMapper extends BaseMapper<Goods> {
}

Start class configuration scan Mapper package

@SpringBootApplication
@MapperScan("com.xxx.mapper")
public class ShardingJdbcApplication {

    public static void main(String[] args) {
        SpringApplication.run(ShardingJdbcApplication.class, args);
    }

}

Four, application.properties configuration Sharding-JDBC

 
  1. # 配置Sharding-JDBC的分片策略

  2. # 配置数据源,给数据源起名g1,g2...此处可配置多数据源

  3. spring.shardingsphere.datasource.names=g1

  4.  
  5. # 配置数据源具体内容————————包含 连接池, 驱动, 地址, 用户名, 密码

  6. # 由于上面配置数据源只有g1因此下面只配置g1.type,g1.driver-class-name,g1.url,g1.username,g1.password

  7. spring.shardingsphere.datasource.g1.type=com.alibaba.druid.pool.DruidDataSource

  8. spring.shardingsphere.datasource.g1.driver-class-name=com.mysql.cj.jdbc.Driver

  9. spring.shardingsphere.datasource.g1.url=jdbc:mysql://localhost:3306/goods_db?characterEncoding=utf-8&useUnicode=true&useSSL=false&serverTimezone=UTC

  10. spring.shardingsphere.datasource.g1.username=root

  11. spring.shardingsphere.datasource.g1.password=123456

  12.  
  13. # 配置表的分布,表的策略

  14. spring.shardingsphere.sharding.tables.goods.actual-data-nodes=g1.goods_$->{1..2}

  15.  
  16. # 指定goods表 主键gid 生成策略为 SNOWFLAKE

  17. spring.shardingsphere.sharding.tables.goods.key-generator.column=gid

  18. spring.shardingsphere.sharding.tables.goods.key-generator.type=SNOWFLAKE

  19.  
  20. # 指定分片策略 约定gid值是偶数添加到goods_1表,如果gid是奇数添加到goods_2表

  21. spring.shardingsphere.sharding.tables.goods.table-strategy.inline.sharding-column=gid

  22. spring.shardingsphere.sharding.tables.goods.table-strategy.inline.algorithm-expression=goods_$->{gid % 2 + 1}

  23.  
  24. # 打开sql输出日志

  25. spring.shardingsphere.props.sql.show=true

PS: Such configuration files can be found on the ShardingShpere official website, so don't worry about not remembering them.

Five, write test code

 ShardingjdbcdemoApplicationTests.Java code:

 
  1. package com.ws.shardingjdbcdemo;

  2.  
  3. import com.ws.shardingjdbcdemo.mapper.GoodsMapper;

  4. import com.ws.shardingjdbcdemo.pojo.Goods;

  5. import org.junit.jupiter.api.Test;

  6. import org.springframework.beans.factory.annotation.Autowired;

  7. import org.springframework.boot.test.context.SpringBootTest;

  8.  
  9. @SpringBootTest

  10. class ShardingjdbcdemoApplicationTests {

  11.  
  12. @Autowired

  13. GoodsMapper goodsMapper;

  14.  
  15. @Test

  16. void addGoods() {

  17. Goods good = new Goods();

  18. good.setGname("小米手机");

  19. good.setUserId(100L);

  20. good.setGstatus("已发布");

  21. goodsMapper.insert(good);

  22. }

  23.  
  24. }

I started the test method, and I got an error:

 It probably means that two beans with the same name are not allowed.

 solution:

    spring.main.allow-bean-definition-overriding=true

Configure this in application.properties

 
  1. # 配置Sharding-JDBC的分片策略

  2. # 配置数据源,给数据源起名g1,g2...此处可配置多数据源

  3. spring.shardingsphere.datasource.names=g1

  4.  
  5. # 配置允许后面的Bean覆盖前面名称重复的Bean

  6. spring.main.allow-bean-definition-overriding=true

  7.  
  8. # 配置数据源具体内容————————包含 连接池, 驱动, 地址, 用户名, 密码

  9. # 由于上面配置数据源只有g1因此下面只配置g1.type,g1.driver-class-name,g1.url,g1.username,g1.password

  10. spring.shardingsphere.datasource.g1.type=com.alibaba.druid.pool.DruidDataSource

  11. spring.shardingsphere.datasource.g1.driver-class-name=com.mysql.cj.jdbc.Driver

  12. spring.shardingsphere.datasource.g1.url=jdbc:mysql://localhost:3306/goods_db?characterEncoding=utf-8&useUnicode=true&useSSL=false&serverTimezone=UTC

  13. spring.shardingsphere.datasource.g1.username=root

  14. spring.shardingsphere.datasource.g1.password=123456

  15.  
  16. # 配置表的分布,表的策略

  17. spring.shardingsphere.sharding.tables.goods.actual-data-nodes=g1.goods_$->{1..2}

  18.  
  19. # 指定goods表 主键gid 生成策略为 SNOWFLAKE

  20. spring.shardingsphere.sharding.tables.goods.key-generator.column=gid

  21. spring.shardingsphere.sharding.tables.goods.key-generator.type=SNOWFLAKE

  22.  
  23. # 指定分片策略 约定gid值是偶数添加到goods_1表,如果gid是奇数添加到goods_2表

  24. spring.shardingsphere.sharding.tables.goods.table-strategy.inline.sharding-column=gid

  25. spring.shardingsphere.sharding.tables.goods.table-strategy.inline.algorithm-expression=goods_$->{gid % 2 + 1}

  26.  
  27. # 打开sql输出日志

  28. spring.shardingsphere.props.sql.show=true

Start the test method again to successfully insert data and print out the SQL statement on the console:

 goods_1 table:

 goods_2 table:

PS: You can see that the gid is odd, data is inserted in goods_2, and there is nothing in goods_1. 

 Console:

 Next, test whether the table is inserted according to odd and even numbers.

Modify the code of the addGoods() method:

 
  1. @Test

  2. void addGoods() {

  3. for (int i = 0; i < 10; i++){

  4. Goods good = new Goods();

  5. good.setGname("小米手机" + i);

  6. good.setUserId(100L);

  7. good.setGstatus("已发布");

  8. goodsMapper.insert(good);

  9. }

  10. }

Results of the:

goods_1 table:

goods_2 table:

 It can be seen that the rules of inserting 2 tables for odd numbers and 1 table for even numbers are completely followed.

Test query:

1. Write the getGood() method

 
  1. @Test

  2. void getGood(){

  3. QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();

  4. queryWrapper.eq("gid",479688915029065729L);

  5. Goods good = goodsMapper.selectOne(queryWrapper);

  6. System.out.println(good.toString());

  7. }

2. Execute getGood(), and successfully print out the data information of gid = 479688915029065729

The above is the operation of Sharding-JDBC on the horizontal sub-table. If you have any questions, you can leave a comment, aiming to learn together and make progress together!

The above describes how to use Sharding-JDBC to operate the horizontal sub-table, and then play with the horizontal sub-database sub-table operation.

Sharding-JDBC operation level sub-database + level sub-table

1. Create two databases goods_db_1 and goods_db_2, each database contains two tables goods_1 and goods_2, goods_1 and goods_2 have the same structure as the above sub-table.

2. Configure application.properties

 
  1. # 配置Sharding-JDBC的分片策略

  2. # 配置数据源,给数据源起名g1,g2...此处可配置多数据源

  3. spring.shardingsphere.datasource.names=g1,g2

  4.  
  5. # 配置允许一个实体类映射多张表

  6. spring.main.allow-bean-definition-overriding=true

  7.  
  8. # 配置数据源具体内容————————包含 连接池, 驱动, 地址, 用户名, 密码

  9. # g1配置g1.type,g1.driver-class-name,g1.url,g1.username,g1.password

  10. spring.shardingsphere.datasource.g1.type=com.alibaba.druid.pool.DruidDataSource

  11. spring.shardingsphere.datasource.g1.driver-class-name=com.mysql.cj.jdbc.Driver

  12. spring.shardingsphere.datasource.g1.url=jdbc:mysql://localhost:3306/goods_db_1?characterEncoding=utf-8&useUnicode=true&useSSL=false&serverTimezone=UTC

  13. spring.shardingsphere.datasource.g1.username=root

  14. spring.shardingsphere.datasource.g1.password=123456

  15.  
  16. # 配置数据源具体内容————————包含 连接池, 驱动, 地址, 用户名, 密码

  17. # g2配置g2.type,g2.driver-class-name,g2.url,g2.username,g2.password

  18. spring.shardingsphere.datasource.g2.type=com.alibaba.druid.pool.DruidDataSource

  19. spring.shardingsphere.datasource.g2.driver-class-name=com.mysql.cj.jdbc.Driver

  20. spring.shardingsphere.datasource.g2.url=jdbc:mysql://localhost:3306/goods_db_2?characterEncoding=utf-8&useUnicode=true&useSSL=false&serverTimezone=UTC

  21. spring.shardingsphere.datasource.g2.username=root

  22. spring.shardingsphere.datasource.g2.password=123456

  23.  
  24. # 配置数据库的分布,表的分布

  25. # m1:goods_1 goods_2; m2:goods_1,goods_2;

  26. spring.shardingsphere.sharding.tables.goods.actual-data-nodes=g$->{1..2}.goods_$->{1..2}

  27.  
  28. # 指定goods表 主键gid 生成策略为 SNOWFLAKE

  29. spring.shardingsphere.sharding.tables.goods.key-generator.column=gid

  30. spring.shardingsphere.sharding.tables.goods.key-generator.type=SNOWFLAKE

  31.  
  32. # 指定数据库分片策略 约定user_id值是偶数添加到goods_db_1中,奇数添加到goods_db_2中

  33. spring.shardingsphere.sharding.tables.goods.database-strategy.inline.sharding-column=user_id

  34. spring.shardingsphere.sharding.tables.goods.database-strategy.inline.algorithm-expression=g$->{user_id % 2 + 1}

  35.  
  36. # 指定表分片策略 约定gid值是偶数添加到goods_1表,如果gid是奇数添加到goods_2表

  37. spring.shardingsphere.sharding.tables.goods.table-strategy.inline.sharding-column=gid

  38. spring.shardingsphere.sharding.tables.goods.table-strategy.inline.algorithm-expression=goods_$->{gid % 2 + 1}

  39.  
  40. # 打开sql输出日志

  41. spring.shardingsphere.props.sql.show=true

 3. Write the test method addGoods02()

 
  1. @Test

  2. void addGoods02(){

  3. Goods good = new Goods();

  4. good.setGname("华为手机");

  5. good.setUserId(100L);

  6. good.setGstatus("已发布");

  7. goodsMapper.insert(good);

  8. }

 4. Execute addGoods02() method

     Since our user_id is set to 100L, which is an even number, according to our original intention, the even number is stored in goods_db_1.

 5. Implementation results

     The console sql statement is consistent with what we expect.

      The database results are also consistent with our expectations. 

      You can try it yourself for the remaining batch insert, query, modify, and delete operations.

Sharding-JDBC operation vertical sub-database + horizontal sub-database + horizontal sub-table

Background description:

      In the   sub-database sub-table & sub-database sub-table tool  article, I learned what is vertical sub-database sub-table, horizontal sub-database sub-table, and why sub-library sub-table; in our actual project, there will be multiple databases after vertical sub-database , Each database has its own tables and public tables (operating public tables will be described later in this section).

      In the above chapter, we have used Sharding-JDBC to operate level database and table. On the basis of the above, we also have a database user_db, which contains a t_user table to store user information. There are multiple in one of our projects. Database, each database has different tables. 

The database structure and t_user table structure are as follows:

1. Demand analysis

      There are so many databases and tables above in our project. When we operate the t_user table, we can quickly and accurately operate to the userdb database> t_user table.

Second, realize the demand

     1. Configure the application.properties file

     On the basis of the above horizontal sub-database and sub-tables, write the configuration about user_db and t_user tables.

 
  1. # 配置Sharding-JDBC的分片策略

  2. # 配置数据源,给数据源起名g1,g2...此处可配置多数据源

  3. spring.shardingsphere.datasource.names=g1,g2,u0

  4.  
  5. # 配置允许一个实体类映射多张表

  6. spring.main.allow-bean-definition-overriding=true

  7.  
  8. # 配置g1数据源具体内容————————包含 连接池, 驱动, 地址, 用户名, 密码

  9. # g1配置g1.type,g1.driver-class-name,g1.url,g1.username,g1.password

  10. spring.shardingsphere.datasource.g1.type=com.alibaba.druid.pool.DruidDataSource

  11. spring.shardingsphere.datasource.g1.driver-class-name=com.mysql.cj.jdbc.Driver

  12. spring.shardingsphere.datasource.g1.url=jdbc:mysql://localhost:3306/goods_db_1?characterEncoding=utf-8&useUnicode=true&useSSL=false&serverTimezone=UTC

  13. spring.shardingsphere.datasource.g1.username=root

  14. spring.shardingsphere.datasource.g1.password=123456

  15.  
  16. # 配置g2数据源具体内容————————包含 连接池, 驱动, 地址, 用户名, 密码

  17. # g2配置g2.type,g2.driver-class-name,g2.url,g2.username,g2.password

  18. spring.shardingsphere.datasource.g2.type=com.alibaba.druid.pool.DruidDataSource

  19. spring.shardingsphere.datasource.g2.driver-class-name=com.mysql.cj.jdbc.Driver

  20. spring.shardingsphere.datasource.g2.url=jdbc:mysql://localhost:3306/goods_db_2?characterEncoding=utf-8&useUnicode=true&useSSL=false&serverTimezone=UTC

  21. spring.shardingsphere.datasource.g2.username=root

  22. spring.shardingsphere.datasource.g2.password=123456

  23.  
  24. # 配置u0数据源具体内容————————包含 连接池, 驱动, 地址, 用户名, 密码

  25. # u0配置u0.type,u0.driver-class-name,u0.url,u0.username,u0.password

  26. spring.shardingsphere.datasource.u0.type=com.alibaba.druid.pool.DruidDataSource

  27. spring.shardingsphere.datasource.u0.driver-class-name=com.mysql.cj.jdbc.Driver

  28. spring.shardingsphere.datasource.u0.url=jdbc:mysql://localhost:3306/user_db?characterEncoding=utf-8&useUnicode=true&useSSL=false&serverTimezone=UTC

  29. spring.shardingsphere.datasource.u0.username=root

  30. spring.shardingsphere.datasource.u0.password=123456

  31.  
  32. #配置垂直分库t_user的策略

  33. spring.shardingsphere.sharding.tables.t_user.actual-data-nodes=u0.t_user

  34. spring.shardingsphere.sharding.tables.t_user.key-generator.column=user_id

  35. spring.shardingsphere.sharding.tables.t_user.key-generator.type=SNOWFLAKE

  36. spring.shardingsphere.sharding.tables.t_user.table-strategy.inline.sharding-column=user_id

  37. #由于只有一张表,因此在此直接写表明,不需要像水平分多个表那样写策略

  38. spring.shardingsphere.sharding.tables.t_user.table-strategy.inline.algorithm-expression=t_user

  39.  
  40. # 配置数据库的分布,表的分布

  41. # g1:goods_1 goods_2; g2:goods_1,goods_2;

  42. spring.shardingsphere.sharding.tables.goods.actual-data-nodes=g$->{1..2}.goods_$->{1..2}

  43.  
  44. # 指定goods表 主键gid 生成策略为 SNOWFLAKE

  45. spring.shardingsphere.sharding.tables.goods.key-generator.column=gid

  46. spring.shardingsphere.sharding.tables.goods.key-generator.type=SNOWFLAKE

  47.  
  48. # 指定数据库分片策略 约定user_id值是偶数添加到goods_db_1中,奇数添加到goods_db_2中

  49. spring.shardingsphere.sharding.tables.goods.database-strategy.inline.sharding-column=user_id

  50. spring.shardingsphere.sharding.tables.goods.database-strategy.inline.algorithm-expression=g$->{user_id % 2 + 1}

  51.  
  52. # 指定表分片策略 约定gid值是偶数添加到goods_1表,如果gid是奇数添加到goods_2表

  53. spring.shardingsphere.sharding.tables.goods.table-strategy.inline.sharding-column=gid

  54. spring.shardingsphere.sharding.tables.goods.table-strategy.inline.algorithm-expression=goods_$->{gid % 2 + 1}

  55.  
  56. # 打开sql输出日志

  57. spring.shardingsphere.props.sql.show=true

     2. Write the code

        User.java

 
  1. package com.ws.shardingjdbcdemo.pojo;

  2.  
  3. import com.baomidou.mybatisplus.annotation.TableName;

  4. import lombok.Data;

  5.  
  6. @Data

  7. @TableName("t_user")

  8. public class User {

  9. private Long userId;

  10. private String username;

  11. private String ustatus;

  12. }

        UserMapper.java

 
  1. package com.ws.shardingjdbcdemo.mapper;

  2.  
  3. import com.baomidou.mybatisplus.core.mapper.BaseMapper;

  4. import com.ws.shardingjdbcdemo.pojo.User;

  5. import org.springframework.stereotype.Repository;

  6.  
  7. @Repository

  8. public interface UserMapper extends BaseMapper<User> {

  9.  
  10. }

        Tests.java

 
  1. @Test

  2. void addUser(){

  3. User user = new User();

  4. user.setUsername("琳妹妹");

  5. user.setUstatus("0");

  6. userMapper.insert(user);

  7. }

  8.  
  9. @Test

  10. void getUser(){

  11. QueryWrapper<User> queryWrapper = new QueryWrapper<>();

  12. //此处请填写自己程序生成的ID

  13. queryWrapper.eq("user_id",100L);

  14. User good = userMapper.selectOne(queryWrapper);

  15. System.out.println(good.toString());

  16. }

3. Test and results

addUser() result:

  getUser result:

 Sharding-JDBC operation common table

      Background description:

        In the project, there are generally some tables whose contents are fixed, or tables that are rarely modified, but they are often queried in association with these tables, such as some status information. Generally in our project this kind of table will be stored in our various databases, so it is called a public table.

        In the above chapter, we used Sharding-JDBC to operate the vertical sub-database + horizontal sub-database + horizontal sub-table, assuming that there is a common table t_dict in each of our databases.

      Database & table structure:

      demand:

            When operating a common table, such as adding and deleting operations, this table in all databases will be modified.

      achieve:

          1. Based on the previous chapter, write and configure application.properties (configuration common table part)

 
  1. # 配置Sharding-JDBC的分片策略

  2. # 配置数据源,给数据源起名g1,g2...此处可配置多数据源

  3. spring.shardingsphere.datasource.names=g1,g2,u0

  4.  
  5. # 配置允许一个实体类映射多张表

  6. spring.main.allow-bean-definition-overriding=true

  7.  
  8. # 配置g1数据源具体内容————————包含 连接池, 驱动, 地址, 用户名, 密码

  9. # g1配置g1.type,g1.driver-class-name,g1.url,g1.username,g1.password

  10. spring.shardingsphere.datasource.g1.type=com.alibaba.druid.pool.DruidDataSource

  11. spring.shardingsphere.datasource.g1.driver-class-name=com.mysql.cj.jdbc.Driver

  12. spring.shardingsphere.datasource.g1.url=jdbc:mysql://localhost:3306/goods_db_1?characterEncoding=utf-8&useUnicode=true&useSSL=false&serverTimezone=UTC

  13. spring.shardingsphere.datasource.g1.username=root

  14. spring.shardingsphere.datasource.g1.password=123456

  15.  
  16. # 配置g2数据源具体内容————————包含 连接池, 驱动, 地址, 用户名, 密码

  17. # g2配置g2.type,g2.driver-class-name,g2.url,g2.username,g2.password

  18. spring.shardingsphere.datasource.g2.type=com.alibaba.druid.pool.DruidDataSource

  19. spring.shardingsphere.datasource.g2.driver-class-name=com.mysql.cj.jdbc.Driver

  20. spring.shardingsphere.datasource.g2.url=jdbc:mysql://localhost:3306/goods_db_2?characterEncoding=utf-8&useUnicode=true&useSSL=false&serverTimezone=UTC

  21. spring.shardingsphere.datasource.g2.username=root

  22. spring.shardingsphere.datasource.g2.password=123456

  23.  
  24. # 配置u0数据源具体内容————————包含 连接池, 驱动, 地址, 用户名, 密码

  25. # u0配置u0.type,u0.driver-class-name,u0.url,u0.username,u0.password

  26. spring.shardingsphere.datasource.u0.type=com.alibaba.druid.pool.DruidDataSource

  27. spring.shardingsphere.datasource.u0.driver-class-name=com.mysql.cj.jdbc.Driver

  28. spring.shardingsphere.datasource.u0.url=jdbc:mysql://localhost:3306/user_db?characterEncoding=utf-8&useUnicode=true&useSSL=false&serverTimezone=UTC

  29. spring.shardingsphere.datasource.u0.username=root

  30. spring.shardingsphere.datasource.u0.password=123456

  31.  
  32. # 配置公共表

  33. spring.shardingsphere.sharding.broadcast-tables=t_dict

  34. # 配置公共表ID及生成策略

  35. spring.shardingsphere.sharding.tables.t_dict.key-generator.column=dict_id

  36. spring.shardingsphere.sharding.tables.t_dict.key-generator.type=SNOWFLAKE

  37.  
  38. #配置垂直分库t_user的策略

  39. spring.shardingsphere.sharding.tables.t_user.actual-data-nodes=u0.t_user

  40. spring.shardingsphere.sharding.tables.t_user.key-generator.column=user_id

  41. spring.shardingsphere.sharding.tables.t_user.key-generator.type=SNOWFLAKE

  42. spring.shardingsphere.sharding.tables.t_user.table-strategy.inline.sharding-column=user_id

  43. #由于只有一张表,因此在此直接写表明,不需要像水平分多个表那样写策略

  44. spring.shardingsphere.sharding.tables.t_user.table-strategy.inline.algorithm-expression=t_user

  45.  
  46. # 配置数据库的分布,表的分布

  47. # g1:goods_1 goods_2; g2:goods_1,goods_2;

  48. spring.shardingsphere.sharding.tables.goods.actual-data-nodes=g$->{1..2}.goods_$->{1..2}

  49.  
  50. # 指定goods表 主键gid 生成策略为 SNOWFLAKE

  51. spring.shardingsphere.sharding.tables.goods.key-generator.column=gid

  52. spring.shardingsphere.sharding.tables.goods.key-generator.type=SNOWFLAKE

  53.  
  54. # 指定数据库分片策略 约定user_id值是偶数添加到goods_db_1中,奇数添加到goods_db_2中

  55. spring.shardingsphere.sharding.tables.goods.database-strategy.inline.sharding-column=user_id

  56. spring.shardingsphere.sharding.tables.goods.database-strategy.inline.algorithm-expression=g$->{user_id % 2 + 1}

  57.  
  58. # 指定表分片策略 约定gid值是偶数添加到goods_1表,如果gid是奇数添加到goods_2表

  59. spring.shardingsphere.sharding.tables.goods.table-strategy.inline.sharding-column=gid

  60. spring.shardingsphere.sharding.tables.goods.table-strategy.inline.algorithm-expression=goods_$->{gid % 2 + 1}

  61.  
  62. # 打开sql输出日志

  63. spring.shardingsphere.props.sql.show=true

          2. Write code

                MyDict.java

 
  1. package com.ws.shardingjdbcdemo.pojo;

  2.  
  3. import com.baomidou.mybatisplus.annotation.TableName;

  4. import lombok.Data;

  5.  
  6. @Data

  7. @TableName("t_dict")

  8. public class MyDict {

  9. private Long dictId;

  10. private String dictName;

  11. private String dictCode;

  12. }

                MyDictMapper.java

 
  1. package com.ws.shardingjdbcdemo.mapper;

  2.  
  3. import com.baomidou.mybatisplus.core.mapper.BaseMapper;

  4. import com.ws.shardingjdbcdemo.pojo.MyDict;

  5. import org.springframework.stereotype.Repository;

  6.  
  7. @Repository

  8. public interface MyDictMapper extends BaseMapper<MyDict> {

  9.  
  10. }

                Tests.java

 
  1. /**

  2. * 下面是公共表测试方法

  3. */

  4. @Test

  5. void addDict(){

  6. MyDict myDict = new MyDict();

  7. myDict.setDictName("已启用");

  8. myDict.setDictCode("1");

  9. myDictMapper.insert(myDict);

  10. }

  11.  
  12. @Test

  13. void deleteDict(){

  14. QueryWrapper<MyDict> wrapper = new QueryWrapper<>();

  15. wrapper.eq("dict_id","");

  16. myDictMapper.delete(wrapper);

  17. }

          3. Test 

          addDict() method:

          deleteDict() method: 

      The above is the operation of Sharding-JDBC of Apache-ShardingSphere on sub-database sub-tables and public tables. If you have any questions, you can leave a comment below, aiming to learn together and make progress together!

Guess you like

Origin blog.csdn.net/feikillyou/article/details/112545986