• MySQL水平拆分(取模算法)


    一:准备数据库表结构

    create table user0(
    id int unsigned primary key ,
    name varchar(32) not null default '',
    pwd  varchar(32) not null default '')
    engine=myisam charset utf8;
    
    create table user1(
    id int unsigned primary key ,
    name varchar(32) not null default '',
    pwd  varchar(32) not null default '')
    engine=myisam charset utf8;
    
    create table user2(
    id int unsigned primary key ,
    name varchar(32) not null default '',
    pwd  varchar(32) not null default '')
    engine=myisam charset utf8;
    
    
    create table uuid(
    id int unsigned primary key auto_increment)engine=myisam charset utf8;

    二:准备依赖

         <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.38</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>

    三:配置文件

    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.url=jdbc:mysql://localhost:3306/mydata
    spring.datasource.username=root
    spring.datasource.password=123

    四:业务代码

    package com.yjc.service;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class register {
           @Autowired
           JdbcTemplate jdbcTemplate;
    
           @RequestMapping("/register")
           public  String register(String name,String pwd){
                  //往uuid表中添加一条空记录,主键自增
                  String insertSql="INSERT INTO uuid VALUES (NULL);";
                  jdbcTemplate.update(insertSql);
                  //获取最后一次添加数据的主键
                  Long aLong = jdbcTemplate.queryForObject("select last_insert_id()", Long.class);
                  //取余,看看存到哪张表里
                  String table="user"+aLong%3;
                  //插入到该去的表中
                  String insertUserSql = "INSERT INTO " + table + " VALUES ('" + aLong + "','" + name + "','" + pwd + "');";
                  jdbcTemplate.update(insertUserSql);
                  return "success";
           }
    
           @RequestMapping("/get")
           public String get(Long id){
                  //去拿张表里取数据
                  String table ="user"+id%3;
                  String sql = "select name from " + table + "  where id="+id;
                  String name = jdbcTemplate.queryForObject(sql, String.class);
                  return name;
           }
     }

    五:测试

    启动项目请求,模拟用户注册情况

    http://localhost:8080/register?name=123&pwd=123
    http://localhost:8080/get?id=8
  • 相关阅读:
    VBScript 语法函数(ScriptControl 控件)
    VB 读和写配置文件
    【转】VB控件和对象:ScriptControl 控件
    VB ADODB中的RecordSet.Open打开记录的两个参数adOpenKeyset、adLockBatchOptimistic的详细说明
    JavaScript的replace方法与正则表达式结合应用讲解
    jQuery DatePicker
    PHP date与gmdate的区别及如何修改PHP的默认时区
    jQuery获取Select选择的Text和 Value(转)
    JavaScript字符串函数大全
    CONFLICTING TYPES的原因
  • 原文地址:https://www.cnblogs.com/yjc1605961523/p/12331190.html
Copyright © 2020-2023  润新知