当前位置: 首页 > news >正文

贵阳网站建设费用今天的最新新闻内容

贵阳网站建设费用,今天的最新新闻内容,wordpress 实名,小程序商店多少钱目录一、ShardingSphere简介二、ShardingSphere-分库分表1、垂直拆分(1)垂直分库(2)垂直分表2、水平拆分(1)水平分库(2)水平分表三、水平分库操作1、创建数据库和表2、配置分片的规则…

目录

  • 一、ShardingSphere简介
  • 二、ShardingSphere-分库分表
    • 1、垂直拆分
      • (1)垂直分库
      • (2)垂直分表
    • 2、水平拆分
      • (1)水平分库
      • (2)水平分表
  • 三、水平分库操作
    • 1、创建数据库和表
    • 2、配置分片的规则
    • 3、测试类
  • 四、水平分表操作
    • 1、引入依赖
    • 2、创建数据库、表
    • 3、分片策略
    • 4、编写代码实现
      • (1)实体类
      • (2)编写Mapper
      • (3)启动类修改(添加扫描mapper的入口)
      • (4)测试类(采用springboot给我们提供的)
  • 五、垂直分库/分表操作
    • 1、创建数据库、表
    • 2、分片策略
    • 3、编写代码实现
      • (1)创建User实体类
      • (2)Mapper
    • 4、测试
  • 六、公共表
    • 1、概念
    • 2、实现
      • (1)在三个数据库中创建公共表
      • (2)公共表的配置
      • (3)测试代码
    • 3、相关问题及解决方法
      • (1)Caused by: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'null' in 'class com.chenxin.shardingsphere.entity.Udict'

一、ShardingSphere简介

Apache ShardingSphere 是一个开源的分布式数据库中间件解决方案组成的生态圈,且它的产品有Sharding-JDBC和Sharding-Proxy组成(他们两个之间是独立的),同时又能混合部署(组合起来一起使用)。它们都提供了标准化的数据分片、分布式事务和数据库的治理能力,可适用如Java、云原生开发的应用场景。

ShardingSphere定位是关系型数据库中间件,目的是充分为了合理地在分布式的场景下利用关系型数据库的计算能力和存储能力,而不是实现一个全新的关系型数据库。

二、ShardingSphere-分库分表

1、垂直拆分

(1)垂直分库

垂直分库:把单一的数据库按照业务的不同进行划分(专库专表)
在这里插入图片描述

(2)垂直分表

操作数据库中的某张表,我们把这张表里面的一部分字段拿出来存储到一张新的表里,剩下的字段放在另一张表里。

在这里插入图片描述

2、水平拆分

(1)水平分库

水平分库相当于把数据库水平切割,原来一个表中的数据可能会分配到不同的数据库中,这就是水平分库。
在这里插入图片描述

(2)水平分表

水平分表就是把表中的数据进行了水平切割,意味着按照行进行切割,也就是说不同行的数据被切割后可能在不同的表中。
在这里插入图片描述

三、水平分库操作

1、创建数据库和表

-- 创建两个数据库
CREATE DATABASE edu_db_1;
CREATE DATABASE edu_db_2;
-- 需要在两个数据库中都执行下面的脚本
CREATE TABLE course_1(cid BIGINT(20) PRIMARY KEY,cname VARCHAR(20) NOT NULL,user_id BIGINT(20) NOT NULL,cstatus VARCHAR(20) NOT NULL
);CREATE TABLE course_2(cid BIGINT(20) PRIMARY KEY,cname VARCHAR(20) NOT NULL,user_id BIGINT(20) NOT NULL,cstatus VARCHAR(20) NOT NULL
);

2、配置分片的规则

spring.shardingsphere.datasource.names=ds-1,ds-2spring.shardingsphere.datasource.ds-1.jdbc-url=jdbc:mysql://localhost:3306/edu_db_1?serverTimezone=Asia/Shanghai&useSSL=false
spring.shardingsphere.datasource.ds-1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds-1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds-1.username=root
spring.shardingsphere.datasource.ds-1.password=rootspring.shardingsphere.datasource.ds-2.jdbc-url=jdbc:mysql://localhost:3306/edu_db_2?serverTimezone=Asia/Shanghai&useSSL=false
spring.shardingsphere.datasource.ds-2.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds-2.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds-2.username=root
spring.shardingsphere.datasource.ds-2.password=rootspring.main.allow-bean-definition-overriding=truespring.shardingsphere.sharding.tables.course.actual-data-nodes=ds-$->{1..2}.course_$->{1..2}spring.shardingsphere.sharding.tables.course.key-generator.column=cid
spring.shardingsphere.sharding.tables.course.key-generator.type=SNOWFLAKEspring.shardingsphere.sharding.tables.course.table-strategy.inline.sharding-column=cid
spring.shardingsphere.sharding.tables.course.table-strategy.inline.algorithm-expression=course_$->{cid % 2 + 1}spring.shardingsphere.sharding.tables.course.database-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.tables.course.database-strategy.inline.algorithm-expression=ds-$->{user_id % 2 + 1}spring.shardingsphere.props.sql.show=true

3、测试类

// ------------- 测试分库 ---------------------@Testvoid addCorseFpdb() {Course course = new Course();course.setCname("java01");// 根据user_id进行分库,偶数是在edu_db_1,基数是在edu_db_2course.setUserId(100L);course.setCstatus("已启用");courseMapper.insert(course);}

四、水平分表操作

1、引入依赖

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.1.17</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>org.apache.shardingsphere</groupId><artifactId>sharding-jdbc-spring-boot-starter</artifactId><version>4.0.0</version></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.0.5</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>
</dependencies>

2、创建数据库、表

create table course_1(cid bigint(20) primary key,cname varchar(20) not null,user_id bigint(20) not null,cstatus varchar(20) not null
);create table course_2(cid bigint(20) primary key,cname varchar(20) not null,user_id bigint(20) not null,cstatus varchar(20) not null
);

3、分片策略

spring.shardingsphere.datasource.names=ds-0spring.shardingsphere.datasource.ds-0.jdbc-url=jdbc:mysql://localhost:3306/course_db?serverTimezone=Asia/Shanghai&useSSL=false
spring.shardingsphere.datasource.ds-0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds-0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds-0.username=root
spring.shardingsphere.datasource.ds-0.password=rootspring.main.allow-bean-definition-overriding=truespring.shardingsphere.sharding.tables.course.actual-data-nodes=ds-0.course_$->{1..2}spring.shardingsphere.sharding.tables.course.key-generator.column=cid
spring.shardingsphere.sharding.tables.course.key-generator.type=SNOWFLAKEspring.shardingsphere.sharding.tables.course.table-strategy.inline.sharding-column=cid
spring.shardingsphere.sharding.tables.course.table-strategy.inline.algorithm-expression=course_$->{cid % 2 + 1}spring.shardingsphere.props.sql.show=true

4、编写代码实现

(1)实体类

import lombok.Data;
import lombok.ToString;@Data
@ToString
public class Course {private Long cid;private String cname;private Long userId;private String cstatus;
}

(2)编写Mapper

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.chenxin.shardingsphere.entity.Course;
import org.apache.ibatis.annotations.Mapper;@Mapper
public interface CourseMapper extends BaseMapper<Course> {
}

(3)启动类修改(添加扫描mapper的入口)

@SpringBootApplication
@MapperScan("com.chenxin.shardingsphere.mapper")
public class ShardingsphereApplication {public static void main(String[] args) {SpringApplication.run(ShardingsphereApplication.class, args);}}

(4)测试类(采用springboot给我们提供的)

@SpringBootTest
class ShardingsphereApplicationTests {@ResourceCourseMapper courseMapper;@Testvoid addCourse() {Course course = null;for (int i = 0; i < 100; i++) {course = new Course();course.setCname("Java");course.setUserId(1000L);course.setCstatus("Nor1");courseMapper.insert(course);}}}

五、垂直分库/分表操作

1、创建数据库、表

CREATE DATABASE user_db;
USE user_db;
CREATE TABLE T_USER(user_id BIGINT(20) NOT NULL PRIMARY KEY,username VARCHAR(100) NOT NULL,ustatus VARCHAR(50) NOT NULL
);

2、分片策略

spring.shardingsphere.datasource.names=ds-1,ds-2,ds-3spring.shardingsphere.datasource.ds-1.jdbc-url=jdbc:mysql://localhost:3306/edu_db_1?serverTimezone=Asia/Shanghai&useSSL=false
spring.shardingsphere.datasource.ds-1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds-1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds-1.username=root
spring.shardingsphere.datasource.ds-1.password=rootspring.shardingsphere.datasource.ds-2.jdbc-url=jdbc:mysql://localhost:3306/edu_db_2?serverTimezone=Asia/Shanghai&useSSL=false
spring.shardingsphere.datasource.ds-2.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds-2.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds-2.username=root
spring.shardingsphere.datasource.ds-2.password=rootspring.shardingsphere.datasource.ds-3.jdbc-url=jdbc:mysql://localhost:3306/user_db?serverTimezone=Asia/Shanghai&useSSL=false
spring.shardingsphere.datasource.ds-3.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds-3.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds-3.username=root
spring.shardingsphere.datasource.ds-3.password=rootspring.main.allow-bean-definition-overriding=true# spring.shardingsphere.sharding.tables.course.actual-data-nodes=ds-$->{1..2}.course_$->{1..2}
spring.shardingsphere.sharding.tables.t_user.actual-data-nodes=ds-$->{3}.t_userspring.shardingsphere.sharding.tables.t_user.key-generator.column=user_id
spring.shardingsphere.sharding.tables.t_user.key-generator.type=SNOWFLAKEspring.shardingsphere.sharding.tables.t_user.table-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.tables.t_user.table-strategy.inline.algorithm-expression=t_userspring.shardingsphere.props.sql.show=true

3、编写代码实现

(1)创建User实体类

import lombok.Data;
import lombok.ToString;// 实体类添加注解
@TableName(value = "t_user")
@Data
@ToString
public class User {private Long userId;private String username;private String ustatus;
}

(2)Mapper

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.chenxin.shardingsphere.entity.User;
import org.apache.ibatis.annotations.Mapper;@Mapper
public interface UserMapper extends BaseMapper<User> {
}

4、测试

// ------------- 测试垂直分库 ----------------------
@Test
void addUser() {User user = new User();user.setUsername("chenxin");user.setUstatus("a");userMapper.insert(user);
}

六、公共表

1、概念

存储固定数据的表,表里面的数据很少发生变化,查询的时候经常进行关联查询

在每个数据库中创建出相同的结构的表(我们将每个数据库中共有的表成为公共表)

2、实现

(1)在三个数据库中创建公共表

CREATE TABLE t_udict(dicid bigint(20) primary key,ustatus varchar(20) not null,uvalue varchar(100) not null
);

(2)公共表的配置

spring.shardingsphere.sharding.broadcast-tables=t_udict
spring.shardingsphere.sharding.tables.t_udict.key-generator.column=dicid
spring.shardingsphere.sharding.tables.t_udict.key-generator.type=SNOWFLAKE

(3)测试代码

创建实体类

import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.ToString;@Data
@ToString
@TableName(value = "t_udict")
public class Udict {private Integer dicid;private String ustatus;private String uvalue;
}

Mapper

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.chenxin.shardingsphere.entity.Udict;
import org.apache.ibatis.annotations.Mapper;@Mapper
public interface UdictMapper extends BaseMapper<Udict> {
}

测试代码

@Test
void addDict() {Udict udict = new Udict();udict.setUstatus("0");udict.setUvalue("已启用");udictMapper.insert(udict);
}@Test
void updateDict() {Udict udict = new Udict();udict.setDicid(792033627201339393L);udict.setUstatus("1");udict.setUvalue("未启用");udictMapper.updateById(udict);
}

3、相关问题及解决方法

(1)Caused by: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named ‘null’ in ‘class com.chenxin.shardingsphere.entity.Udict’

问题原因
数据库中字段名和实体类中相差太大,程序自己对应不上

解决方法

// 需要在实体类指定主键是哪一个属性
@Data
@ToString
@TableName(value = "t_udict")
public class Udict {@TableIdprivate Long dicid;private String ustatus;private String uvalue;
}
http://www.hkea.cn/news/481453/

相关文章:

  • 站群系统哪个好用怎样进行seo推广
  • 淄博网站建设方案网络推广是做什么的
  • 网站建设销售工作职责seo平台怎么样
  • 免费的网站给一个百度seo优化服务项目
  • 现代广告创意设计郑州百度网站优化排名
  • 兰州网站建设哪家专业谷歌seo网站推广
  • 崇信门户网站留言回复上优化seo
  • 网站建设费用有哪些站长工具搜索
  • 云主机能干什么独立站seo怎么做
  • 苏州专业网站建设设计公司免费发布推广信息的b2b
  • 空间 两个网站网络推广培训班
  • 零基础学做网站推广公司简介
  • 公司做网站最好引擎搜索器
  • 济南手工网站建设北京百度seo服务
  • 网站建设原创百度seo官网
  • 徐州企业网站建设衡阳seo服务
  • 网站自然排名优化seo专员是什么职业
  • 视频网站制作广告代理公司
  • wordpress主题域名授权密钥生成镇海seo关键词优化费用
  • 北京东直门+网站建设汕头seo外包平台
  • 长沙 做网站企业网络组网设计
  • 北京哪家做网站优化产品seo基础优化
  • 招商加盟网站建设百度网址安全检测
  • 知名做网站费用2024年将爆发新瘟疫
  • 河北省城乡与建设厅网站企业关键词排名优化哪家好
  • 网站开发合同协议百度百科推广费用
  • 推荐黄的网站产品推广策划
  • 济南网站建设设计公司线上运营推广
  • 小清新 wordpressseo排名是什么意思
  • 从客户—管理者为某一公司做一份电子商务网站管理与维护的方案自媒体是如何赚钱的