Bootstrap

shardingjdbc介绍

1、shardingjdbc介绍

在这里插入图片描述

1.1、读写分离、数据分片(分库分表)中间件:

1.1.1、shardingsphere

  1. proxy:创建一个中间层项目,处理所有的数据库操作分发请求
  2. jdbc:在一个项目的配置文件中配置读写分离

1.1.2、mycat

2、shardingjdbc-demo搭建

2.1、创建项目

项目类型:Spring Initializr
SpringBoot脚手架:https://start.springboot.io
项目名:sharding-jdbc-demo
SpringBoot版本:3.0.5在这里插入图片描述

2.2、添加依赖

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

        <!--操作数据库-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.30</version>
        </dependency>

        <!--mybatisPlus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.3.1</version>
        </dependency>

        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>shardingsphere-jdbc-core</artifactId>
            <version>5.4.0</version>
        </dependency>

        <!--兼容jdk17和spring boot3-->
        <dependency>
            <groupId>org.yaml</groupId>
            <artifactId>snakeyaml</artifactId>
            <version>1.33</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>2.3.8</version>
        </dependency>

    </dependencies>

2.3、application.yml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
    url: jdbc:mysql://192.168.74.148:3306/mydb2?serverTimezone=GMT%2B8

2.4、创建实体类 User

create table t_user
(
    id   BIGINT auto_increment,
    name VARCHAR(30) null,
    constraint t_user_pk
        primary key (id)
);
package com.atguigu.sharding.jdbc.demo.bean;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName(value = "t_user")
public class User {

    private Long id;

    private String name;

}

2.5、创建 UserMapper

package com.atguigu.sharding.jdbc.demo.mapper;
import com.atguigu.sharding.jdbc.demo.bean.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper extends BaseMapper<User> {
}

2.6、创建测试类 ShardingJdbcDemoApplicationTests

package com.atguigu.sharding.jdbc.demo;

import com.atguigu.sharding.jdbc.demo.mapper.UserMapper;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class ShardingJdbcDemoApplicationTests {

    @Resource
    UserMapper userMapper;

    @Test
    void contextLoads() {
        userMapper.selectList(null).forEach(System.out::println);
    }

}

2024-09-23T21:17:04.983+08:00  INFO 3416 --- [           main] a.s.j.d.ShardingJdbcDemoApplicationTests : Started ShardingJdbcDemoApplicationTests in 2.135 seconds (process running for 3.649)
2024-09-23T21:17:05.518+08:00  INFO 3416 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2024-09-23T21:17:06.147+08:00  INFO 3416 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Added connection com.mysql.cj.jdbc.ConnectionImpl@6a4a2090
2024-09-23T21:17:06.150+08:00  INFO 3416 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
User(id=1, name=张三)
User(id=2, name=李四)
2024-09-23T21:17:06.250+08:00  INFO 3416 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2024-09-23T21:17:06.256+08:00  INFO 3416 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.

;