Springboot接口简单实现调用接口生成MySQL插入语句

在实际测试中,有这样一个需求场景,比如:在性能压力测试中,可能需要我们事先插入数据库中一些相关联的数据。

我们在实际测试中,遇到问题,需要事先在数据库中创建10000家门店,存在shop表中。关键字段(门店的编号6位数)。

分析:两种具体实现方案。

一、利用MySQL函数功能随机生成<SELECT FLOOR(+ RAND() * 1000000)>,最后编写insert语句。但是效率低下。

二、使用springboot编写接口实现,并自动生成insert脚本保存本地。

本次实现以springboot接口实现此次目的:

  • 开发环境

      • 开发工具IDEA

      • JDK 1.8

  • 新建springboot项目

1、新建project

2、填写springbootDemo

直到填写finish完成。

​    ​3、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>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springbootDemo</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</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.16.14</version>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

4、打开工程,看到main里面有默认启动类。

@RestController
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run
(DemoApplication.class, args);
} }
  • 编写接口实现类

    * 分析:
    * inpuid: uuid
    * sotreId=commonCode 以8开头后面五位随机
    * createDatetime:当前时间
    * create_by: admin
    * is_recharge_consumption:1
    * updateDatetime:当前时间
    * update_by:admin
    * sync_date:null
    * channel_key:123456
    * chainName: XX
    * state:0
    * shopCode 以8开头后面五位随机
    @RequestMapping("/generateSql")
public static String insert(@RequestParam(required = false) int size) throws IOException {
// 开时时间
Long begin = new Date().getTime();
log.info("begin:{}",begin);
for (int i = 0; i <size ; i++) {
StringBuffer addSql = new StringBuffer();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
addSql.append("INSERT INTO"
+ " t_shop"
+ "(`inputId`, `storeId`, `storeType`, `initialsName`, `storeName`, `ipAddress`, `portEP`, `ipAddressEP`, `fixedTelephone`, `mobile`, `contactPerson`, `email`, `address`, `registrationNo`, `createDatetime`, `create_by`, `is_recharge_consumption`, `updateDatetime`, `update_by`, `sync_date`, `channel_key`, `chainName`, `state`, `commonCode`, `areaCode`, `registNumberEP`, `softName`, `busiNature`, `brand`, `floor`, `usageArea`, `computerNum`, `profit_rate`)"); addSql.append("values" + "(");
addSql.append("'"+UUID.randomUUID().toString().replace("-", "") + "',");
//生成commoncode storeid
String newStoreId;
double a = (Math.random()+1)*1000;
int sotreId = Integer.parseInt(String.valueOf(a).substring(0,4));
newStoreId = "'8" + String.valueOf(sotreId) + "'";
addSql.append(newStoreId + ",");
// String empty = "".join("'", Collections.nCopies(12,"',"));
String empty = "'',";
addSql.append(empty);
addSql.append(empty);
addSql.append(newStoreId + ",");
addSql.append(empty);
addSql.append(empty);
addSql.append(empty);
addSql.append(empty);
addSql.append(empty);
addSql.append(empty);
addSql.append(empty);
addSql.append(empty);
addSql.append(empty);
Date date = new Date();
SimpleDateFormat sdf1 = new
SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String createTime = sdf1.format(date);
addSql.append("'"+createTime + "',");
addSql.append("'admin',");
addSql.append("'1',");
addSql.append("'"+createTime + "',");
addSql.append("'admin',");
addSql.append(NULL + ",");
addSql.append("'123456',");
addSql.append("'XX',");
addSql.append("'0',");
addSql.append(newStoreId + ",");
addSql.append(empty);
addSql.append("'201805093512',");
addSql.append(empty);
addSql.append(empty);
addSql.append(empty);
addSql.append(empty);
addSql.append(NULL + ",");
addSql.append(NULL + ",");
addSql.append(NULL);
addSql.append(");");
String path = "D:\\sqlYpay.txt";
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path,true)));
out.write(addSql+"\r\n");
System.out.println(addSql);
out.close(); }
// System.out.println(addSql);
// 结束时间
Long end = new Date().getTime();
log.info("end:{}",end);
log.info("耗时:{}",end-begin);
// 耗时
System.out.println("cast : " + (end - begin) / 1000 + " s");
return "生成SQL执行语句成功,一共生成了:>>>>>>" + size;
}
  • 启动主程序,调用接口

​SQL语句生成执行成功,查看文件夹生成文件。执行即可。

Springboot接口简单实现生成MySQL插入语句的更多相关文章

  1. 快速将一个表的数据生成SQL插入语句

    将一个表中的数据生成SQL插入语句,方便系统快速初始化,在数据库中执行创建以下过程就可以了. ) Drop Procedure GenerateData go CREATE PROCEDURE Gen ...

  2. mysql 插入语句

    mysql 插入语句 什么时候用单引号,什么时候不用? 1.先创建一个表 create table user(username varchar(255),age int,marry boolean,b ...

  3. 生成大量插入语句,并将语句写入txt文件中

    import java.io.*; /** * Created by czz on 2019/9/23. */ public class TTest { /** * 生成大量插入语句,并将语句写入tx ...

  4. 比较两个文件不同以及生成SQL插入语句

    Tips 做一个终身学习的人! 日拱一卒,功不唐捐. 今天有个小小的需求,具体需求是这样的: 有两个文本文件,每个文件都有一些字符串文本: 比较第一个文件中,在第二个文件中,不存在的字符串文本: 把这 ...

  5. MySQL插入语句解析

    1.INSERT INTO 最常用简单的插入语句,可以有以下两种用法 1>  INSERT INTO tb_user(id, name, age) VALUES (100022, 'Tom', ...

  6. Python 生成MYSQL inser语句

    背景: 一般来说,navicat生成的insert已经够用了 如果说一张表有2,30个字段,每个字段还得一一对上,其实是很难的.所以抽空写了个小程序.用它完全不用担心字段对不上了.因为没有时间,需要手 ...

  7. Mysql插入语句.txt

    INSERT INTO 目标表 SELECT * FROM 来源表;比如要将 articles 表插入到 newArticles 表中,则是:INSERT INTO newArticles SELEC ...

  8. Android之网络编程利用PHP操作MySql插入数据(四)

    因为最近在更新我的项目,就想着把自己在项目中用到的一些的简单的与网络交互的方法总结一下,所以最近Android网络编程方面的博文会比较多一些,我尽量以最简单的方法给大家分享,让大家明白易懂.如果有什么 ...

  9. 简单的多表插入(oracle)

    简单的多表插入语句: insert all into 表1(字段1,2...) values(值1,值2......) into 表2(字段1,2...)) values(值1,值2......) s ...

随机推荐

  1. UNITY中有Timer

    using UnityEngine; using System.Collections; using System.Timers; public class NewBehaviourScript : ...

  2. jquery中的trigger()和preventDefault()方法

    trigger()方法用户模拟用户操作,比较常见的一种情况就是输入框自动获得焦点: <!doctype html> <html> <head> <meta c ...

  3. 单词搜索 II · Word Search II

    [抄题]: 给出一个由小写字母组成的矩阵和一个字典.找出所有同时在字典和矩阵中出现的单词.一个单词可以从矩阵中的任意位置开始,可以向左/右/上/下四个相邻方向移动. 给出矩阵: doafagaidca ...

  4. [leetcode]206. Reverse Linked List反转链表

    Reverse a singly linked list. Input: 1->2->3->4->5->NULL Output: 5->4->3->2- ...

  5. 9-eclispe中右键BuildPath没有了

    Eclipse 右上角的代码视图,选择Java就好了!

  6. C语言时间处理

    一.简介 时间处理在编程中经常遇到,包括程序的运行时间和显示时间等.在标准C中, 日期和时间的处理包含在 time.h 的头文件中,需要使用日期和时间相关的类型的函数的话, 需要导入time.h. 二 ...

  7. windows下使用redis

    一.下载windows版本的Redis 官网只提供linux版本的下载 官网下载地址:http://redis.io/download Redis 没有官方的Windows版本,但是微软开源技术团队( ...

  8. Ckeditor 中粘贴图片

    我们在ckeditor 中有上传图片,但是实际使用中这种手动上传图片方式并不是很方便,而是复制或者截图粘贴图片. 这里我们实现主要是获取对应的粘贴事件. CKEDITOR.instances[&quo ...

  9. myeclipse 快捷键,从步骤开始的大括号定位到匹配方法结束的大括号

    myeclipse 快捷键,从方法开始的大括号定位到匹配方法结束的大括号转至匹配的括号 Ctrl+Shift+P ctr+shift+r   文件名搜索文件 ctr+h           搜索文件里 ...

  10. PHP 微信公众号开发 - 消息推送

    项目微信公众号开发,需要做用户消息推送,记录下来以便日后使用 1,接上一篇文章,可以查看如何获取用户openid PHP 微信公众号开发 - 获取用户信息 2,添加模板消息 3,查看模板详情 根据模板 ...