java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > springboot使用yml文件方式配置shardingsphere

springboot如何使用yml文件方式配置shardingsphere

作者:紫荆-往往

这篇文章主要介绍了springboot如何使用yml文件方式配置shardingsphere问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

1、创建maven项目

引入依赖包

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
            <version>4.1.1</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.22</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>
    </dependencies>

2、配置yml文件

spring:
  shardingsphere:
    datasource:
      names: m1
      m1:
        type: com.alibaba.druid.pool.DruidDataSource
        url: jdbc:mysql://ip:3306/test?serverTimezone=GMT%2B8
        driver-class-name: com.mysql.jdbc.Driver
        username: 用户名
        password: 密码
#      m2:
#        type: com.alibaba.druid.pool.DruidDataSource
#        url: jdbc:mysql://10.0.5.118:3306/test2?serverTimezone=GMT%2B8
#        driver-class-name: com.mysql.jdbc.Driver
#        username: root
#        password: dcg123456
    sharding:
      tables:
        course:
          actual-data-nodes: m1.course_$->{1..2}
          key-generator:
            column: cid
            type: SNOWFLAKE
#          databaseStrategy: #分库策略
#            inline:
#              shardingColumn: cid
#              algorithmExpression: m$->{cid % 2 +1}
#分表策略
          table-strategy:
            inline:
              sharding-column: cid
              algorithm-expression: course_$->{cid%2+1}
 #打印sql
    props:
      sql:
        show: true

3、创建course实体类及dao层代码

package com.lifwe.shardingsphere.entity;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
/**
 * @author lifwe
 * @version 1.0
 * @ClassName :Course
 * @Description :
 * @date 2021/3/5 20:27
 */
@Data
@NoArgsConstructor
@ToString
public class Course {
    private Long cid;
    private String cname;
    private Long userId;
    private String cstatus;
}
package com.lifwe.shardingsphere.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lifwe.shardingsphere.entity.Course;
import org.apache.ibatis.annotations.Mapper;
/**
 * @author lifwe
 * @version 1.0
 * @ClassName :CourseDao
 * @Description :
 * @date 2021/3/5 20:32
 */
@Mapper
public interface CourseDao extends BaseMapper<Course> {
}

4、进行junit测试

上图所指示,两个分片表插入前是无数据的,执行测试用例,

日志为:

查看两个分片表数据,

结果如下所示:

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文