Spring Boot 整合 Sharding-JDBC + Mybatis-Plus 實現分庫分表

2020-10-02 16:00:01

一、 Sharding-jdbc簡介
「Sharding-jdbc是開源的資料庫操作中介軟體;定位為輕量級Java框架,在Java的JDBC層提供的額外服務。它使用使用者端直連資料庫,以jar包形式提供服務,無需額外部署和依賴,可理解為增強版的JDBC驅動,完全相容JDBC和各種ORM框架。
官方檔案地址:https://shardingsphere.apache.org/document/current/cn/overview/
本文demo實現了分庫分表功能。如有錯誤,歡迎各位在評論中指出。不勝感激!

二、專案結構
首先建立一個一般的Spring boot專案,專案採用三層架構,
結構圖如下:
在這裡插入圖片描述

POM.xml檔案如下:
![<?xml version="1.0" encoding="UTF-8"?>

4.0.0

org.springframework.boot
spring-boot-starter-parent
2.1.6.RELEASE


com.macky
spring-boot-shardingjdbc
0.0.1-SNAPSHOT
spring-boot-shardingjdbc
Demo project for spring-boot-shardingjdbc

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <!--mysql-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <!--Mybatis-Plus-->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.1.1</version>
    </dependency>
    <!--shardingsphere start-->
    <!-- for spring boot -->
    <dependency>
        <groupId>io.shardingsphere</groupId>
        <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
        <version>3.1.0</version>
    </dependency>
    <!-- for spring namespace -->
    <dependency>
        <groupId>io.shardingsphere</groupId>
        <artifactId>sharding-jdbc-spring-namespace</artifactId>
        <version>3.1.0</version>
    </dependency>
    <!--shardingsphere end-->
    <!--lombok-->
    <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>

]

實體類以書本為例

![package com.macky.springbootshardingjdbc.entity;

import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import groovy.transform.EqualsAndHashCode;
import lombok.Data;
import lombok.experimental.Accessors;
]

@Data
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
@TableName(「book」)
public class Book extends Model {
private int id;
private String name;
private int count;
}]

開放儲存和查詢兩個介面,程式碼如下:

![package com.macky.springbootshardingjdbc.controller;

import com.macky.springbootshardingjdbc.entity.Book;
import com.macky.springbootshardingjdbc.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class BookController {

@Autowired
BookService bookService;

@RequestMapping(value = "/book", method = RequestMethod.GET)
public List<Book> getItems(){
    return bookService.getBookList();
}

@RequestMapping(value = "/book",method = RequestMethod.POST)
public Boolean saveItem(Book book){
    return bookService.save(book);
}

BookServiceImpl.java

![package com.macky.springbootshardingjdbc.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.macky.springbootshardingjdbc.entity.Book;
import com.macky.springbootshardingjdbc.mapper.BookMapper;
import com.macky.springbootshardingjdbc.service.BookService;
import org.springframework.stereotype.Service;

import java.util.List;

/**

  • @author Mac

  • @Title ServiceImpl

  • @Description:Doer

  • @date 2020/10/12 20:47
    */
    @Service
    public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {

    @Override
    public List getBookList() {
    return baseMapper.selectList(Wrappers.lambdaQuery());
    }

    @Override
    public boolean save(Book book) {
    return super.save(book);
    }

BookMapper.java

package com.macky.springbootshardingjdbc.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.macky.springbootshardingjdbc.entity.Book;

建立資料庫表,DDL語句如下

建立資料庫表資料
CREATE DATABASE IF NOT EXISTS db0;USE db0;DROP TABLE IF EXISTS book_0;CREATE TABLE book_0 ( id INT ( 11 ) NOT NULL, name VARCHAR ( 255 ) DEFAULT NULL, count INT ( 11 ) DEFAULT NULL, PRIMARY KEY ( id )) ENGINE = INNODB DEFAULT CHARSET = utf8mb4;DROP TABLE IF EXISTS book_1;CREATE TABLE book_1 ( id INT ( 11 ) NOT NULL, name VARCHAR ( 255 ) DEFAULT NULL, count INT ( 11 ) DEFAULT NULL, PRIMARY KEY ( id )) ENGINE = INNODB DEFAULT CHARSET = utf8mb4;CREATE DATABASE IF NOT EXISTS db1;USE db1;DROP TABLE IF EXISTS book_0;CREATE TABLE book_0 ( id INT ( 11 ) NOT NULL, name VARCHAR ( 255 ) DEFAULT NULL, count INT ( 11 ) DEFAULT NULL, PRIMARY KEY ( id )) ENGINE = INNODB DEFAULT CHARSET = utf8mb4;DROP TABLE IF EXISTS book_1;CREATE TABLE book_1 ( id INT ( 11 ) NOT NULL, name VARCHAR ( 255 ) DEFAULT NULL, count INT ( 11 ) DEFAULT NULL, PRIMARY KEY ( id )) ENGINE = INNODB DEFAULT CHARSET = utf8mb4;CREATE DATABASE IF NOT EXISTS db2;USE db2;DROP TABLE IF EXISTS book_0;CREATE TABLE book_0 ( id INT ( 11 ) NOT NULL, name VARCHAR ( 255 ) DEFAULT NULL, count INT ( 11 ) DEFAULT NULL, PRIMARY KEY ( id )) ENGINE = INNODB DEFAULT CHARSET = utf8mb4;DROP TABLE IF EXISTS book_1;CREATE TABLE book_1 ( id INT ( 11 ) NOT NULL, name VARCHAR ( 255 ) DEFAULT NULL, count INT ( 11 ) DEFAULT NULL, PRIMARY KEY ( id )) ENGINE = INNODB DEFAULT CHARSET = utf8mb4

三、總結

分庫分表實現按照官方檔案做一個demo是第一步,如需深入還需要研究原始碼,研究架構,研究思想;此文僅作為入門demo搭建指南,如需深入理解,還請移步至官方檔案。