1. 程式人生 > 實用技巧 >[轉]Springboot介面簡單實現生成MySQL插入語句

[轉]Springboot介面簡單實現生成MySQL插入語句

在實際測試中,有這樣一個需求場景,比如:在效能壓力測試中,可能需要我們事先插入資料庫中一些相關聯的資料。

我們在實際測試中,遇到問題,需要事先在資料庫中建立10000家門店,存在shop表中。關鍵欄位(門店的編號6位數)。

分析:兩種具體實現方案。

一、利用MySQL函式功能隨機生成<SELECT FLOOR(+ RAND() * 1000000)>,最後編寫insert語句。但是效率低下。

二、使用springboot編寫介面實現,並自動生成insert指令碼儲存本地。

本次實現以springboot介面實現此次目的:

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>

  main裡面有預設啟動類。

@RestController
@SpringBootApplication
public class DemoApplication {
   public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
   }

}

編寫介面實現類

@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;
    }

  啟動主程式,呼叫介面