sping boot mybatis Sharding-JDBC实现单库分表

  1. mybatis官网 shardingSphere官网
  2. 对于一些数据量较大的数据我们就需要分表,而java的框架不像python的框架资源那么少,已经有封装好的数据库中间件,比如Sharding-jdbc,sharding的分片策略有五种,我们这里使用的是行表达式分片策略如: t_user_$->{u_id % 8} 表示t_user表根据u_id模8,而分成8张表,表名称为t_user_0到t_user_7,这里为了方便,就分了两个表。
  3. 表目录结构:
    sping boot mybatis Sharding-JDBC实现单库分表_第1张图片
    在navicat建立的表:
    在这里插入图片描述

dao层:

package cn.bean;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class BootBean {

    private Long id;

    private Integer sex;
}

Controller层:

package cn.coll;


import cn.bean.BootBean;
import cn.server.BootServer;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
public class BootController {

    private final BootServer bootServer;

    public BootController(BootServer bootServer){
        this.bootServer = bootServer;
    }


    @PostMapping("/boot/info")
    public String bootAdd(@RequestBody BootBean bootBean){

        int res = bootServer.BootInsert(bootBean);

        return "成功";
    }

    @PutMapping("/boot/info")
    public String bootUpdate(@RequestBody BootBean bootBean){

        int res = bootServer.BootUpdate(bootBean);

        return "修改成功";
    }

    @GetMapping("/boot/info")
    public List<BootBean> bootSelect(BootBean bootBean){
        System.out.println(bootBean.getSex());
        System.out.println(bootBean.getId());

        List<BootBean> bootBeans = bootServer.BootSelect(bootBean);

        return bootBeans;
    }
}

mapper层:

package cn.mapper;

import cn.bean.BootBean;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

public interface BootMapper extends Mapper<BootBean> {

    // 插入
    int BootInsert(BootBean u);

    // 修改
    int BootUpdate(BootBean u);

    // 查询所有
    List<BootBean> BootSelect(BootBean u);
}

server层:

package cn.server.Impl;

import cn.bean.BootBean;
import cn.mapper.BootMapper;
import cn.server.BootServer;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BootServerImpl implements BootServer {

    private final BootMapper bootMapper;

    public BootServerImpl(BootMapper bootMapper){
        this.bootMapper = bootMapper;
    }

    @Override
    public int BootInsert(BootBean u){
        return bootMapper.BootInsert(u);
    }

    @Override
    public int BootUpdate(BootBean u){
        return bootMapper.BootUpdate(u);
    }

    @Override
    public List<BootBean> BootSelect(BootBean u){
        return bootMapper.BootSelect(u);
    }
}

package cn.server;

import cn.bean.BootBean;

import java.util.List;

public interface BootServer{

    // 新增
    int BootInsert(BootBean u);

    // 修改
    int BootUpdate(BootBean u);

    // 查询所有
    List<BootBean> BootSelect(BootBean u);
}

mapper:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.mapper.BootMapper">

    <insert id="BootInsert" parameterType="cn.bean.BootBean" >
        insert into tb_boot(id, sex) values (#{id},#{sex});
    </insert>

    <update id="BootUpdate" parameterType="cn.bean.BootBean" >
        update tb_boot set sex = #{sex} where id = #{id}
    </update>

    <select id="BootSelect" resultType="cn.bean.BootBean">
        select id, sex from tb_boot
        <where>
            <if test="id != null">
                and id = #{id}
            </if>
            <if test="sex != null">
                and sex = #{sex}
            </if>
        </where>
        order by id asc limit 0,3;
    </select>

</mapper>

application.yml:

# 框架设置
spring:
  # 读取配置文件
  profiles:
    active: dev  #开发环境
    # active: pro  #正式环境


# mybatis
mybatis:
  mapper-locations: classpath:mapper/*Mapper.xml
  configuration:
    map-underscore-to-camel-case: true

sharding:
  jdbc:
    datasource:
      names: ds0
      # 数据源ds0
      ds0:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://xx.xxx.xx.xx:3306/imageserver?serverTimezone=GMT%2B8&useSSL=false
        username: canal
        password: canal
    config:
      sharding:
        props:
          sql.show: true
        tables:
          tb_boot: # tb_boot表
            key-generator-column-name: id  #主键
            actual-data-nodes: ds0.tb_boot${0..1}    #数据节点,均匀分布
            table-strategy: #分表策略
              inline: #行表达式
                sharding-column: id
                algorithm-expression: tb_boot${id % 2}  #按模运算分配


# 日志配置
logging:
  config: classpath:logging/logback.xml
  level:
    cn.mapper: debug

pom文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.10.RELEASE</version>
        <relativePath/>
    </parent>

    <groupId>org.example</groupId>
    <artifactId>sharding</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <sharding.jdbc.version>3.0.0</sharding.jdbc.version>
        <mybatis.version>1.3.0</mybatis.version>
        <druid.version>1.1.10</druid.version>
        <mysql.version>5.1.38</mysql.version>
        <lombok.version>1.18.16</lombok.version>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis.version}</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>${druid.version}</version>
        </dependency>

        <dependency>
            <groupId>io.shardingsphere</groupId>
            <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
            <version>${sharding.jdbc.version}</version>
        </dependency>


        <!-- Lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
        </dependency>

        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>1.2.3</version>
        </dependency>


    </dependencies>



</project>
  1. 一旦分表,主键不建议使用自增,可以使用redis的计数器来每次增加,雪花算法生成唯一id也行

你可能感兴趣的:(spring,spring,boot,java,spring)