shardingsphere-jdbc 整合 springboot

shardingsphere官网地址

https://shardingsphere.apache.org/document/5.2.0/cn/user-manual/shardingsphere-jdbc/spring-boot-starter/rules/sharding/

当前我们演示的是水平分表

1、基础环境配置以及依赖管理

1.1 创建数据库表结构

CREATE TABLE `address_0` (
  `id` bigint(20) NOT NULL,
  `name` varchar(255) DEFAULT NULL,
  `addr` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `address_1` (
  `id` bigint(20) NOT NULL,
  `name` varchar(255) DEFAULT NULL,
  `addr` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

1.2 maven依赖

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
    <groupId>mysqlgroupId>
    <artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
    <groupId>com.baomidougroupId>
    <artifactId>mybatis-plus-boot-starterartifactId>
    <version>3.4.1version>
dependency>
<dependency>
    <groupId>org.apache.shardingspheregroupId>
    <artifactId>shardingsphere-jdbc-core-spring-boot-starterartifactId>
    <version>5.2.0version>
dependency>
<dependency>
    <groupId>org.projectlombokgroupId>
    <artifactId>lombokartifactId>
dependency>

1.3 水平分表 application.properties 文件

server.port=8200
spring.application.name=sharding
## 单节点
spring.shardingsphere.mode.type=Standalone
## 日志打印
spring.shardingsphere.props.sql-show=true
# 配置真实数据源,多个英文逗号间隔
spring.shardingsphere.datasource.names=ds1

# 配置第 1 个数据源
## 格式 spring.shardingsphere.datasource.<定义的数据源名称>.
spring.shardingsphere.datasource.ds1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds1.jdbc-url=jdbc:mysql://192.168.1.184:3306/db1?useSSL=false&characterEncoding=UTF8
spring.shardingsphere.datasource.ds1.username=root
spring.shardingsphere.datasource.ds1.password=root

## 格式 spring.shardingsphere.rules.sharding.tables.<定义的表名称>.
spring.shardingsphere.rules.sharding.tables.address.actual-data-nodes=ds1.address_$->{0..1}
spring.shardingsphere.rules.sharding.tables.address.table-strategy.standard.sharding-column=id
spring.shardingsphere.rules.sharding.tables.address.table-strategy.standard.sharding-algorithm-name=address-inline

## 主键标识 以及生产策略
spring.shardingsphere.rules.sharding.tables.address.key-generate-strategy.column=id
spring.shardingsphere.rules.sharding.tables.address.key-generate-strategy.key-generator-name=snowflake

## 主键取模存储具体那张表上
spring.shardingsphere.rules.sharding.sharding-algorithms.address-inline.type=INLINE
spring.shardingsphere.rules.sharding.sharding-algorithms.address-inline.props.algorithm-expression=address_$->{id % 2}

## sharding主键生成策略
spring.shardingsphere.rules.sharding.key-generators.snowflake.type=SNOWFLAKE

## 开启mybatis-plus的扫描位置
mybatis-plus.mapper-locations=classpath*:com/cloud/**/xml/*Mapper.xml

2、创建对应实体和相关控制层

2.1 创建对应类

@Data
public class Address  {
    @TableId(value = "id")
    private Long id;
    @TableField("name")
    private String name;
    @TableField("addr")
    private String addr;
}
@RestController
public class AddressController {

    @Autowired
    private AddressService addressService;

    private static String[] addrArrays = new String[]{"青岛","北京","上海","遂州","杭州","烟台","内蒙古","海南","台湾","香港","石家庄"};
    private static String[] fisterName = new String[]{"张","北","上","遂","杭","烟","古","海","台","港","石"};

    @GetMapping("/saveAddr")
    public void save(){
        int nextName = new Random().nextInt(10);
        List<Address> addressList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Address address = new Address();
            int nextInt = new Random().nextInt(10);
            address.setName(fisterName[nextName]+"-"+i);
            address.setAddr(addrArrays[nextInt]);
            addressList.add(address);
        }
        addressService.saveBatch(addressList);
    }
    @GetMapping("/getAddr")
    public Object get(){
        return addressService.list(new QueryWrapper<Address>().last(" LIMIT 10 "));
    }
}

2.2 调用结果与表数据查看

2.2.1 插入数据

shardingsphere-jdbc 整合 springboot_第1张图片
shardingsphere-jdbc 整合 springboot_第2张图片
shardingsphere-jdbc 整合 springboot_第3张图片

2.2.2 查询数据

shardingsphere-jdbc 整合 springboot_第4张图片

在这里插入图片描述

2.2.3 修改数据

shardingsphere-jdbc 整合 springboot_第5张图片

2.2.4 删除数据

在这里插入图片描述

3 分库分表 application.yml

server:
  port: 8200
spring:
  application:
    name: sharding
  shardingsphere:
    mode:
      type: Standalone
    props:
      sql-show: true
    datasource:
      names: db-0,db-1
      db-0:
        jdbc-url: jdbc:mysql://192.168.1.184:3306/ddd1?useSSL=false&characterEncoding=UTF8
        username: root
        password: root
        driver-class-name: com.mysql.cj.jdbc.Driver
        type: com.zaxxer.hikari.HikariDataSource
      db-1:
        jdbc-url: jdbc:mysql://192.168.1.184:3306/bbb2?useSSL=false&characterEncoding=UTF8
        username: root
        password: root
        driver-class-name: com.mysql.cj.jdbc.Driver
        type: com.zaxxer.hikari.HikariDataSource
    rules:
      sharding:
        tables:
          address:
             ##分库策略
            database-strategy:
              standard:
                sharding-column: id
                sharding-algorithm-name: id-inline
            actual-data-nodes: db-$->{0..1}.address_$->{0..1}
            ##分表策略
            table-strategy:
              standard:
                sharding-column: id
                sharding-algorithm-name: address-inline
            key-generate-strategy:
              column: id
              key-generator-name: snowflake
        sharding-algorithms:
          id-inline:
            type: INLINE
            props:
              algorithm-expression: db-$->{id % 2}
          address-inline:
            type: INLINE
            props:
              algorithm-expression: address_$->{id % 2}
        key-generators:
          snowflake:
            type: SNOWFLAKE

mybatis-plus:
  #  **  代表多级  *代表一级
  mapper-locations: classpath*:com/cloud/**/xml/*Mapper.xml

你可能感兴趣的:(ORM,SpringBoot,spring,boot,java,mybatis,sharding)