ShardingSphere学习(4)- Sharding-JDBC 实现垂直分库

垂直分库
其实就是配置两个数据源,在代码书写中对这两个数据库的表可以不用手动切换数据源,无感操作。

1. 数据库结构

在这里插入图片描述

2. 配置文件

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

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

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:3333/course_db?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m1.username=root
spring.shardingsphere.datasource.m1.password=root

spring.shardingsphere.datasource.m2.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m2.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m2.url=jdbc:mysql://localhost:3333/user_db?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m2.username=root
spring.shardingsphere.datasource.m2.password=root

# 配置数据库里面专库专表
spring.shardingsphere.sharding.tables.t_course.actual-data-nodes=m$->{1}.t_course
spring.shardingsphere.sharding.tables.t_user.actual-data-nodes=m$->{2}.t_user

# 配置主键 id 生成策略 SNOWFLAKE 雪花算法
spring.shardingsphere.sharding.tables.t_course.key-generator.column=id
spring.shardingsphere.sharding.tables.t_course.key-generator.type=SNOWFLAKE
spring.shardingsphere.sharding.tables.t_user.key-generator.column=id
spring.shardingsphere.sharding.tables.t_user.key-generator.type=SNOWFLAKE

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

3. 创建实体类

package com.wuk.shardingspheredemo.model;

import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

/**
 * @author wuk
 * @description:
 * @menu
 * @date 2021/10/22 14:07
 */
@Data
@TableName(value = "t_course")
public class Course {

    private Long id;

    private String cname;

    private Long userId;

    private String cstatus;
}

package com.wuk.shardingspheredemo.model;

import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;

/**
 * @author wuk
 * @description:
 * @menu
 * @date 2021/10/14 16:31
 */
@Data
@TableName(value = "t_user")
public class User {

    private Long id;

    private String name;

    private int age;
}

4. 测试

package com.wuk.shardingspheredemo;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wuk.shardingspheredemo.mapper.CourseMapper;
import com.wuk.shardingspheredemo.mapper.UserMapper;
import com.wuk.shardingspheredemo.model.Course;
import com.wuk.shardingspheredemo.model.User;
import com.wuk.shardingspheredemo.service.CourseService;
import com.wuk.shardingspheredemo.service.UserService;
import com.wuk.shardingspheredemo.service.impl.UserServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@SpringBootTest
class ShardingSphereDemoApplicationTests {

    @Resource
    private CourseMapper courseMapper;

    @Resource
    private CourseService courseService;

    @Resource
    private UserMapper userMapper;

    @Resource
    private UserService userService;

    //添加操作
    @Test
    public void addCourseDb() {
        List<Course> list = new ArrayList<>();
        for (int i=1;i<10;i++){
            Course course = new Course();
            course.setCname("javademo3");
            course.setUserId(Long.parseLong(String.valueOf(i)));
            course.setCstatus("Normal3");
            list.add(course);
        }
        courseService.saveBatch(list);

        List<User> userList = new ArrayList<>();
        for (int i=1;i<10;i++){
            User user = new User();
            user.setName("javademo3");
            user.setAge(10+i);
            userList.add(user);
        }
        userService.saveBatch(userList);
    }
    //查询操作
    @Test
    public void findCourseDb() {
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        //设置 userid 值
        wrapper.eq("user_id",1452902324366213125L);
        //设置 cid 值
        Course course = courseMapper.selectOne(wrapper);
        System.out.println(course);
    }


}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

技术闲聊DD

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值