首頁 > 軟體

springboot 註解方式批次插入資料的實現

2022-03-28 13:01:07

一.使用場景

一次請求需要往資料庫插入多條資料時,可以節省大量時間,mysql操作在連線和斷開時的開銷超過本次操作總開銷的40%。

二.實現方法

1.mysql表結構

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("中國圖書網");
        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.測試結果

三.插入效率對比

1.批次插入

2.一條一條插入

 到此這篇關於springboot 註解方式批次插入資料的文章就介紹到這了,更多相關springboot 註解方式批次插入資料內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com