【Sharding JDBC】分库分表小demo


引入依赖



    com.alibaba
    druid-spring-boot-starter
    1.1.20



    mysql
    mysql-connector-java



    org.apache.shardingsphere
    sharding-jdbc-spring-boot-starter
    4.0.0-RC1



    com.baomidou
    mybatis-plus-boot-starter
    3.0.5



    org.projectlombok
    lombok

实体类

package com.yibing.entity;
import lombok.Data;
/**
 * @author zhangyb
 * @date 2022/5/10
 */
@Data
public class Course {
    private Long cid;
    private String cname;
    private Long userId;
    private String cstatus;
}

使用MyBatisPlus建立Mapper

package com.yibing.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yibing.entity.Course;
import org.springframework.stereotype.Repository;
/**
 * @author zhangyb
 * @date 2022/5/10
 */
@Repository
public interface CourseMapper extends BaseMapper {
}

appltaction.proprtties

# 应用名称
spring.application.name=sharding-jdbc-study
server.port=8080
server.servlet.context-path=/sharding



# shardingjdbc分片策略
# 配置数据源,给数据源起名称,
# 水平分库,配置两个数据源
spring.shardingsphere.datasource.names=m0,m1

# 一个实体类对应两张表,覆盖
spring.main.allow-bean-definition-overriding=true

#配置第一个数据源具体内容,包含连接池,驱动,地址,用户名和密码
spring.shardingsphere.datasource.m0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m0.url=jdbc:mysql://localhost:3306/sharding_study_0?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m0.username=root
spring.shardingsphere.datasource.m0.password=123456

#配置第二个数据源具体内容,包含连接池,驱动,地址,用户名和密码
spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m1.url=jdbc:mysql://localhost:3306/sharding_study_1?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m1.username=root
spring.shardingsphere.datasource.m1.password=123456

#指定数据库分布情况,数据库里面表分布情况
# m0  m1    course_0 course_1
spring.shardingsphere.sharding.tables.course.actual-data-nodes=m$->{0..1}.course_$->{0..1}

# 指定course表里面主键cid 生成策略  SNOWFLAKE
spring.shardingsphere.sharding.tables.course.key-generator.column=cid
spring.shardingsphere.sharding.tables.course.key-generator.type=SNOWFLAKE

# 指定数据库分片策略 约定user_id是偶数添加m0,是奇数添加m1
spring.shardingsphere.sharding.tables.course.database-strategy.inline..sharding-column=cid
spring.shardingsphere.sharding.tables.course.database-strategy.inline.algorithm-expression=m$->{cid % 2}

# 指定表分片策略  约定cid值偶数添加到course_0表,如果cid是奇数添加到course_1表
spring.shardingsphere.sharding.tables.course.table-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.tables.course.table-strategy.inline.algorithm-expression=course_$->{user_id % 2}

# 配置公共表
spring.shardingsphere.sharding.broadcast-tables=common
spring.shardingsphere.sharding.tables.common.key-generator.column=id
spring.shardingsphere.sharding.tables.common.key-generator.type=SNOWFLAKE
# 打开sql输出日志
spring.shardingsphere.props.sql.show=false