@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批量保存的更多相关文章

  1. springboot jpa 批量保存数据--EntityManager和 JpaRepository

    1: 项目里面使用springboo-boot-start-data-jpa操作数据库,通过源码,在repository上继承JpaRepository 可以实现保存操作,其中源码接口为: <S ...

  2. SpringBoot使用JdbcTemplate

    前言 本文是对SpringBoot使用JdbcTemplate操作数据库的一个介绍,,提供一个小的Demo供大家参考. 操作数据库的方式有很多,本文介绍使用SpringBoot结合JdbcTempla ...

  3. 使用SpringBoot-JPA进行自定义的保存及批量保存

    更多精彩博文,欢迎访问我的个人博客 说明 SpringBoot版本:2.1.4.RELEASE java版本:1.8 文中所说JPA皆指spring-boot-starter-data-jpa 使用J ...

  4. .NET DLL 保护措施应用实例(百度云批量保存工具)

    最近做了个小工具,将保护措施思路全部应用到了此工具中. 点我下载   百度云批量保存工具是一款专门用于自动批量保存百度云分享的软件. 本软件特点:1:完全模拟人工操作:2:可以批量保存百度分享的文件( ...

  5. springboot之JdbcTemplate

    springboot可以使用JdbcTemplate进行数据库访问,代码如下 添加pom文件 <parent> <groupId>org.springframework.boo ...

  6. sqlbulkcopy 多表批量保存

    /// <summary> /// 批量保存多表 /// </summary> /// <param name="dt1"></param ...

  7. 使用EntityManager批量保存数据

    @PersistenceContext EntityManager em; 从别的系统中定期同步某张表的数据,由于数据量较大,采用批量保存 JPA EntityManager的四个主要方法 ① pub ...

  8. Howto: 在ArcGIS10中将地图文档(mxd文档)批量保存到之前版本

     Howto: 在ArcGIS10中将地图文档(mxd文档)批量保存到之前版本 文章编号 : 38783 软件: ArcGIS - ArcEditor 10 ArcGIS - ArcInfo 10 A ...

  9. mybatis单笔批量保存

    在上一篇写了接口调用解析返回的xml,并赋值到实体.这一篇主要介绍,如何保存实体数据. 一,xml样例 <?xml version="1.0" encoding=" ...

随机推荐

  1. python数据结构:数组和列表

    线性结构有两种:数组和列表 array和list 其中list各项操作的时间复杂度如下 因为insert是在头部插入 所以列表所有元素后移,时间复杂度为O(n) remove移除列表中某个值的第一个匹 ...

  2. Smartbi代替Alteryx+Tableau,用1份投入如何获得2份回报?

    Smartbi是国内一家知名的BI厂商,Alteryx.Tableau是国外两款重要的BI工具,它们都是在BI领域内提供特定的功能,以满足企业的数据分析需求.那么,对于用户来说,在选择BI工具的时候要 ...

  3. 【C# .Net GC】清除非托管类型(Finalize终结器、dispose模式以及safeHandler)

    总结 1.一般要获取一个内核对象的引用,最好用SafeHandle来引用它,这个类可以帮你管理引用计数,而且用它引用内核对象,代码更健壮 2.托管中生成并引用非托管,一但非托管和托管中的引用断开(托管 ...

  4. linux时钟校准

    ## 查看系统时间 date ## 查看硬件时间 hwclock ## 手动设置时间 date -s "20210507 17:55:00" ## 同步硬件时间 hwclock - ...

  5. Ubuntu 18.04 安装配置LAMP

    --作者:飞翔的小胖猪 --创建时间:2021年5月29日 --修改时间:2021年5月29日 一.准备 1.1 环境 操作系统:Ubuntu 18.04 网页引擎:Apache php版本:7.4 ...

  6. Java课程设计---安装Mysql及管理工具

    1.安装mysql 没有安装包的可以在这个地址下载:https://dev.mysql.com/downloads/mysql/5.5.html 双击提供的安装包 (安装路径可以不用更改) 在弹出的窗 ...

  7. Qt:Shadow Build

    每个编辑器有Build和Run两个设置界面. 在Build界面上,有一个"Shadow build"复选框.如果勾选此项,编译后将在项目的同级目录下建立一个编译后的文件目录,目录名 ...

  8. JavaScript与C#互通的DES加解密算法的实现(转)

    本文提供了一个能使JavaScript与C#互通的DES加解密算法的实现,在前台页面中用JavaScript版本的DES算法将数据加密之后,传到服务器端,在服务器端可用C#版本的DES解密算法将其解密 ...

  9. JZ-031-从 1 到 n 整数中 1 出现的次数

    从 1 到 n 整数中 1 出现的次数 题目描述 求出1-13的整数中1出现的次数,并算出100-1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12.13因此 ...

  10. JZ-014-链表中倒数第 K 个结点

    链表中倒数第 K 个结点 题目描述 输入一个链表,输出该链表中倒数第k个结点. 题目链接: 链表中倒数第 K 个结点 代码 /** * 标题:链表中倒数第 K 个结点 * 题目描述 * 输入一个链表, ...