shardingjdbc介绍

news2024/9/23 23:22:25

文章目录

  • 1、shardingjdbc介绍
    • 1.1、读写分离、数据分片(分库分表)中间件:
      • 1.1.1、shardingsphere
      • 1.1.2、mycat
  • 2、shardingjdbc-demo搭建
    • 2.1、创建项目
    • 2.2、添加依赖
    • 2.3、application.yml
    • 2.4、创建实体类 User
    • 2.5、创建 UserMapper
    • 2.6、创建测试类 ShardingJdbcDemoApplicationTests

1、shardingjdbc介绍

在这里插入图片描述

1.1、读写分离、数据分片(分库分表)中间件:

1.1.1、shardingsphere

  1. proxy:创建一个中间层项目,处理所有的数据库操作分发请求
  2. jdbc:在一个项目的配置文件中配置读写分离

1.1.2、mycat

2、shardingjdbc-demo搭建

2.1、创建项目

项目类型:Spring Initializr
SpringBoot脚手架:https://start.springboot.io
项目名:sharding-jdbc-demo
SpringBoot版本:3.0.5在这里插入图片描述

2.2、添加依赖

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--操作数据库-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.30</version>
        </dependency>

        <!--mybatisPlus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.3.1</version>
        </dependency>

        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>shardingsphere-jdbc-core</artifactId>
            <version>5.4.0</version>
        </dependency>

        <!--兼容jdk17和spring boot3-->
        <dependency>
            <groupId>org.yaml</groupId>
            <artifactId>snakeyaml</artifactId>
            <version>1.33</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>2.3.8</version>
        </dependency>

    </dependencies>

2.3、application.yml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
    url: jdbc:mysql://192.168.74.148:3306/mydb2?serverTimezone=GMT%2B8

2.4、创建实体类 User

create table t_user
(
    id   BIGINT auto_increment,
    name VARCHAR(30) null,
    constraint t_user_pk
        primary key (id)
);
package com.atguigu.sharding.jdbc.demo.bean;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName(value = "t_user")
public class User {

    private Long id;

    private String name;

}

2.5、创建 UserMapper

package com.atguigu.sharding.jdbc.demo.mapper;
import com.atguigu.sharding.jdbc.demo.bean.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper extends BaseMapper<User> {
}

2.6、创建测试类 ShardingJdbcDemoApplicationTests

package com.atguigu.sharding.jdbc.demo;

import com.atguigu.sharding.jdbc.demo.mapper.UserMapper;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class ShardingJdbcDemoApplicationTests {

    @Resource
    UserMapper userMapper;

    @Test
    void contextLoads() {
        userMapper.selectList(null).forEach(System.out::println);
    }

}

2024-09-23T21:17:04.983+08:00  INFO 3416 --- [           main] a.s.j.d.ShardingJdbcDemoApplicationTests : Started ShardingJdbcDemoApplicationTests in 2.135 seconds (process running for 3.649)
2024-09-23T21:17:05.518+08:00  INFO 3416 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2024-09-23T21:17:06.147+08:00  INFO 3416 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Added connection com.mysql.cj.jdbc.ConnectionImpl@6a4a2090
2024-09-23T21:17:06.150+08:00  INFO 3416 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
User(id=1, name=张三)
User(id=2, name=李四)
2024-09-23T21:17:06.250+08:00  INFO 3416 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2024-09-23T21:17:06.256+08:00  INFO 3416 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2158739.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

筛子排序(SieveSort)

当你手头有了支持AVX-512&#xff08;SIMD&#xff09;的i9-11900K&#xff0c;你最想做什么&#xff1f; i9-11900K&#xff1f;现在都14代了&#xff0c;谁还用11代的&#xff1f; 但12代以上就没有AVX-512了&#xff01; AVX-512有什么特别之处&#xff1f;有了这个硬件支…

How do you send files to the OpenAI API?

题意&#xff1a;你如何向 OpenAI API 发送文件 问题背景&#xff1a; For fun I wanted to try to make a tool to ask chatgpt to document rust files. I found an issue, in that the maximum message length the API allows seems to be 2048 characters. 为了好玩&…

LLMs之PE:AI for Grant Writing的简介、使用方法、案例应用之详细攻略

LLMs之PE&#xff1a;AI for Grant Writing的简介、使用方法、案例应用之详细攻略 目录 AI for Grant Writing的简介 AI for Grant Writing的使用方法—提示资源 1、提示集合 2、提示工程 3、快速提示 为了提高文本清晰度 为了让文本更有吸引力 为了改进文本的结构和流…

QT窗口无法激活弹出问题排查记录

问题背景 问题环境 操作系统: 银河麒麟V10SP1qt版本 : 5.12.12 碰见了一个问题应用最小化,然后激活程序窗口无法弹出 这里描述一下代码的逻辑,使用QLocalServer实现一个单例进程,具体的功能就是在已存在一个程序A进程时,再启动这个程序A,新的程序A进程会被杀死,然后激活已存…

MFC - 复杂控件_1

前言 各位师傅大家好&#xff0c;我是qmx_07&#xff0c;今天给大家讲解复杂控件的相关知识点 复杂控件 进度条 绘图准备: 调整windows窗口大小、设置 Progress Control 进度条设置Button 按钮 添加进度条变量 m_Progress,通过按钮触发 void CMFCApplication2Dlg::OnBnCl…

基于JAVA+SpringBoot+Vue的景区民宿预约系统

基于JAVASpringBootVue的景区民宿预约系统 前言 ✌全网粉丝20W,csdn特邀作者、博客专家、CSDN[新星计划]导师、java领域优质创作者,博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和毕业项目实战✌ &#x1f345;文末附源码下载链接&#x1f345; 哈…

Django 数据库配置以及字段设置详解

配置PostGre 要在 Django 中配置连接 PostgreSQL 数据库&#xff0c;并创建一个包含“使用人”和“车牌号”等字段的 Car 表 1. 配置 PostgreSQL 数据库连接 首先&#xff0c;在 Django 项目的 settings.py 中配置 PostgreSQL 连接。 修改 settings.py 文件&#xff1a; …

C++ set 和 map学习

一、set(multiset)的基本知识和使用 set也是一种我们直接可以使用的容器&#xff0c;使用应该包含 #include <set> 这个头文件。此处暂且不讨论其底层&#xff0c;只探讨set如何使用即可。 我们看到&#xff0c;set 的模板参数有三个&#xff0c;第一个就是其存储的数据…

Python学习——【4.6】数据容器:dict 字典、映射

文章目录 【4.6】数据容器&#xff1a;dict 字典、映射一、字典的定义二、字典的常用操作三、字典的遍历四、字典的特点 【4.6】数据容器&#xff1a;dict 字典、映射 一、字典的定义 为什么使用字典 以生活中的新华字典举例。它的功能是&#xff0c;让我们通过【字】&#x…

Linux基础4-进程2(Linux中的进程状态,R,S,D,T,t,Z,X,僵尸进程,孤儿进程)

上篇文章&#xff1a;Linux基础4-进程1&#xff08;操作系统&#xff0c;进程介绍&#xff0c;Linux进程相关命令&#xff0c;getpid&#xff0c;fork&#xff09;-CSDN博客 本章重点&#xff1a; 进程状态相关知识 目录 1. 进程常见的状态 2.普遍的操作系统理解进程状态 3.…

c++263抽象类在继承中的应用

#include<iostream> using namespace std; //计算程序员工资 //1.要求能计算出junior_programmmer mid-programer adv-programmer的工资 //2.要求利用抽象类统一界面 方便程序的扩展 ex&#xff1a;新增计算架构师architect的工资class programmer { public:virtual void…

CTFHub技能树-SQL注入-Cookie注入

使用bp发现cookie的注入点 id1&#xff0c;发现为数字型 首先使用联合查询 id 1 order by 2 id 1 order by 3发现2的时候有回显&#xff0c;而3的时候无回显 Cookie: id-1 union select database(),user() 后面开始库->表->列->数据 Cookie: id-1 union select 1…

WebLogic文件任意上传漏洞CVE-2018-2894

1.环境搭建 cd vulhub-master/weblogic/CVE-2018-2894 docker-compose up -d 2.获取环境后台密码 docker-compose logs | grep password 3.开启web服务测试 设置web服务测试开启:域结构->base-domain->高级 ->启动Web服务测试页 4.修改访问目录 先进入/ws_utc/co…

Java基础知识扫盲

目录 Arrays.sort的底层实现 BigDecimal(double)和BigDecimal(String)有什么区别 Char可以存储一个汉字吗 Java中的Timer定时调度任务是咋实现的 Java中的序列化机制是咋实现的 Java中的注解是干嘛的 Arrays.sort的底层实现 Arrays.sort是Java中提供的对数组进行排序的…

每日一练:对称二叉树

101. 对称二叉树 - 力扣&#xff08;LeetCode&#xff09; 一、题目要求 给你一个二叉树的根节点 root &#xff0c; 检查它是否轴对称。 示例 1&#xff1a; 输入&#xff1a;root [1,2,2,3,4,4,3] 输出&#xff1a;true示例 2&#xff1a; 输入&#xff1a;root [1,2,2,n…

对象【JavaScript】

在JavaScript中&#xff0c;对象是一种复合数据类型&#xff0c;它使用花括号 {} 包裹一组键值对。每个键&#xff08;属性名&#xff09;后面跟着一个冒号 : 和对应的值。键通常是字符串&#xff08;或符号&#xff09;&#xff0c;而值可以是任意数据类型。 1. 对象字面…

数据脱敏-快速使用

1.数据脱敏定义 数据脱敏百度百科中是这样定义的&#xff1a; 数据脱敏&#xff0c;指对某些敏感信息通过脱敏规则进行数据的变形&#xff0c;实现敏感隐私数据的可靠保护。 因为在真正的生产环境中,很多数据是不能直接返回,但是我们工作的时候可能经常性的需要返回一些用户信…

公司将被千万美金收购,工程师却误删数据库 —— 没 有 备 份!!!

前些天&#xff0c;Retention 和 RB2B 的 CEO&#xff0c;Adam Robinson&#xff0c;在领英&#xff08;LinkedIn&#xff09;发帖讲了一个恐怖故事。 2021 年 3 月&#xff0c;在他第一个创业公司即将以一千万美金被收购的两周前&#xff0c;他们的一位工程师不小心删除了整个…

M9410A VXT PXI 矢量收发信机,300/600/1200MHz带宽

M9410A PXI 矢量收发信机 -300/600/1200MHz带宽- M9410A VXT PXI 矢量收发信机&#xff0c;300/600/1200MHz带宽支持 5G 的 PXI 矢量收发信机&#xff08;VXT&#xff09;是一个 2 插槽模块&#xff0c;具有 1.2 GHz 的瞬时带宽 主要特点 Keysight M9410A VXT PXIe 矢量收发…

SpringBoot+Vue技术框架开发的ADR智能监测系统源码,Java语言的药品不良反应智能监测系统源代码

系统概述&#xff1a; 药品不良反应是指合格药品在正常用法用量下出现的与用药目的无关的有害反应。药品不良反应智能监测系统是一种用于监测和收集药品在使用过程中发生的不良反应的系统。它基于医院临床数据中心&#xff0c;运用信息技术实现药品不良反应的智能监测、报告管…