SpringBoot使用JdbcTemplate批量保存
@Autowired
DataSourceProperties dataSourceProperties; @Autowired
ApplicationContext applicationContext;
public List<SubjectKycFileVO> batch() {
// JDBC模板依赖于连接池来获得数据的连接,所以必须先要构造连接池
DataSource dataSource = applicationContext.getBean(DataSource.class);
// dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
// dataSource.setUrl("jdbc:mysql://localhost:3306/leasing-boot?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Hongkong&nullCatalogMeansCurrent=true");
// dataSource.setUsername("root");
// dataSource.setPassword("root");
String sql = " INSERT INTO sj_kyc_file (id, createdAt, modifiedAt, createdBy, modifiedBy, version, isDelete, companyId, subjectId, numericalOrder, fileName, filePath, uploadDate) " +
" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ";
JdbcTemplate batchUpdate = new JdbcTemplate(dataSource);
// batchUpdate.setDataSource(dataSource);
List<SubjectKycFile> list = new ArrayList<>();
String currentUserId = super.getCurrentAuditor().get();
LocalDateTime now = LocalDateTime.now();
List<Object[]> objectList = new ArrayList<>();
for (int i = 0; i < 10000; i++) {
SubjectKycFile subjectKycFile = new SubjectKycFile();
subjectKycFile.setCreatedAt(now);
subjectKycFile.setModifiedAt(now);
subjectKycFile.setCreatedBy(currentUserId);
subjectKycFile.setModifiedBy(currentUserId);
subjectKycFile.setIsDelete(0);
subjectKycFile.setVersion(0);
subjectKycFile.setId(UUID.randomUUID().toString());
subjectKycFile.setCompanyId("26e5bacb-e3d3-49a9-92a2-1252068ebd66");
subjectKycFile.setNumericalOrder(i);
subjectKycFile.setFileName("" + i);
subjectKycFile.setFilePath("" + i);
subjectKycFile.setUploadDate(LocalDate.now());
subjectKycFile.setSubjectId("2a90f646-c47f-43a5-87c8-5c7f945b7b69");
list.add(subjectKycFile);
}
for (SubjectKycFile subjectKycFile : list) {
objectList.add(new Object[] { subjectKycFile.getId(), subjectKycFile.getCreatedAt(),subjectKycFile.getModifiedAt(),subjectKycFile.getCreatedBy(),subjectKycFile.getModifiedBy()
,subjectKycFile.getVersion(),subjectKycFile.getIsDelete(),subjectKycFile.getCompanyId(),subjectKycFile.getSubjectId(),subjectKycFile.getNumericalOrder(),subjectKycFile.getFileName()
,subjectKycFile.getFilePath(),subjectKycFile.getUploadDate()});
}
batchUpdate.batchUpdate(sql, objectList);
return null;
}
package com.cloudkeeper.leasing.subject.domain; import com.cloudkeeper.leasing.base.constant.BaseConstants;
import com.cloudkeeper.leasing.base.domain.SubjectBaseEntity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate; import javax.persistence.*;
import java.time.LocalDate;
import java.time.LocalDateTime; /**
* kyc附件
* @author lixin.shao
*/
@ApiModel(value = "kyc附件", description = "kyc附件")
@Getter
@Setter
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "sj_kyc_file")
public class SubjectKycFile { @Id
@GeneratedValue(generator = "idGenerator")
@GenericGenerator(name = "idGenerator", strategy = "uuid2")
@Column(length = 36)
@ApiModelProperty(value = "主键id", position = 1)
private String id; /** 创建时间 */
@CreatedDate
@ApiModelProperty(value = "创建时间", position = 2)
private LocalDateTime createdAt; /** 更新时间 */
@LastModifiedDate
@ApiModelProperty(value = "更新时间", position = 3)
private LocalDateTime modifiedAt; /** 创建人 */
@Column(length = 36)
@CreatedBy
@ApiModelProperty(value = "创建人", position = 4)
private String createdBy; /** 更新人 */
@Column(length = 36)
@LastModifiedBy
@ApiModelProperty(value = "更新人", position = 5)
private String modifiedBy; /** 版本(乐观锁) */
@Version
@ApiModelProperty(value = "版本(乐观锁)", position = 6)
private Integer version; /** 逻辑删除 */
@ApiModelProperty(value = "逻辑删除", position = 7)
private Integer isDelete = BaseConstants.Boolean.FALSE.ordinal(); /** 公司id */
@ApiModelProperty(value = "公司id", position = 8)
@Column(length = 36)
private String companyId; /** 案件id*/
@ApiModelProperty(value = "案件id", position = 9)
@Column(name = "subjectId", length = 36)
private String subjectId; /** 案件*/
@ApiModelProperty(value = "案件")
@ManyToOne
@JoinColumn(name = "subjectId", updatable = false, insertable = false)
@JsonIgnore
private Subject subject; /** 序号 */
@ApiModelProperty(value = "序号", position = 10)
private Integer numericalOrder; /** 文件名称 */
@ApiModelProperty(value = "文件名称", position = 10)
@Column(length = 100)
private String fileName; /** 文件路径 */
@ApiModelProperty(value = "文件路径", position = 10)
@Column(length = 200)
private String filePath; /** 上传时间 */
@ApiModelProperty(value = "上传时间", position = 10)
private LocalDate uploadDate; }
使用jdbcTemplate BatchUpdate批量插入效率慢
rewriteBatchedStatements=true
url: jdbc:mysql://localhost:3306/leasing-boot?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Hongkong&rewriteBatchedStatements=true
SpringBoot使用JdbcTemplate批量保存的更多相关文章
- springboot jpa 批量保存数据--EntityManager和 JpaRepository
1: 项目里面使用springboo-boot-start-data-jpa操作数据库,通过源码,在repository上继承JpaRepository 可以实现保存操作,其中源码接口为: <S ...
- SpringBoot使用JdbcTemplate
前言 本文是对SpringBoot使用JdbcTemplate操作数据库的一个介绍,,提供一个小的Demo供大家参考. 操作数据库的方式有很多,本文介绍使用SpringBoot结合JdbcTempla ...
- 使用SpringBoot-JPA进行自定义的保存及批量保存
更多精彩博文,欢迎访问我的个人博客 说明 SpringBoot版本:2.1.4.RELEASE java版本:1.8 文中所说JPA皆指spring-boot-starter-data-jpa 使用J ...
- .NET DLL 保护措施应用实例(百度云批量保存工具)
最近做了个小工具,将保护措施思路全部应用到了此工具中. 点我下载 百度云批量保存工具是一款专门用于自动批量保存百度云分享的软件. 本软件特点:1:完全模拟人工操作:2:可以批量保存百度分享的文件( ...
- springboot之JdbcTemplate
springboot可以使用JdbcTemplate进行数据库访问,代码如下 添加pom文件 <parent> <groupId>org.springframework.boo ...
- sqlbulkcopy 多表批量保存
/// <summary> /// 批量保存多表 /// </summary> /// <param name="dt1"></param ...
- 使用EntityManager批量保存数据
@PersistenceContext EntityManager em; 从别的系统中定期同步某张表的数据,由于数据量较大,采用批量保存 JPA EntityManager的四个主要方法 ① pub ...
- Howto: 在ArcGIS10中将地图文档(mxd文档)批量保存到之前版本
Howto: 在ArcGIS10中将地图文档(mxd文档)批量保存到之前版本 文章编号 : 38783 软件: ArcGIS - ArcEditor 10 ArcGIS - ArcInfo 10 A ...
- mybatis单笔批量保存
在上一篇写了接口调用解析返回的xml,并赋值到实体.这一篇主要介绍,如何保存实体数据. 一,xml样例 <?xml version="1.0" encoding=" ...
随机推荐
- jmeter变量嵌套:__V
问题复现 ${name_${n}} 下面没有获取到结果 解决方案 __V是用于执行变量名表达式 ${__V(name_${n})} 获取到结果
- Zookeeper应用场景和ZAB协议
Zookeeper应用场景 数据发布/订阅(配置中心) 我们平常的开发过程中,经常会碰到这样的需求:系统中需要一些通用的配置信息,如一些运行时的开关.前端需要展示的通知信息.数据库配置信息等等.这些需 ...
- 【windows 操作系统】异步
转载:https://cloud.tencent.com/developer/article/1744660 二.异步与多线程 1)基本概念 1. 并发:在操作系统中,是指一个时间段中有几个程序都处于 ...
- shell脚本编程练习
转至:http://www.178linux.com/88406 1.写一个脚本,使用ping命令探测172.16.250.1-172.16.250.254之间的所有主机的在线状态 在线的主机使用绿色 ...
- java 执行shell命令遇到的坑
正常来说java调用shell命令就是用 String[] cmdAry = new String[]{"/bin/bash","-c",cmd} Runtim ...
- MySQL第三讲
昨日内容回顾 公钥私钥 数据库存储引擎 MyISAM 是5.5版本之前默认的存储引擎 存取数据的速度较快.但是安全性偏差 三个文件 结构.索引.数据 InnoDB 5.5版本及之后默认的存储引擎 存取 ...
- Redis-基本概念、java操作redis、springboot整合redis,分布式缓存,分布式session管理等
NoSQL的引言 Redis数据库相关指令 Redis持久化相关机制 SpringBoot操作Redis Redis分布式缓存实现 Resis中主从复制架构和哨兵机制 Redis集群搭建 Redis实 ...
- 【一】TSP、VRP、VRP模型介绍
一. TSP问题数学模型 编辑 TSP,即Traveling Salesman Problem,也就是旅行商问题,又译为旅行推销员问题.货郎担问题,简称为TSP问题,是最基本的路线问题,该问题是在寻求 ...
- MATLAB神经网络应用设计【2】
一.深度学习中经常看到epoch. iteration和batchsize,下面按自己的理解说说这三个的区别: (1)batchsize:批大小.在深度学习中,一般采用SGD训练,即每次训练在训练集中 ...
- TP5实现多文件上传及展示
view层上传: <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...