Sharding-jdbc实现水平分表

silencehgt 2020-07-19

1、搭建环境

(1)SpringBoot2.2.1+MyBatisPlus+Sharding-JDBC+Druid连接池

(2)创建SpringBoot工程,版本2.2.1

Sharding-jdbc实现水平分表

(3)引入相关依赖

<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>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.20</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
            <version>4.0.0-RC1</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.0.5</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

2、按照水平分表的方式创建数据库,创建数据库表

(1)创建数据库 course_db

(2)在数据库中创建两张表 course_1 和 course_2

(3)约定一种规则:如果添加的课程id是偶数,把数据添加到course_1

如果是基数,则添加到course2

Sharding-jdbc实现水平分表

3、编写代码,完成对分库分表后数据的操作

(1)创建实体类,mapper

Sharding-jdbc实现水平分表

4、配置sharding-jdbc的分片策略

(1)在项目的配置文件中进行配置

#sharding-jdbc 分片策略
# 数据源名称,多数据源以逗号分隔
spring.shardingsphere.datasource.names=m1

#配置数据源的详细信息
# 数据库连接池
spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource  
#数据库驱动类名
spring.shardingsphere.datasource.m1.driver-class-name= com.mysql.cj.jdbc.Driver
# 数据库 URL 连接
spring.shardingsphere.datasource.m1.url=jdbc:mysql://localhost:3306/course_db?serverTimezone=GMT%2B8
# 数据库用户名
spring.shardingsphere.datasource.m1.username= root
# 数据库密码
spring.shardingsphere.datasource.m1.password=houchen

# 指定course表的分布情况,配置在哪个数据库,表名称是什么  m1.course_1  m1.course_2
spring.shardingsphere.sharding.tables.course.actual-data-nodes=m1.course_$->{1..2}

#指定course表中主键的生成策略  SNOWFLAKE:雪花算法,随机生成主键Id
spring.shardingsphere.sharding.tables..course.key-generator.column= = cid
spring.shardingsphere.sharding.tables..course.key-generator.type= =SNOWFLAKE

# 指定分片的策略 约定cid值 : 偶数--》course_1  奇数-->course_2
spring.shardingsphere.sharding.tables.course.table-strategy.inline.sharding-column= cid
spring.shardingsphere.sharding.tables.course.table-strategy.inline.algorithm-expression=course_$->{cid%2 +1}

#打开sql的输出日志
spring.shardingsphere.props.sql.show = true

5、编写测试代码

@RunWith(SpringRunner.class)
@SpringBootTest
public class ShardingJdbcDdemoApplicationTest {

    @Autowired
    private CourseMapper courseMapper;

    @Test
    public void testInsert(){
        Course course =new Course();
        course.setCname("java");
        course.setUserId(100L);
        course.setCstatus("normal");
        courseMapper.insert(course);
    }

}

Sharding-jdbc实现水平分表

相关推荐