SpringBoot JdbcTemplate批量操作的示例代码
作者:柒''s 时间:2023-05-15 21:46:42
前言
在我们做后端服务Dao层开发,特别是大数据批量插入的时候,这时候普通的ORM框架(Mybatis、hibernate、JPA)就无法满足程序对性能的要求了。当然我们又不可能使用原生的JDBC进行操作,那样尽管效率会高,但是复杂度会上升。
综合考虑我们使用Spring中的JdbcTemplate和具名参数namedParameterJdbcTemplate来进行批量操作。
改造前
在开始讲解之前,我们首先来看下之前的JPA是如何批量操作的。
实体类User:
public class AppStudent {
private Integer id;
private Integer classId;
private String name;
private Integer age;
//伪代码、省略构造和get、set方法
}
DynamicQuery伪代码:
@Repository
public class DynamicQueryImpl implements DynamicQuery {
@PersistenceContext
private EntityManager em;
public EntityManager getEntityManager() {
return em;
}
//其实就是for循环、使用EntityManager的persist方法循环保存而已
@Override
public <T> void saveList(List<T> resultList) {
for (int i = 0; i < resultList.size(); i++) {
T t = resultList.get(i);
em.persist(t);
}
}
}
改造后
JdbcTemplate
JdbcTemplate提供的主要方法:
execute方法:可以用于执行任何SQL语句,一般用于执行DDL语句;
update方法及batchUpdate方法:update方法用于执行新增、修改、删除等语句;batchUpdate方法用于执行批处理相关语句;
query方法及queryForXXX方法:用于执行查询相关语句;
call方法:用于执行存储过程、函数相关语句。
我们只需要在使用jdbcTemplate类中使用@Autowired进行注入即可:
@Autowired
private JdbcTemplate jdbcTemplate;
批量插入操作:
public void batchSave(){
List<Object[]> batchArgs=new ArrayList<Object[]>();
batchArgs.add(new Object[]{1,"小明",21});
batchArgs.add(new Object[]{2,"小红",22});
batchArgs.add(new Object[]{3,"露西",23});
String sql = "insert into user (username,password) values (?,?)";
jdbcTemplate.batchUpdate(sql, batchArgs);
}
以上基本实现了批量插入功能,但是当数据库字段比较多的时候,再以?占位符的形式编码的话就可能不是那么好一 一对应了,这里spring还提供了SimpleJdbcTemplate(Spring3.1+ 以后被标记为过时,到Spring 4.3则被完全移除,后面这个完全能满足需求)和NamedParameterJdbcTemplate模板引擎。
NamedParameterJdbcTemplate
相信使用过Hibernate的同学都知道,HQL中可以使用?或者:*的方式在外部配置查询参数。在 Spring JDBC 框架中,也提供了一种绑定 SQL 参数的方式,使用具名参数(named parameter)。
我们只需要在使用NamedParameterJdbcTemplate类中使用@Autowired进行注入即可:
@Autowired
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
批量插入操作:
public void batchSave(){
List<User> list = new ArrayList<User>();
//新增用户
list.add(new AppStudent(1,"张三",21));
list.add(new AppStudent(1,"李四",22));
list.add(new AppStudent(1,"王二麻子",23));
//批量转数组
SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(list.toArray());
String sql = "INSERT INTO app_student(class_id,name,age) VALUES (:classId,:name,:age)";
namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
}
最后我们使用System.currentTimeMillis()来对比打印一下具体改造前后的执行时间。
long start = System.currentTimeMillis();
//改造前后代码、自行补充
long end = System.currentTimeMillis();
System.out.println("花费时间:"+(end-start));
快肯定是快了,至于快多少,那就要根据数据量以及机器配置来做相关的对比了。
项目源码:https://gitee.com/52itstyle/spring-data-jpa
来源:https://blog.52itstyle.com/archives/2675/
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Mybatis generator自动生成代码插件实例解析
![](https://img.aspxhome.com/file/2023/4/65754_0s.png)
在springboot中实现个别bean懒加载的操作
![](https://img.aspxhome.com/file/2023/3/60033_0s.jpg)
IDEA与模拟器安装调试失败的处理方法:INSTALL_PARSE_FAILED_NO_CERTIFICATES
![](https://img.aspxhome.com/file/2023/2/75462_0s.png)
JAVA内存模型(JMM)详解
![](https://img.aspxhome.com/file/2023/5/59975_0s.png)
Java使用组合模式实现表示公司组织结构功能示例
![](https://img.aspxhome.com/file/2023/4/75404_0s.png)
java servlet 几种页面跳转的方法
通过C++程序示例理解设计模式中的外观模式
![](https://img.aspxhome.com/file/2023/7/107387_0s.jpg)
Android中生成、使用Json数据实例
Unity3D 计时器的实现代码(三种写法总结)
WPF Slider滑动条的颜色修改方法
![](https://img.aspxhome.com/file/2023/7/81147_0s.png)
idea向System.getenv()添加系统环境变量的操作
![](https://img.aspxhome.com/file/2023/5/129205_0s.jpg)
Java MAVEN 工程pom配置报错解决方案
![](https://img.aspxhome.com/file/2023/0/110820_0s.png)
详解c# 委托链
![](https://img.aspxhome.com/file/2023/0/77300_0s.png)
android studio 4.0 新建类没有修饰符的方法
![](https://img.aspxhome.com/file/2023/4/138754_0s.jpg)
基于Apache组件分析对象池原理的实现案例分析
![](https://img.aspxhome.com/file/2023/2/99742_0s.png)
WPF自定义控件和样式之自定义按钮(Button)
![](https://img.aspxhome.com/file/2023/5/108825_0s.gif)
Java实现提取QSV文件视频内容
Android 用 camera2 API 自定义相机
![](https://img.aspxhome.com/file/2023/2/139532_0s.jpg)
spring boot项目快速构建的全步骤
![](https://img.aspxhome.com/file/2023/9/110809_0s.png)
基于Unity制作一个简易的计算器
![](https://img.aspxhome.com/file/2023/2/82162_0s.png)