前言
在我們做后端服務(wù)dao層開發(fā),特別是大數(shù)據(jù)批量插入的時候,這時候普通的orm框架(mybatis、hibernate、jpa)就無法滿足程序?qū)π阅艿囊罅?。?dāng)然我們又不可能使用原生的jdbc進行操作,那樣盡管效率會高,但是復(fù)雜度會上升。
綜合考慮我們使用spring中的jdbctemplate和具名參數(shù)namedparameterjdbctemplate來進行批量操作。
改造前
在開始講解之前,我們首先來看下之前的jpa是如何批量操作的。
實體類user:
1
2
3
4
5
6
7
|
public class appstudent { private integer id; private integer classid; private string name; private integer age; //偽代碼、省略構(gòu)造和get、set方法 } |
dynamicquery偽代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
@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進行注入即可:
1
2
|
@autowired private jdbctemplate jdbctemplate; |
批量插入操作:
1
2
3
4
5
6
7
8
|
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進行注入即可:
1
2
|
@autowired private namedparameterjdbctemplate namedparameterjdbctemplate; |
批量插入操作:
1
2
3
4
5
6
7
8
9
10
11
|
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í)行時間。
1
2
3
4
|
long start = system.currenttimemillis(); //改造前后代碼、自行補充 long end = system.currenttimemillis(); system.out.println( "花費時間:" +(end-start)); |
快肯定是快了,至于快多少,那就要根據(jù)數(shù)據(jù)量以及機器配置來做相關(guān)的對比了。
項目源碼:https://gitee.com/52itstyle/spring-data-jpa
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.52itstyle.com/archives/2675/