Sub-library and sub-table in springboot project

  1. The process of sub-database and sub-table:

    • Database horizontal splitting: split the original database into multiple databases, each database is responsible for a part of the data.
    • Vertical split of database: Split the original database into multiple databases according to business functions, and each database is responsible for part of the functions.
    • Table horizontal splitting: split the original table into multiple tables, each table is responsible for a part of the data.
    • Table vertical splitting: Split the original table into multiple tables according to fields, and each table is responsible for a part of the fields.
  2. Selection of sub-database and sub-table:

    • Choose based on business needs: Choose a suitable database and table sharding strategy based on business read-write ratio, data volume and other factors.
    • Selection based on data characteristics: If there is a large imbalance in data characteristics, you can divide databases and tables according to data characteristics.
    • Choose based on scalability: Choose a sub-database and table strategy that can be horizontally expanded to meet future business development needs.
    • Selection based on technical costs: Weigh technical costs and benefits, and choose a sharding strategy that is suitable for the project.

It should be noted that sub-database and sub-table will increase the complexity of the system, and the development and maintenance costs will increase. Therefore, before sharding databases and tables, it is necessary to fully evaluate project requirements, data characteristics, and technical costs, and select appropriate database and table sharding strategies.

To implement sub-database and sub-table in Spring Boot, you can use the MyBatis framework and ShardingSphere for configuration.

First, you need to add the corresponding dependencies in pom.xml. In this example, we are using mysql as the database and the dependencies can be configured as follows:

<!-- MyBatis -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.2.0</version>
</dependency>

<!-- ShardingSphere -->
<dependency>
    <groupId>org.apache.shardingsphere</groupId>
    <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
    <version>4.1.1</version>
</dependency>

Next, we need to configure the database connection information and ShardingSphere rules in the application.properties or application.yml file.

spring:
  datasource:
    # 主数据库配置
    shardingsphere:
      datasource:
        ds0:
          url: jdbc:mysql://localhost:3306/db0?useSSL=false&serverTimezone=UTC
          username: root
          password: root
          driver-class-name: com.mysql.cj.jdbc.Driver
        ds1:
          url: jdbc:mysql://localhost:3306/db1?useSSL=false&serverTimezone=UTC
          username: root
          password: root
          driver-class-name: com.mysql.cj.jdbc.Driver

  sharding:
    # 分库配置
    sharding-rule:
      binding-tables:
        - user
      tables:
        user:
          actual-data-nodes: ds$->{0..1}.user$->{0..1}
          table-strategy:
            inline:
              sharding-column: user_id
              algorithm-expression: user$->{user_id % 2}

In this example, we have configured two databases ds0 and ds1, and each database has two tables user0 and user1. Based on the results of user_id%2, the data is dispersed into different databases and tables.

Next, we need to create the User entity class and UserMapper interface.

public class User {
    private Long id;
    private String name;
    // getters and setters
}

@Mapper
public interface UserMapper {
    @Insert("INSERT INTO user (name) VALUES (#{name})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(User user);

    @Select("SELECT * FROM user WHERE id = #{id}")
    User findById(Long id);
}

Configure the insertion statement to the method of the Mapper interface through the @Insert annotation, and configure the query statement through the @Select annotation.

Finally, we can use UserMapper in the Service layer to perform database operations.

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    public void addUser(User user) {
        userMapper.insert(user);
    }

    public User findUserById(Long id) {
        return userMapper.findById(id);
    }
}

The above is the basic configuration of using Spring Boot, MyBatis and ShardingSphere to shard databases and tables.

Guess you like

Origin blog.csdn.net/Flying_Fish_roe/article/details/135017801