zoukankan      html  css  js  c++  java
  • SpringBoot + ShardingSphere 实现分库分表

    ShardingSphere定位为轻量级 Java 框架,在 Java 的 JDBC 层提供的额外服务。 它使用客户端直连数据库,以 jar 包形式提供服务,无需额外部署和依赖,可理解为增强版的 JDBC 驱动,完全兼容 JDBC 和各种 ORM 框架。

    代码实现:

         以用户表为例, 我们需要将它分成2个库,每个库三张表,根据id字段取模确定最终数据的位置
            ds0:
               user_0
               user_1
               user_2
           ds1:
               user_0
               user_1
               user_2

     ds0,ds1两个数据库的三张表的逻辑表都为user表,可以使用以下脚本建表:
        CREATE TABLE `user_0` (
          `id` bigint(11) NOT NULL,
           `name` varchar(20) COLLATE utf8_bin DEFAULT NULL,
           `age` int(11) DEFAULT NULL,
            PRIMARY KEY (`id`)
          ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
         


         修改pom.xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.3.0.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.mscloudmesh.sharding</groupId>
        <artifactId>springboot-shardingsphere</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>springboot-shardingsphere</name>
        <description>springboot-shardingsphere</description>
    
        <properties>
            <java.version>1.8</java.version>
            <mybatis-plus.version>3.1.1</mybatis-plus.version>
            <sharding-sphere.version>4.1.0</sharding-sphere.version>
            <mysql-connector.version>8.0.18</mysql-connector.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>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
    
    
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql-connector.version}</version>
            </dependency>
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>${mybatis-plus.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.shardingsphere</groupId>
                <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
                <version>${sharding-sphere.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.shardingsphere</groupId>
                <artifactId>sharding-jdbc-spring-namespace</artifactId>
                <version>${sharding-sphere.version}</version>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.12</version>
            </dependency>
    
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>

     mybatis-plus配置信息类: 

    package com.mscloudmesh.sharding.springbootshardingsphere.config;
    
    import com.baomidou.mybatisplus.core.parser.ISqlParser;
    import com.baomidou.mybatisplus.extension.parsers.BlockAttackSqlParser;
    import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
    import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @author kevin
     * @date 2020/6/8
     * @desc mybatis-plus配置类
     */
    @Configuration
    @MapperScan("com.mscloudmesh.sharding.springbootshardingsphere.dao")
    public class MybatisPlusConfig {
        
        @Bean
        public PaginationInterceptor paginationInterceptor() {
            PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
            List<ISqlParser> sqlParserList = new ArrayList<>();
            sqlParserList.add(new BlockAttackSqlParser());
            paginationInterceptor.setSqlParserList(sqlParserList);
            return new PaginationInterceptor();
        }
    
        
        @Bean
        public PerformanceInterceptor performanceInterceptor() {
            return new PerformanceInterceptor();
        }
    }

     用户实体类:

    package com.mscloudmesh.sharding.springbootshardingsphere.model;
    import com.baomidou.mybatisplus.annotation.TableName;
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    import java.util.Date;
    
    /**
     * @author kevin
     * @date 2020/6/8
     * @desc 用户实现类
     */
    @Data
    @TableName("user")
    @AllArgsConstructor
    @NoArgsConstructor
    public class User {
        private Long id;
        private String name;
        private int age;
    
    }

    用户dao接口类:
      

    package com.mscloudmesh.sharding.springbootshardingsphere.dao;
    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    import com.mscloudmesh.sharding.springbootshardingsphere.model.User;
    /**
     * @author kevin
     * @desc 订单dao层
     */
    public interface UserMapper extends BaseMapper<User> {
    }
    
    用户服务接口:
      
    
    package com.mscloudmesh.sharding.springbootshardingsphere.service;
    import com.baomidou.mybatisplus.extension.service.IService;
    import com.mscloudmesh.sharding.springbootshardingsphere.model.User;
    /**
     * @author kevin
     * @desc 用户服务接口
     * @date 2020/6/8
     */
    public interface UserService extends IService<User> {
    }
    用户服务实现层:
      
    
    package com.mscloudmesh.sharding.springbootshardingsphere.service.impl;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import com.mscloudmesh.sharding.springbootshardingsphere.dao.UserMapper;
    import com.mscloudmesh.sharding.springbootshardingsphere.model.User;
    import com.mscloudmesh.sharding.springbootshardingsphere.service.UserService;
    import org.springframework.stereotype.Service;
    /**
     * @author kevin
     * @date 2020/6/8
     * @desc 用户服务实现层
     **/
    
    @Service
    public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    }

    springboot启动类:
      

    package com.mscloudmesh.sharding.springbootshardingsphere;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class SpringbootShardingsphereApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringbootShardingsphereApplication.class, args);
        }
    
    }

    测试类userController:

      

    package com.mscloudmesh.sharding.springbootshardingsphere.controller;
    
    
    import com.mscloudmesh.sharding.springbootshardingsphere.model.User;
    import com.mscloudmesh.sharding.springbootshardingsphere.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class UserController {
    
        @Autowired
        private UserService userService;
    
        @GetMapping("/add")
        public User add() {
            User user = new User();
            user.setName("test");
            user.setAge(30);
            userService.save(user);
            return user;
        }
    }
    请求 http://localhost:8761/add
    控制台输出:
      
  • 相关阅读:
    HashMap按键排序和按值排序
    LeetCode 91. Decode Ways
    LeetCode 459. Repeated Substring Pattern
    JVM
    LeetCode 385. Mini Parse
    LeetCode 319. Bulb Switcher
    LeetCode 343. Integer Break
    LeetCode 397. Integer Replacement
    LeetCode 3. Longest Substring Without Repeating Characters
    linux-网络数据包抓取-tcpdump
  • 原文地址:https://www.cnblogs.com/mscm/p/13066243.html
Copyright © 2011-2022 走看看