欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot?注解方式批量插入數(shù)據(jù)的實現(xiàn)

 更新時間:2022年03月28日 11:19:26   作者:什么都干的派森  
一次請求需要往數(shù)據(jù)庫插入多條數(shù)據(jù)時,可以節(jié)省大量時間,本文主要介紹了springboot?注解方式批量插入數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

一.使用場景

一次請求需要往數(shù)據(jù)庫插入多條數(shù)據(jù)時,可以節(jié)省大量時間,mysql操作在連接和斷開時的開銷超過本次操作總開銷的40%。

二.實現(xiàn)方法

1.mysql表結(jié)構(gòu)

在這里插入圖片描述

2.domain

在這里插入圖片描述

package com.cxstar.order.domain;

import java.util.Date;

@lombok.Data
public class Data {
    private int id;
    private String ruid;
    private String title;
    private String author;
    private String coverUrl;
    private String detialUrl;
    private String code;
    private int type;
    private String publisher;
    private int groupId;
    private String groupName;
    private int pageNo;
    private String searchKey;
    private Date createTime;
}

3.mapper

在這里插入圖片描述

package com.cxstar.order.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cxstar.order.domain.Data;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Mapper
public interface DataMapper extends BaseMapper<Data> {

    @InsertProvider(type = Provider.class, method = "batchInsert")
    int batchInsert(List<Data> data);

    class Provider {
        /* 批量插入 */
        public String batchInsert(Map map) {
            List<Data> data = (List<Data>) map.get("list");
            StringBuilder sb = new StringBuilder();
            sb.append("INSERT INTO data (" +
                        "ruid," +
                        "title," +
                        "author," +
                        "code," +
                        "type," +
                        "publisher," +
                        "group_id," +
                        "group_name," +
                        "page_no," +
                        "search_key," +
                        "create_time," +
                        "cover_url," +
                        "detial_url" +
                    ") VALUES ");
            MessageFormat mf = new MessageFormat(
                    "(" +
                        "#'{'list[{0}].ruid}, " +
                        "#'{'list[{0}].title}, " +
                        "#'{'list[{0}].author}, " +
                        "#'{'list[{0}].code}, " +
                        "#'{'list[{0}].type}, " +
                        "#'{'list[{0}].publisher}, " +
                        "#'{'list[{0}].groupId}, " +
                        "#'{'list[{0}].groupName}, " +
                        "#'{'list[{0}].pageNo}, " +
                        "#'{'list[{0}].searchKey}, " +
                        "#'{'list[{0}].createTime}, " +
                        "#'{'list[{0}].coverUrl}, " +
                        "#'{'list[{0}].detialUrl}" +
                    ")"
            );

            for (int i = 0; i < data.size(); i++) {
                sb.append(mf.format(new Object[] {i}));
                if (i < data.size() - 1)
                    sb.append(",");
            }
            return sb.toString();
        }
        
    }

}

4.測試類

在這里插入圖片描述

package com.cxstar.order;

import com.alibaba.fastjson.JSONObject;
import com.cxstar.order.domain.Data;
import com.cxstar.order.mapper.DataMapper;
//import com.cxstar.order.service.OrderService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.Date;
import java.util.UUID;

@SpringBootTest
class OrderApplicationTests {

    @Autowired
    DataMapper dataMapper;

    @Test
    void data_batch_insert() {

        ArrayList<Data> batchInsertList = new ArrayList<>();

        Data data = new Data();
        data.setTitle("歷史上的今天");
        data.setAuthor("郭漫");
        data.setCoverUrl("http://image31.bookschina.com/2011/20110520/s5143135.jpg");
        data.setDetialUrl("http://www.bookschina.com/5143135.htm");
        data.setGroupId(1);
        data.setGroupName("中國圖書網(wǎng)");
        data.setRuid(UUID.randomUUID().toString().replaceAll("-",""));
        data.setType(1);
        data.setCode(null);
        data.setPublisher(null);
        data.setPageNo(1);
        data.setSearchKey("歷史上的今天");
        data.setCreateTime(new Date());

        batchInsertList.add(data);

        System.out.println(dataMapper.batchInsert(batchInsertList));
    }

}

5.測試結(jié)果

在這里插入圖片描述

在這里插入圖片描述

三.插入效率對比

1.批量插入

在這里插入圖片描述

2.一條一條插入

在這里插入圖片描述

 到此這篇關(guān)于springboot 注解方式批量插入數(shù)據(jù)的文章就介紹到這了,更多相關(guān)springboot 注解方式批量插入數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java并發(fā)編程工具類JUC之ArrayBlockingQueue

    java并發(fā)編程工具類JUC之ArrayBlockingQueue

    類ArrayBlockingQueue是BlockingQueue接口的實現(xiàn)類,它是有界的阻塞隊列,內(nèi)部使用數(shù)組存儲隊列元素,通過代碼給大家說明如何初始化一個ArrayBlockingQueue,并向其中添加一個對象,對java并發(fā)編程工具類ArrayBlockingQueue相關(guān)知識感興趣的朋友一起看看吧
    2021-05-05
  • 簡單談?wù)凧ava 中的線程的幾種狀態(tài)

    簡單談?wù)凧ava 中的線程的幾種狀態(tài)

    這篇文章主要介紹了簡單談?wù)凧ava 中的線程的幾種狀態(tài)的相關(guān)資料,需要的朋友可以參考下
    2020-02-02
  • RabbitMQ消息拒絕如何解決

    RabbitMQ消息拒絕如何解決

    這篇文章主要介紹了RabbitMQ消息拒絕如何解決問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • Java ArrayList與Vector和LinkedList的使用及源碼分析

    Java ArrayList與Vector和LinkedList的使用及源碼分析

    ArrayList、Vector、LinkedList類均在java.util包中,均為可伸縮數(shù)組,即可以動態(tài)改變長度的數(shù)組。ArrayList 和 Vector都是基于存儲元素的Object[] array來實現(xiàn)的,它們會在內(nèi)存中開辟一塊連續(xù)的內(nèi)存來存儲
    2022-11-11
  • Java Swing實現(xiàn)窗體添加背景圖片的2種方法詳解

    Java Swing實現(xiàn)窗體添加背景圖片的2種方法詳解

    這篇文章主要介紹了Java Swing實現(xiàn)窗體添加背景圖片的2種方法,結(jié)合實例形式較為詳細(xì)的分析了Swing實現(xiàn)窗體添加背景圖片的方法,并總結(jié)分析了Swing重繪中repaint與updateUI的區(qū)別,需要的朋友可以參考下
    2017-11-11
  • 使用IntelliJ IDEA搭建SSM框架的圖文教程

    使用IntelliJ IDEA搭建SSM框架的圖文教程

    本文通過圖文并茂的形式給大家介紹了使用IntelliJ IDEA搭建SSM框架的教程,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-05-05
  • Jenkins自動構(gòu)建部署項目到遠程服務(wù)器上的方法步驟

    Jenkins自動構(gòu)建部署項目到遠程服務(wù)器上的方法步驟

    這篇文章主要介紹了Jenkins自動構(gòu)建部署項目到遠程服務(wù)器上的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • Java中的CAS和ABA問題說明

    Java中的CAS和ABA問題說明

    這篇文章主要介紹了Java中的CAS和ABA問題說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • 教你怎么用Java完成人民幣大寫轉(zhuǎn)化

    教你怎么用Java完成人民幣大寫轉(zhuǎn)化

    這篇文章主要介紹了教你怎么用Java完成人民幣大寫轉(zhuǎn)化,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)java的小伙伴們有很好的幫助,需要的朋友可以參考下
    2021-04-04
  • IDEA+Maven創(chuàng)建Spring項目的實現(xiàn)步驟

    IDEA+Maven創(chuàng)建Spring項目的實現(xiàn)步驟

    這篇文章主要介紹了IDEA+Maven創(chuàng)建Spring項目的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07

最新評論