SpringBoot JdbcTemplate批量操作的示例代碼
前言
在我們做后端服務(wù)Dao層開發(fā),特別是大數(shù)據(jù)批量插入的時候,這時候普通的ORM框架(Mybatis、hibernate、JPA)就無法滿足程序?qū)π阅艿囊罅?。?dāng)然我們又不可能使用原生的JDBC進(jìn)行操作,那樣盡管效率會高,但是復(fù)雜度會上升。
綜合考慮我們使用Spring中的JdbcTemplate和具名參數(shù)namedParameterJdbcTemplate來進(jìn)行批量操作。
改造前
在開始講解之前,我們首先來看下之前的JPA是如何批量操作的。
實體類User:
public class AppStudent { private Integer id; private Integer classId; private String name; private Integer age; //偽代碼、省略構(gòu)造和get、set方法 }
DynamicQuery偽代碼:
@Repository public class DynamicQueryImpl implements DynamicQuery { @PersistenceContext private EntityManager em; public EntityManager getEntityManager() { return em; } //其實就是for循環(huán)、使用EntityManager的persist方法循環(huán)保存而已 @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方法:可以用于執(zhí)行任何SQL語句,一般用于執(zhí)行DDL語句;
- update方法及batchUpdate方法:update方法用于執(zhí)行新增、修改、刪除等語句;batchUpdate方法用于執(zhí)行批處理相關(guān)語句;
- query方法及queryForXXX方法:用于執(zhí)行查詢相關(guān)語句;
- call方法:用于執(zhí)行存儲過程、函數(shù)相關(guān)語句。
我們只需要在使用jdbcTemplate類中使用@Autowired進(jìn)行注入即可:
@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); }
以上基本實現(xiàn)了批量插入功能,但是當(dāng)數(shù)據(jù)庫字段比較多的時候,再以?占位符的形式編碼的話就可能不是那么好一 一對應(yīng)了,這里spring還提供了SimpleJdbcTemplate(Spring3.1+ 以后被標(biāo)記為過時,到Spring 4.3則被完全移除,后面這個完全能滿足需求)和NamedParameterJdbcTemplate模板引擎。
NamedParameterJdbcTemplate
相信使用過Hibernate的同學(xué)都知道,HQL中可以使用?或者:*的方式在外部配置查詢參數(shù)。在 Spring JDBC 框架中,也提供了一種綁定 SQL 參數(shù)的方式,使用具名參數(shù)(named parameter)。
我們只需要在使用NamedParameterJdbcTemplate類中使用@Autowired進(jìn)行注入即可:
@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)); //批量轉(zhuǎn)數(shù)組 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()來對比打印一下具體改造前后的執(zhí)行時間。
long start = System.currentTimeMillis(); //改造前后代碼、自行補(bǔ)充 long end = System.currentTimeMillis(); System.out.println("花費時間:"+(end-start));
快肯定是快了,至于快多少,那就要根據(jù)數(shù)據(jù)量以及機(jī)器配置來做相關(guān)的對比了。
項目源碼:https://gitee.com/52itstyle/spring-data-jpa
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
淺談hibernate中對象的3種狀態(tài)_瞬時態(tài)、持久態(tài)、脫管態(tài)
下面小編就為大家?guī)硪黄獪\談hibernate中對象的3種狀態(tài)_瞬時態(tài)、持久態(tài)、脫管態(tài)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08SpringBoot快速整合RabbitMq小案例(使用步驟)
這篇文章主要介紹了SpringBoot快速整合RabbitMq小案例,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-06-06關(guān)于JAVA8的 Stream學(xué)習(xí)
這篇文章主要介紹了JAVA8 Stream學(xué)習(xí)方法的相關(guān)資料,需要的朋友可以參考下面文章內(nèi)容2021-09-09