springboot 注解方式批量插入数据的实现
作者:什么都干的派森 时间:2022-02-20 19:03:50
一.使用场景
一次请求需要往数据库插入多条数据时,可以节省大量时间,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.一条一条插入
来源:https://blog.csdn.net/weixin_43721000/article/details/123751975
标签:springboot,注解,批量插入
0
投稿
猜你喜欢
Android客户端程序Gradle如何打包
2023-06-05 14:10:07
c# list部分操作实现代码
2021-09-25 03:43:19
Java注释代码执行方法解析
2023-09-28 00:16:01
Java开发人员需知的十大戒律
2023-09-17 07:33:50
java中如何获取相关参数
2023-11-17 20:12:39
springboot读取自定义配置文件时出现乱码解决方案
2022-01-29 11:09:40
Java 中的弱引用是什么
2021-08-21 08:10:46
MyBatis3用log4j在控制台输出SQL的方法示例
2023-07-01 07:32:36
Android Handler,Message,MessageQueue,Loper源码解析详解
2022-04-17 17:34:57
SpringMVC Restful api接口实现的代码
2023-11-29 12:34:27
SpringMVC如何用Post方式重定向
2021-10-05 21:34:27
C#使用Socket实现本地多人聊天室
2022-01-06 12:47:27
android实现倒计时功能(开始、暂停、0秒结束)
2021-09-29 11:02:29
WPF实现魔方小游戏
2022-09-06 01:53:18
java读写ini文件、FileOutputStream问题
2023-11-29 08:50:13
总结Java集合类操作优化经验
2023-01-27 10:29:37
Android实现图文垂直跑马灯效果
2023-09-05 14:19:36
深入理解C# DateTime日期格式化
2021-12-08 04:23:17
Java线程池 ThreadPoolExecutor 详解
2021-05-24 10:35:42
Java中初始化List集合的八种方式汇总
2021-09-20 22:31:54