MySQL功能测试-之应用工程

news2024/12/23 11:00:18

MySQL功能测试-之应用工程

  • 前言
  • pom.xml
  • application.yml 文件
  • common.vo 包
    • ResultVO
  • config 包
    • properties 包
      • DruidConfigProperty
      • DruidMonitorProperty
    • AutoFillMetaObjectHandler
    • DruidConfig
    • FluxConfiguration
    • MyBatisPlusConfig
  • controller 包
    • ClientController
    • DruidController
    • WebFluxController
  • entity 包
    • ClientEntity
  • exception 包
    • code 包
      • CRUDStatus
    • BizException
    • GlobalException
  • mapper 包
    • ClientMapper
  • service 包
    • impl 包
    • ClientService
  • DruidApplication
  • resource/mapper 包
    • ClientMapper.xml

前言

应用工程的建设,主要是为了支撑 MySQL 数据库的一些测试场景,例如:

  1. 应用实时写入数据库时,进行修改表结构,查看锁的情况。
  2. 应用实时写入数据库时,数据库增量备份/恢复测试。
  3. 应用实时写入数据库时,数据库主从切换脚本测试。等等…各种场景。

当前工程是在《SpringBoot【2】集成 MyBatis Plus》基础上,进行代码迭代开发的,MySQL 数据库相关测试功能所涉及应用仅此一个,故此应用后续会不断完善,也会同步更新此文档。

目前完成后的项目截图如下:
在这里插入图片描述

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.3.12.RELEASE</version>
        <relativePath />
    </parent>


    <groupId>com.junjiu.springboot.druid</groupId>
    <artifactId>junjiu-springboot-druid</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <mysql.connector.java.version>8.0.28</mysql.connector.java.version>
        <mybatis.plus.boot.starter.version>3.3.1</mybatis.plus.boot.starter.version>
        <druid.version>1.2.13</druid.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-webflux</artifactId>
        </dependency>

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


        <!-- MySQL驱动依赖. -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.connector.java.version}</version>
        </dependency>
        <!-- mybatis-plus 依赖
            https://baomidou.com/getting-started/
        -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version> ${mybatis.plus.boot.starter.version} </version>
        </dependency>

        <!-- Druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>${druid.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <!-- lombok 依赖 -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <!-- 阿里巴巴 fastjson -->
        <dependency>
            <groupId>com.alibaba.fastjson2</groupId>
            <artifactId>fastjson2</artifactId>
            <version>2.0.25</version>
        </dependency>
    </dependencies>

</project>

application.yml 文件

# 端口
server:
  port: 5826

spring:
  application:
    # 应用名称
    name: junjiu-springboot-druid
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://192.168.88.54:3306/ideadb?useUnicode=true&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull&useSSL=false
      username: fid_idea
      password: 123456
      initial-size: 10
      max-active: 100
      min-idle: 10
      max-wait: 60000
      pool-prepared-statements: true
      max-pool-prepared-statement-per-connection-size: 20
      time-between-eviction-runs-millis: 60000
      min-evictable-idle-time-millis: 300000
      max-evictable-idle-time-millis: 600000
      validation-query: SELECT 1 FROM DUAL
      # validation-query-timeout: 5000
      test-on-borrow: false
      test-on-return: false
      test-while-idle: true
      connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
      #filters: #配置多个英文逗号分隔(统计,sql注入,log4j过滤)
      filters: stat,wall
      stat-view-servlet:
        enabled: true
        url-pattern: /druid/*

# 监控页面配置.
jj:
  druid:
    monitor:
      login-username: root
      login-password: 123456
      reset-enable: false

# mybatis plus 配置
mybatis-plus:
  global-config:
    db-config:
      logic-delete-value: 1
      logic-not-delete-value: 0
  mapper-locations: classpath*:/mapper/*Mapper.xml
  type-aliases-package: com.junjiu.springboot.druid.entity

common.vo 包

ResultVO

package com.junjiu.springboot.druid.common.vo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * program: junjiu-springboot-druid
 * ClassName: ResultVO
 * description: 定义统一响应 Vo
 *
 * @author: 君九
 * @create: 2024-06-15 10:11
 * @version: 1.0
 **/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ResultVO implements Serializable {


    private Integer code;

    private String message;

    private Object data;

    /**
     * 状态码
     */
    enum Status {

        SUCCESS(200, "成功"),
        SERVER_ERROR(500, "服务器内部错误");

        private Integer code;
        private String message;

        Status(Integer code, String message) {
            this.code = code;
            this.message = message;
        }

        public Integer getCode() {
            return this.code;
        }

        public String getMessage() {
            return this.message;
        }

    }

    public static ResultVO success(String message) {
        return new ResultVO(Status.SUCCESS.code, message, null);

    }
    public static ResultVO success(String message, Object data) {
        return new ResultVO(Status.SUCCESS.code, message, data);
    }

    public static ResultVO error(String message) {
        return new ResultVO(Status.SERVER_ERROR.code, message, null);
    }

    public static ResultVO error(String message, Object data) {
        return new ResultVO(Status.SERVER_ERROR.code, message, data);
    }

    public static ResultVO error(Integer code, String message, Object data) {
        return new ResultVO(code, message, data);
    }

    public static ResultVO error(Integer code, String message) {
        return new ResultVO(code, message, null);
    }


}

config 包

properties 包

DruidConfigProperty

package com.junjiu.springboot.druid.config.properties;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * program: junjiu-springboot-druid
 * ClassName: DruidConfigProperty
 * description:
 *
 * @author: 君九
 * @create: 2024-05-26 13:19
 * @version: 1.0
 **/
@Getter
@Setter
@Component
@ConfigurationProperties(prefix = "spring.datasource.druid")
public class DruidConfigProperty {

    private String url;
    private String username;
    private String password;
    private String driverClassName;
    private int initialSize;
    private int maxActive;
    private int minIdle;
    private int maxWait;
    private boolean poolPreparedStatements;
    private int maxPoolPreparedStatementPerConnectionSize;
    private int timeBetweenEvictionRunsMillis;
    private int minEvictableIdleTimeMillis;
    private int maxEvictableIdleTimeMillis;
    private String validationQuery;
    private boolean testWhileIdle;
    private boolean testOnBorrow;
    private boolean testOnReturn;
    private String filters;
    private String connectionProperties;

}

DruidMonitorProperty

package com.junjiu.springboot.druid.config.properties;

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * program: junjiu-springboot-druid
 * ClassName: DruidMonitorProperty
 * description:
 *
 * @author: 君九
 * @create: 2024-05-26 13:27
 * @version: 1.0
 **/
@Getter
@Setter
@Component
@ConfigurationProperties(prefix = "jj.druid.monitor")
public class DruidMonitorProperty {

    private String loginUsername;
    private String loginPassword;
    private String resetEnable;

}

AutoFillMetaObjectHandler

package com.junjiu.springboot.druid.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * program: junjiu-springboot-druid
 * ClassName: AutoFillMetaObjectHandler
 * description:
 *
 * @author: 君九
 * @create: 2024-06-12 22:52
 * @version: 1.0
 **/
@Component
public class AutoFillMetaObjectHandler  implements MetaObjectHandler {

    /**
     * 新增数据时,自动填充创建时间+修改时间 为当前时间。
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createdTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "updatedTime", LocalDateTime.class, LocalDateTime.now());
    }

    /**
     * 更新数据时,自动填充创建时间+修改时间 为当前时间。
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "updatedTime", LocalDateTime.class, LocalDateTime.now());
    }
}

DruidConfig

package com.junjiu.springboot.druid.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import com.junjiu.springboot.druid.config.properties.DruidConfigProperty;
import com.junjiu.springboot.druid.config.properties.DruidMonitorProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * program: junjiu-springboot-druid
 * ClassName: DruidConfig
 * description: Druid 配置文件
 *
 * @author: 君九
 * @create: 2024-05-26 13:16
 * @version: 1.0
 **/
@Configuration
public class DruidConfig {

    @Autowired
    DruidConfigProperty druidConfigProperty;

    @Autowired
    DruidMonitorProperty druidMonitorProperty;

    /**
     * Druid 连接池配置
     */
    @Bean
    public DruidDataSource dataSource() {
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl(druidConfigProperty.getUrl());
        datasource.setUsername(druidConfigProperty.getUsername());
        datasource.setPassword(druidConfigProperty.getPassword());
        datasource.setDriverClassName(druidConfigProperty.getDriverClassName());
        datasource.setInitialSize(druidConfigProperty.getInitialSize());
        datasource.setMinIdle(druidConfigProperty.getMinIdle());
        datasource.setMaxActive(druidConfigProperty.getMaxActive());
        datasource.setMaxWait(druidConfigProperty.getMaxWait());
        datasource.setTimeBetweenEvictionRunsMillis(druidConfigProperty.getTimeBetweenEvictionRunsMillis());
        datasource.setMinEvictableIdleTimeMillis(druidConfigProperty.getMinEvictableIdleTimeMillis());
        datasource.setMaxEvictableIdleTimeMillis(druidConfigProperty.getMaxEvictableIdleTimeMillis());
        datasource.setValidationQuery(druidConfigProperty.getValidationQuery());
        datasource.setTestWhileIdle(druidConfigProperty.isTestWhileIdle());
        datasource.setTestOnBorrow(druidConfigProperty.isTestOnBorrow());
        datasource.setTestOnReturn(druidConfigProperty.isTestOnReturn());
        datasource.setPoolPreparedStatements(druidConfigProperty.isPoolPreparedStatements());
        datasource.setMaxPoolPreparedStatementPerConnectionSize(druidConfigProperty.getMaxPoolPreparedStatementPerConnectionSize());
        try {
            datasource.setFilters(druidConfigProperty.getFilters());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        datasource.setConnectionProperties(druidConfigProperty.getConnectionProperties());
        return datasource;
    }

    /**
     * JDBC操作配置
     */
    @Bean
    public JdbcTemplate jdbcTemplate (@Autowired DruidDataSource dataSource){
        return new JdbcTemplate(dataSource) ;
    }

    /**
     * 配置 Druid 监控界面
     */
    @Bean
    public ServletRegistrationBean statViewServlet(){
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
        //设置控制台管理用户
        servletRegistrationBean.addInitParameter("loginUsername",druidMonitorProperty.getLoginUsername());
        servletRegistrationBean.addInitParameter("loginPassword",druidMonitorProperty.getLoginPassword());
        // 是否可以重置数据
        servletRegistrationBean.addInitParameter("resetEnable",druidMonitorProperty.getResetEnable());
        return servletRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean statFilter(){
        //创建过滤器
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
        //设置过滤器过滤路径
        filterRegistrationBean.addUrlPatterns("/*");
        //忽略过滤的形式
        filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }

}

FluxConfiguration

package com.junjiu.springboot.druid.config;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.Bean;
import reactor.core.publisher.DirectProcessor;
import reactor.core.publisher.FluxSink;


/**
 * program: junjiu-springboot-druid
 * ClassName: FluxConfiguration
 * description:
 *
 * @author: 君九
 * @create: 2024-06-15 12:16
 * @version: 1.0
 **/
@Configuration
public class FluxConfiguration {

    @Bean
    public DirectProcessor<String> directProcessor() {
        return DirectProcessor.create();
    }

    @Bean
    public FluxSink<String> fluxSink(DirectProcessor<String> directProcessor) {
        return directProcessor.sink();
    }
}

MyBatisPlusConfig

package com.junjiu.springboot.druid.config;

import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * program: junjiu-springboot-druid
 * ClassName: MyBatisPlusConfig
 * description:
 *
 * @author: 君九
 * @create: 2024-06-12 22:49
 * @version: 1.0
 **/
@Configuration
public class MyBatisPlusConfig {


    @Bean
    public PaginationInterceptor paginationInterceptor() {

        return new PaginationInterceptor();
    }

    /**
     *  自动填充功能
     * @return
     */
    @Bean
    public GlobalConfig globalConfig() {
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setMetaObjectHandler(new AutoFillMetaObjectHandler());
        return globalConfig;
    }

}

controller 包

ClientController

package com.junjiu.springboot.druid.controller;

import com.junjiu.springboot.druid.common.vo.ResultVO;
import com.junjiu.springboot.druid.entity.ClientEntity;
import com.junjiu.springboot.druid.service.ClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.time.Duration;

/**
 * program: junjiu-springboot-druid
 * ClassName: ClientController
 * description:
 *
 * @author: 君九
 * @create: 2024-06-12 22:59
 * @version: 1.0
 **/
@RestController
@RequestMapping("/client")
public class ClientController {

    @Autowired
    private ClientService clientService;

    /**
     * 测试添加数据.
     * @return
     */
    @RequestMapping("/testAdd")
    public String testAdd() {

        ClientEntity clientEntity = new ClientEntity();
        clientEntity.setClientId(1001L);
        clientEntity.setNickName("九皇叔叔");
        clientEntity.setRealName("君九");
        clientEntity.setUserNo("JunJiu");
        clientEntity.setUserPassword("123456");

        clientService.save(clientEntity);

        return "Success.";
    }


    /**
     * 我当前做测试,传入 loop、threadSize 一定有值,不在校验了
     *
     * @param loop 是否循环
     * @param threadSize 线程个数.
     * @return
     */
    @GetMapping("/setAddRandomData/{loop}/{threadSize}")
    public ResultVO setAddRandomData(
            @PathVariable("loop") Boolean loop,
            @PathVariable("threadSize") Integer threadSize
    ) {


        clientService.setAddRandomData(loop, threadSize);

        return ResultVO.success("操作成功.loop=" + loop);
    }

}

DruidController

package com.junjiu.springboot.druid.controller;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * program: junjiu-springboot-druid
 * ClassName: DruidController
 * description:
 *
 * @author: 君九
 * @create: 2024-05-26 13:32
 * @version: 1.0
 **/
@RestController
public class DruidController {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private DruidDataSource druidDataSource;

    /**
     * 测试 Druid
     * @return
     */
    @GetMapping("/test")
    public String testDruid() {

        String sql = "select version()";
        String result = jdbcTemplate.queryForObject(sql, String.class);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        return "Success.".concat(simpleDateFormat.format(new Date())).concat(":").concat(result);
    }

    /**
     * 测试连接池.
     *  查看 performance_schema.threads 情况.
     * @param size
     * @return
     */
    @GetMapping("/mutix/{size}")
    public String testDruidMutix(@PathVariable("size") Integer size) {
        for(int k = 0; k < size; k++) {
            new Thread(() -> {
                String sql = "select '你也可以通过操作系统级别的资源限制来控制 MySQL 进程的内存使用情况。具体的操作方式会根据操作系统的不同而有所不同,比如在 Linux 中可以使用 ulimit 命令来设置进程的资源限制。'";
                String result = jdbcTemplate.queryForObject(sql, String.class);
                System.out.println(Thread.currentThread().getName() + ":" + result);
            }).start();
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        return "Success.".concat(simpleDateFormat.format(new Date()));
    }
}

WebFluxController

package com.junjiu.springboot.druid.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.time.Duration;

/**
 * program: junjiu-springboot-druid
 * ClassName: WebFluxController
 * description:
 *
 * @author: 君九
 * @create: 2024-06-15 11:26
 * @version: 1.0
 **/
@RestController
public class WebFluxController {

    @GetMapping("/data")
    public Flux<String> streamData() {
        // 模拟生成实时数据流
        return Flux.just("Data 1", "Data 2", "Data 3", "Data 4").delayElements(Duration.ofSeconds(1));
    }
}

entity 包

ClientEntity

package com.junjiu.springboot.druid.entity;

import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;

/**
 * program: junjiu-springboot-druid
 * ClassName: ClientEntity
 * description:
 *
 * @author: 君九
 * @create: 2024-06-12 22:55
 * @version: 1.0
 **/
@Data
@TableName("client")
public class ClientEntity implements Serializable {

    /**
     * -- 1 -* 客户表
     * create table client(
     *    id bigint not null primary key auto_increment comment '自增ID',
     *    client_id bigint not null comment 'ID编号',
     *    user_no varchar(30) comment '账号',
     *    user_password varchar(60) comment '密码',
     *    nick_name varchar(30) comment '昵称',
     *    real_name varchar(30) comment '真实姓名',
     *    created_time datetime default now() comment '创建时间',
     *    upated_time datetime default now() comment '更新时间'
     * );
     */

    private Long id;
    private Long clientId;
    private String userNo;
    private String userPassword;
    private String nickName;
    private String realName;
    private Date createdTime;
    private Date updatedTime;

}

exception 包

code 包

CRUDStatus

package com.junjiu.springboot.druid.exception.code;

/**
 * program: junjiu-springboot-druid
 * ClassName: CRUDStatus
 * description: CRUD操作通用状态码.
 *
 * @author: 君九
 * @create: 2024-06-15 10:09
 * @version: 1.0
 **/
public enum CRUDStatus {

    // 添加成功
    SUCCESS_INSERT(100001, "添加成功"),
    // 添加失败
    FAIL_INSERT(100002, "添加失败"),
    // 修改成功
    SUCCESS_UPDATE(100003, "修改成功"),
    // 修改失败
    FAIL_UPDATE(100004, "修改失败"),
    // 删除成功
    SUCCESS_DELETE(100005, "删除成功"),
    // 删除失败
    FAIL_DELETE(100006, "删除失败"),
    // 未查询到数据
    NOT_FOUND(100007, "未查询到数据"),
    // 非法数据信息
    ILLEGAL_ARGUMENT(100008, "非法数据信息");

    private Integer code;
    private String message;

    CRUDStatus(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public Integer getCode() {
        return this.code;
    }

    public String getMessage() {
        return this.message;
    }

}

BizException

package com.junjiu.springboot.druid.exception;

import lombok.Data;

/**
 * program: junjiu-springboot-druid
 * ClassName: BizException
 * description: 定义业务类统一异常类
 *
 * @author: 君九
 * @create: 2024-06-15 10:08
 * @version: 1.0
 **/
@Data
public class BizException extends RuntimeException {

    private Integer code;
    private String message;

    public BizException(String message) {
        super(message);
        this.message = message;
    }

    public BizException(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public BizException(String message, Exception exception) {
        super(message, exception);
        this.message = message;
    }

    public BizException(Integer code, String message, Exception exception) {
        super(message, exception);
        this.code = code;
        this.message = message;
    }
}

GlobalException

package com.junjiu.springboot.druid.exception;

/**
 * program: junjiu-springboot-druid
 * ClassName: GlobalException
 * description:
 *
 * @author: 君九
 * @create: 2024-06-15 10:08
 * @version: 1.0
 **/
public class GlobalException {
}

mapper 包

ClientMapper

package com.junjiu.springboot.druid.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.junjiu.springboot.druid.entity.ClientEntity;
import org.apache.ibatis.annotations.Mapper;

/**
 * program: junjiu-springboot-druid
 * ClassName: ClientMapper.xml
 * description:
 *
 * @author: 君九
 * @create: 2024-06-12 22:57
 * @version: 1.0
 **/
@Mapper
public interface ClientMapper extends BaseMapper<ClientEntity> {
}

service 包

impl 包

package com.junjiu.springboot.druid.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.junjiu.springboot.druid.entity.ClientEntity;
import com.junjiu.springboot.druid.exception.BizException;
import com.junjiu.springboot.druid.exception.code.CRUDStatus;
import com.junjiu.springboot.druid.mapper.ClientMapper;
import com.junjiu.springboot.druid.service.ClientService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * program: junjiu-springboot-druid
 * ClassName: ClientServiceImpl
 * description:
 *  客户业务实现类.
 *
 * @author: 君九
 * @create: 2024-06-12 22:58
 * @version: 1.0
 **/
@Service
public class ClientServiceImpl extends ServiceImpl<ClientMapper, ClientEntity> implements ClientService {

    private ThreadLocal<Thread> threadLocal = new ThreadLocal<>(); // ThreadLocal 存储每个线程
    private volatile boolean loop = true; // 用于控制线程循环的共享变量




    @Override
    @Transactional(rollbackFor = Exception.class)
    public void setAddRandomData(Boolean loop, Integer threadSize) {
        this.loop = loop; // 设置共享变量的初始值
        CompletableFuture<?>[] futures = new CompletableFuture[threadSize];

        if(loop) {
            for (int i = 0; i < threadSize; i++) {
                final int threadNumber = i + 1;
                CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
                    while (this.loop) {

                        // 创建随机的 ClientEntity 对象
                        ClientEntity client = new ClientEntity();
                        client.setClientId(Thread.currentThread().getId()); // 设置随机的 clientId
                        client.setUserNo(Thread.currentThread().getName()); // 将 userNo 设置为当前线程名称
                        client.setUserPassword(generateRandomString(16)); // 生成随机密码
                        client.setNickName("NickName_" + Thread.currentThread().getId()); // 设置昵称
                        client.setRealName("RealName_" + Thread.currentThread().getId()); // 设置真实姓名

                        try {
                            // 将生成的 ClientEntity 插入数据库
                            baseMapper.insert(client);
                        } catch (Exception exception) {
                            exception.printStackTrace();
                            throw new BizException(CRUDStatus.FAIL_DELETE.getCode(), CRUDStatus.FAIL_INSERT.getMessage(), exception);
                        }

                        Random random = new Random();
                        int randomData = random.nextInt(1000);
                        try {
                            Thread.sleep(randomData);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                });

                futures[i] = future.exceptionally(ex -> {
                    // 异常处理
                    if (ex.getCause() instanceof BizException) {
                        throw (BizException) ex.getCause(); // 将 BizException 抛给父线程
                    } else {
                        // 其他异常,可以选择处理或包装成自定义异常
                        throw new RuntimeException("Unexpected error occurred in thread " + threadNumber, ex);
                    }
                });

                threadLocal.set(Thread.currentThread());
            }

            try {
                CompletableFuture.allOf(futures).get(); // 等待所有 CompletableFuture 完成
            } catch (InterruptedException | ExecutionException e) {
                // 处理异常
                // e.printStackTrace();
                System.out.println(e.getMessage());
            }

        } else {
            this.loop = false; // 设置 loop 为 false,停止所有线程的循环
        }
    }

    /**
     * 生成指定长度的随机字符串(仅用于示例,实际应用中需要更复杂的实现)
     * @param length 字符串长度
     * @return 随机字符串
     */
    private String generateRandomString(int length) {
        String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(characters.length());
            sb.append(characters.charAt(index));
        }
        return sb.toString();
    }

}

ClientService

package com.junjiu.springboot.druid.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.junjiu.springboot.druid.entity.ClientEntity;

/**
 * program: junjiu-springboot-druid
 * ClassName: ClientService
 * description:
 *  客户端业务接口.
 *
 * @author: 君九
 * @create: 2024-06-12 22:57
 * @version: 1.0
 **/
public interface ClientService extends IService<ClientEntity> {

    /**
     * 随机添加数据,用于模拟:应用实时添加数据情况,对 MySQL 数据库进行备份恢复、调整字段长度等场景。
     *
     * --- 目标:
     * 1.根据传入的线程个数,开启多线程。
     * 2.产生随机等待时长,使线程等待后,在执行添加数据操作。
     * 3.可手动开启/暂停,数据入库。
     *
     * @param loop 循环.
     * @param threadSize 线程个数.
     *
     */
    void setAddRandomData(Boolean loop, Integer threadSize);


}

DruidApplication

package com.junjiu.springboot.druid;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * program: junjiu-springboot-druid
 * ClassName: DruidApplication
 * description:
 *
 * @author: 君九
 * @create: 2024-05-26 13:13
 * @version: 1.0
 **/
@SpringBootApplication
public class DruidApplication {
    public static void main(String[] args) {

        SpringApplication.run(DruidApplication.class);
    }
}

resource/mapper 包

ClientMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.junjiu.springboot.druid.mapper.ClientMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.junjiu.springboot.druid.entity.ClientEntity">
        <id column="id" property="id" />
        <result column="client_id" property="clientId" />
        <result column="user_no" property="userNo" />
        <result column="user_password" property="userPassword" />
        <result column="nick_name" property="nickName" />
        <result column="real_name" property="realName" />
        <result column="created_time" property="createdTime" />
        <result column="updated_time" property="updatedTime" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, client_id, user_no, user_password, nick_name, real_name, created_time, updated_time
    </sql>

</mapper>

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

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

相关文章

Python开发日记--手撸加解密小工具(2)

目录 1. UI设计和代码生成 2.运行代码查看效果 3.小结 1. UI设计和代码生成 昨天讨论到每一类算法设计为一个Tab&#xff0c;利用的是TabWidget&#xff0c;那么接下来就要在每个Tab里设计算法必要的参数了&#xff0c;这里我们会用到组件有Label、PushButton、TextEdit、Ra…

RSA 加密算法的基础数论、基本原理与 Python 实现

Title: RSA 加密算法的基础数论、基本原理与 Python 实现 文章目录 前言I. 数学原理1. 整数环2. 单位元3. 欧拉定理 II. 算法原理1. 扩展欧几里得算法2. RSA 非对称加密算法 III. 算法实现1. 源代码2. 测试结果 总结参考文献 前言 1977 年美国 MIT 的三位数学家 Ronald L. Riv…

gunicorn超时报错[CRITICAL] WORKER TIMEOUT

一. 问题描述 2024-06-18T08:40:39.858804039Z [2024-06-18 08:40:39 0000] [1] [CRITICAL] WORKER TIMEOUT (pid:332) 2024-06-18T08:40:40.918093090Z [2024-06-18 08:40:40 0000] [1] [ERROR] Worker (pid:332) was sent SIGKILL! Perhaps out of memory?二. 原因分析 从…

企业如何做好供应链管理工作?8个步骤及应用详解!

供应链就是采购把东西买进来&#xff0c;生产去加工增值&#xff0c;物流去配送给客户&#xff0c;环环相扣&#xff0c;就形成了供应链。它是将供应商&#xff0c;制造商&#xff0c;分销商直到最终用户连成一个整体的功能网链结构。 而供应链管理就是做好每个环节的管理&…

前沿重器[50] | 聊聊搜索系统3:文档内容处理

前沿重器 栏目主要给大家分享各种大厂、顶会的论文和分享&#xff0c;从中抽取关键精华的部分和大家分享&#xff0c;和大家一起把握前沿技术。具体介绍&#xff1a;仓颉专项&#xff1a;飞机大炮我都会&#xff0c;利器心法我还有。&#xff08;算起来&#xff0c;专项启动已经…

【Maven】项目的Maven插件报错

1. 找到本地maven库 2. 删除本地插件 3. 在IDEA上更新pom.xml

【简易版tinySTL】 deque容器

文章目录 基本概念功能思路数据结构循环数组实现 代码实现deque.htest.cpp 代码详解变量push_frontpush_backpop_front、pop_backoperator[]clearprintElementsresize 本实现版本 和 C STL标准库实现版本的区别&#xff1a; 基本概念 功能&#xff1a; 双端数组&#xff0c;可…

300PLC连接Modbus转Profibus网关与阀岛modbusRTU通讯

一、概况 300PLC作为常见的控制器设备&#xff0c;在与阀岛Modbus RTU通讯时&#xff0c;通常需要借助Modbus转Profibus网关&#xff08;XD-MDPB100&#xff09;来实现连接和数据交换。PLC通过Modbus转Profibus网关&#xff08;XD-MDPB100&#xff09;与阀岛Modbus RTU通讯是比…

无约束动态矩阵控制(DMC)

0、前言 动态矩阵控制&#xff08;Dynamic Matrix Control&#xff0c;DMC&#xff09;是一种典型的模型预测控制方法&#xff0c;其不需要被控对象的数学模型&#xff0c;只需要获取被控对象的阶跃响应序列即可实现控制效果&#xff0c;但其需要被控对象是渐近稳定的。 1、稳…

PTA - 编写函数计算薪资

某公司销售员底薪为5000&#xff0c;销售业绩与利润提成的关系如下表所示&#xff08;计量单位&#xff1a;元&#xff09; 编写函数&#xff0c;计算员工月薪。 函数接口定义&#xff1a; salary(sales) 其中 参数 sales表示员工的月销售业绩。 裁判测试程序样例&#xff…

Ranger配置图片及json文件预览

文章目录 前言下载apt下载pip下载 配置使用json文件预览方法一 修改scope用cat预览方法二 安装jq预览配置ranger 图片文件预览方法一 使用img2txt预览方法二 使用fim预览配置ranger 总结 前言 本文主要讲解Ranger12如何配置json及图片的预览设置&#xff0c;如下是ranger的介绍…

RX数据集成:信创生态下的平滑过渡方案

过去&#xff0c;众多中国企业倾向于采用国际供应商的数据集成产品与方案。其中Informatica作为行业翘楚&#xff0c;以其卓越性能和技术领先地位赢得了全球500强中95%企业的青睐。在中国市场上&#xff0c;众多企业同样信赖并采纳其解决方案。然而&#xff0c;随着国际环境的演…

数学建模基础:线性模型

目录 前言 一、线性方程组 二、线性规划 三、线性回归 四、线性模型的应用 五、实例示范&#xff1a;医疗成本预测 步骤 1&#xff1a;导入数据 步骤 2&#xff1a;数据预处理 步骤 3&#xff1a;建立多元线性回归模型 步骤 4&#xff1a;模型验证 步骤 5&#xff1…

C++多重继承,虚基类与友元

一.多重继承 就是一个类继承多个基类&#xff1b; class <派生类名>&#xff1a;<派生方式1><基类名1>,<派生方式n><基类名n> class Derived:public:Base1,public:Base2 上述形式&#xff1a;基类之间由逗号隔开&#xff0c;且必须指明继承方式…

Redis-数据类型-String

文章目录 1、通过客户端连接redis2、查看当前数据库的key的数量3、切换数据库3.1、切换到第一个数据库3.2、切换到第二个数据库3.3、切换到默认的数据库&#xff0c;第0个数据库 4、当前数据库没有数据5、添加键值对6、查看当前库所有key7、清空当前库8、设置存活的秒数&#x…

定个小目标之刷LeetCode热题(25)

这道题采用的解法是桶排序&#xff0c;画草图如下 代码如下 //基于桶排序求解「前 K 个高频元素」 class Solution {public int[] topKFrequent(int[] nums, int k) {HashMap<Integer, Integer> map new HashMap();for (int num : nums) {if (map.containsKey(num)) {m…

python自动化系列:自动复制一个工作簿的所有工作表到其他工作簿

作品介绍 作品名称&#xff1a;自动复制一个工作簿的所有工作表到其他工作簿 开发环境&#xff1a;PyCharm 2023.3.4 python3.7 用到的库&#xff1a;os、xlwings 作品效果&#xff1a; 实现过程 一、代码设计 以下是代码的详细说明&#xff1a; 导入模块&#xff1a; …

物联网主机E6000:动环监控的全新解决方案!

物联网主机E6000在动环监控中的应用&#xff0c;标志着一场新的技术革命。随着科技的进步&#xff0c;特别是在物联网领域&#xff0c;数据采集和处理已经成为企业运营不可或缺的一环。 E6000作为一款支持多协议、多接口的全能型物联网主机&#xff0c;其在动环监控领域的应用…

详解Spring AOP(二)

目录 1.切点表达式 1.1execution表达式 1.2 annotation 1.2.1自定义注解MyAspect 1.2.3添加自定义注解 2.Sping AOP原理 2.1代理模式 2.1.1静态代理 2.1.2动态代理 2.1.3JDK动态代理 2.1.4CGLIB动态代理 3.总结 承接上文&#xff1a;详解Spring AOP&#xff08;一&…

【GO】rotatelogs库和sirupsen/logrus库实现日志功能的实践用例

“github.com/sirupsen/logrus” 是一个 Go 语言的日志库&#xff0c;它提供了一种简单、灵活的方式来记录日志。该库的主要特点包括&#xff1a; 支持多种日志输出目标&#xff0c;如控制台、文件等。 支持日志轮转&#xff0c;可以按照时间或文件大小进行轮转。 支持日志格式…