当前位置:网站首页>Shardingsphere's level table practice (III)
Shardingsphere's level table practice (III)
2022-07-29 03:14:00 【Melting pole】
summary
Set up the environment
Springboot2.3.2.RELEASE + Tkmybatis + Sharding-jdbc + Druid
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.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.study</groupId>
<artifactId>shardingjdbcdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>shardingjdbcdemo</name>
<description>Demo project for Spring Boot</description>
<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>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.14</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.20</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-RC1</version>
</dependency>
<!-- tkmybatis -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.1.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
Building database 、 Build table
library 、 Table design :
- Create database course_db.
- Create two tables in the database course_1 and course_2.
- Rules of engagement : If you add a course id It is an even number that adds data to course_1, Add odd numbers to course_2.
New database course_db.
new table course_1 and course_2.
CREATE TABLE `course_1` (
`cid` bigint(20) NOT NULL,
`cname` varchar(50) NOT NULL,
`user_id` bigint(20) NOT NULL,
`cstatus` varchar(10) NOT NULL,
PRIMARY KEY (`cid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
The code structure
package com.study.shardingjdbcdemo.entity;
import lombok.Data;
@Data
public class Course {
@Id
private Long cid;
private String cname;
@Column(name = "user_id")
private Long userId;
private String cstatus;
}
package com.study.shardingjdbcdemo.mapper;
import com.study.shardingjdbcdemo.entity.Course;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;
@Repository
public interface CourseMapper extends Mapper<Course> {
}
package com.study.shardingjdbcdemo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.study.shardingjdbcdemo.mapper")
public class ShardingjdbcdemoApplication {
public static void main(String[] args) {
SpringApplication.run(ShardingjdbcdemoApplication.class, args);
}
}
package com.study.shardingjdbcdemo;
import com.study.shardingjdbcdemo.mapper.CourseMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class ShardingjdbcdemoApplicationTests {
@Autowired
private CourseMapper courseMapper;
@Test
void contextLoads() {
}
}
To configure Sharding-JDBC Fragmentation strategy
application.properties
# Configure fragmentation strategy
# Configure data sources
spring.shardingsphere.datasource.names=m1
spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m1.url=jdbc:mysql://localhost:3306/course_db?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m1.username=root
spring.shardingsphere.datasource.m1.password=123456
# Appoint course Table distribution , Which database is the configuration table in , What are the table names
spring.shardingsphere.sharding.tables.course.actual-data-nodes=m1.course_$->{
1..2}
# Appoint course Primary key cid Generation strategy SNOWFLAKE
spring.shardingsphere.sharding.tables.course.key-generator.column=cid
spring.shardingsphere.sharding.tables.course.key-generator.type=SNOWFLAKE
# Specify the fragmentation policy , cid Is an even number added to course_1 In the table ,cid Is an odd number added to course_2
spring.shardingsphere.sharding.tables.course.table-strategy.inline.sharding-column=cid
spring.shardingsphere.sharding.tables.course.table-strategy.inline.algorithm-expression=course_$->{
cid % 2 + 1}
# open sql Output log
spring.shardingsphere.props.sql.show=true
test
The test case
package com.study.shardingjdbcdemo;
import com.study.shardingjdbcdemo.entity.Course;
import com.study.shardingjdbcdemo.mapper.CourseMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class ShardingjdbcdemoApplicationTests {
@Autowired
private CourseMapper courseMapper;
@Test
void add() {
for (int i = 0; i < 10; i++) {
Course course = new Course();
course.setCname("java");
course.setUserId(100L);
course.setCstatus("normal");
// Pay attention to insertSelective, Otherwise, an error will be reported , because sharding-jdbc The primary key field will be generated and spliced into sql in , If sql Statement with cid The primary key will repeatedly report errors
courseMapper.insertSelective(course);
}
}
@Test
void query() {
List<Course> courses = courseMapper.selectAll();
courses.forEach(System.out::println);
}
}
Test error
The bean 'dataSource', defined in class path resource [org/apache/shardingsphere/shardingjdbc/spring/boot/SpringBootConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [com/alibaba/druid/spring/boot/autoconfigure/DruidDataSourceAutoConfigure.class] and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
Solution
application.properties Add configuration in
# Solve a problem Course The problem that entity classes cannot correspond to two tables
spring.main.allow-bean-definition-overriding=true
边栏推荐
- 单例模式(饿汉式 懒汉式)
- Watermelon book learning Chapter 6 -- SVM
- 【机器人学习】机械臂抓手matlab运动学与admas动力学分析
- Verilog: blocking assignment and non blocking assignment
- C陷阱与缺陷 第3章 语义“陷阱” 3.8 运算符&&、||和!
- 接口自动化测试实践指导(上):接口自动化需要做哪些准备工作
- Feedback function of conference OA
- Navicat new database
- C and pointer Chapter 3 semantic "trap" 3.5 null pointer is not a string
- 【FreeSwitch开发实践】UniMRCP编译与安装
猜你喜欢
4000 多字学懂弄通 js 中 this 指向问题,顺便手写实现 call、apply 和 bind
01-sdram: Code of initialization module
CentOS install mysql8
力扣刷题之数组序号计算(每日一题7/28)
01-SDRAM:初始化模块的代码
Does domestic ERP have a chance to beat sap?
Inventory of domestic and foreign project collaborative management software: SAAS and customization become a trend
Self study notes on Apache file management -- mapping folders and configuring Apache virtual machines based on single IP and multi domain names
爆肝整理JVM十大模块知识点总结,不信你还不懂
Shell编程规范与变量
随机推荐
基于单片机烟雾温湿度甲醛监测设计
年内首个“三连跌” 95号汽油回归“8元时代“
Multi table (Association) query of SQL query data
逐步分析类的拆分之案例——五彩斑斓的小球碰撞
Shell编程规范与变量
照片比例校正工具:DxO ViewPoint 3 直装版
扫雷简单版
Detailed steps for installing MySQL 8.0 under Linux
MYSQL入门与进阶(十一)
STC单片机驱动1.8‘TFT SPI屏幕演示示例(含资料包)
MYSQL入门与进阶(十四)
MySQL large table joint query optimization, large transaction optimization, avoiding transaction timeout, lock wait timeout and lock table
Navicat new database
正则表达绕过waf
12_ UE4 advanced_ Change a more beautiful character model
ShardingSphere之水平分表实战(三)
Codeworks 5 questions per day (average 1500) - day 25
增量实时灾备笔记
MYCAT read / write separation configuration
Digital image processing Chapter 10 - image segmentation