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=" ...
随机推荐
- 详解用OpenCV绘制各类几何图形
摘要:本文详细介绍了OpenCV绘制几何图形的方法,利用cv2.line().v2.circle().cv2.rectangle().cv2.ellipse().cv2.polylines().cv2 ...
- (二)ECMA 335 解析 /ECMA 334
C#被ECMA组织,定义为了<ECMA334>标准化语言. 什么概念? 比如说,上一次成为ECMA标准的语言是Javascript.即<ECMA262>标准. <ECMA ...
- WPF中常用控件(TreeView, ComboBox, DataGrid, ListView)使用MVVM模式绑定的demo
之前几篇关于TreeView的博客中只是贴了源代码,并没有把整个项目上传到github.最近就想着把我常用的几个控件做成一个demo,这样也方便自己以后查看.本人也是WPF新手,但是我并没有打算就往这 ...
- python语法:注释
Python语法:注释 python语言中的注释是来帮助程序员理解并读懂代码内容的文字.当然,注释不仅在python语言中是这个作用,在其他语言中也几乎一样. python注释的生成方式 所有演示 ...
- Python:Scipy.interpolate
注意 以下插值函数中,待插值点的坐标,最好按次序排列(参与插值的基准点的坐标可以打乱次序).如果打乱顺序,可能会导致插值结果异常(插值异常而不是错误,不会报错,但是结果有明显异常). griddata ...
- vue--按需加载的3种方式(解决网页首次加载速度慢的问题)
一.vue的异步组件加载 使用异步组件加载,打包的时候会将每个组件分开打包到不同的js文件中: {path: '/index', name: 'index', meta:{ title:'首页', r ...
- LeetCode-290-单词规律
单词规律 题目描述:给定一种规律 pattern 和一个字符串 str ,判断 str 是否遵循相同的规律. 这里的 遵循 指完全匹配,例如, pattern 里的每个字母和字符串 str 中的每个非 ...
- linux文本处理grep
grep grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具 ...
- TCC分布式事框架务详解
之前网上看到很多写分布式事务的文章,不过大多都是将分布式事务各种技术方案简单介绍一下.很多朋友看了还是不知道分布式事务到底怎么回事,在项目里到底如何使用. 所以这篇文章,就用大白话+手工绘图,并结合一 ...
- js开发文档生成工具jsdoc安装使用
1.全局安装jsdoc,需要先安装node环境哦~ npm install -g jsdoc 2.在项目根目录新建文件conf.json,内容如下: { "tags": { &qu ...