1.先看DAO層 html
1
2
3
4
5
6
|
publicvoidupdateList(List<AddService> oldAddServiceList) {
SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH,true);
session.update("AddServiceMapper.batchUpdate",oldAddServiceList);
session.commit();
session.close();
}
|
固然DAO層最上方類中注入上sqlSessionFactory java
1
2
|
@Resource(name ="sqlSessionFactory")
privateSqlSessionFactory sqlSessionFactory;
|
1
|
sqlSessionFactory.openSession(ExecutorType.BATCH,true);
|
詳細瞭解請點擊 http://www.cnblogs.com/xcch/articles/2042298.html mysql
2.再看XML配置文件 sql
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<updateid="batchUpdate"parameterType="java.util.List">
<foreachcollection="list"item="item"index="index"separator=";">
update sp_add_service set
SERVICE_NAME = #{item.serviceName},
SERVICE_TYPE = #{item.serviceType},
PRICE = #{item.price},
DESCRIPTION = #{item.description},
PRICE_RATE = #{item.priceRate},
IS_REQUIRED = #{item.isRequired},
MODIFY_TIME = now()
where ID = #{item.id}
</foreach>
</update>
|
3.再看Service層 數據庫
1
2
3
4
|
@Transactional
publicvoidupdateOldAddServces(List<AddService> oldAddServiceList) {
addServiceDao.updateList(oldAddServiceList);
}
|
注意!!!以上代碼全照例子寫後發現批量更新仍是不行,後來百度了一下,數據庫鏈接代碼後面加上&allowMultiQueries=true session
jdbc:mysql://111.111.111.111:3306/ibada?characterEncoding=UTF-8&allowMultiQueries=true
mybatis
而後再執行,發現OK了。批量更新大工告成。 app
批量插入和批量刪除大致同樣,就給上XML供你們參考,不作詳細描述。 單元測試
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<!-- 批量插入 -->
<insertid="batchInsert"parameterType="java.util.List">
insert into sp_add_service( <includerefid="AddService_Column_List"/> )
values
<foreachcollection="list"item="item"index="index"separator=",">
(#{item.id}, #{item.logisticsLtdId}, #{item.serviceName}, #{item.serviceType},
#{item.price},#{item.description},#{item.priceRate},
#{item.parentId}, #{item.isRequired}, now(), now())
</foreach>
</insert>
<deleteid="batchDelete">
delete from sp_add_service
where LOGISTICS_LTD_ID = #{logisticsLtdId}
and ID not in
<foreachitem="item"index="index"collection="oldServiceIds"
open="("separator=","close=")">
#{item}
</foreach>
</delete>
|
關於上面的博客裏面的內容一直沒仔細看,可能原博主那個true寫的不對,應該是fasle, 測試
1
|
(ExecutorType.BATCH,false);
|
測試環境,mysql數據庫。
測試內容,5000條數據的插入。
測試Dao層代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
@Repository
publicclassTruckSourceDaoextendsBaseDao{
@Resource(name ="sqlSessionFactory")
privateSqlSessionFactory sqlSessionFactory;
publicInteger batchInsertList(List<TruckSource> truckSources){
SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH,false);
for(inti =0;i < truckSources.size(); i++) {
session.insert("TruckSource.insertOne",truckSources.get(i));
}
session.commit();
session.clearCache();
returnnull;
}
publicInteger insertList(List<TruckSource> truckSources){
returngetSqlSession().insert("TruckSource.insertBatch",truckSources);
}
publicInteger batchInsertLists(List<TruckSource> truckSources){
SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH,false);
session.insert("TruckSource.insertBatch",truckSources);
session.commit();
session.clearCache();
returnnull;
}
}
|
Mapper.xml裏面代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<sqlid="TruckSource_Column_List">
TRUCK_OWNER_ID,TRUCK_SOURCE_TYPE,INITIATION_PROVINCE_CODE,INITIATION_CITY_CODE,INITIATION_DISTRICT_CODE,DESTINATION_PROVINCE_CODE,DESTINATION_CITY_CODE,DESTINATION_DISTRICT_CODE,
SURPLUS_LOAD,SURPLUS_LENGTH,SURPLUS_VOLUME,START_TIME,VALID_DATE,STATE,NOTICE,CREATE_TIME,MODIFY_TIME
</sql>
<insertid="insertBatch"parameterType="java.util.List">
insert into sp_truck_source(<includerefid="TruckSource_Column_List"/>)
values
<foreachcollection="list"item="item"index="index"separator=",">
(#{item.truckOwnerId}, #{item.truckSourceType}, #{item.initiationProvinceCode}, #{item.initiationCityCode},
#{item.initiationDistrictCode}, #{item.destinationProvinceCode}, #{item.destinationCityCode}, #{item.destinationDistrictCode},
#{item.surplusLoad}, #{item.surplusLength}, #{item.surplusVolume}, #{item.startTime},
#{item.validDate},#{item.state},#{item.notice},
now(), now())
</foreach>
</insert>
<insertid="insertOne"parameterType="TruckSource">
insert into sp_truck_source(<includerefid="TruckSource_Column_List"/>)
values (#{truckOwnerId},#{truckSourceType},#{initiationProvinceCode},#{initiationCityCode},#{initiationDistrictCode},#{destinationProvinceCode},#{destinationCityCode},#{destinationDistrictCode},
#{surplusLoad},#{surplusLength},#{surplusVolume},#{startTime},#{validDate},#{state},#{notice},now(),now())
</insert>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations ="classpath:applicationContext.xml")
publicclassTTTT {
@Resource
privateTruckSourceDao truckSourceDao;
@Test
publicvoidinsert() {
List<TruckSource> truckSources =newArrayList<TruckSource>();
for(inti =0;i <5000; i++) {
TruckSource truckSource =newTruckSource();
truckSource.setDestinationDistrictCode(i+"");
truckSource.setDestinationCityCode(i+"");
truckSource.setDestinationProvinceCode(i+"");
truckSource.setInitiationCityCode(i+"");
truckSource.setInitiationDistrictCode(i+"");
truckSource.setInitiationProvinceCode(i+"");
truckSources.add(truckSource);
}
Long beginTime = System.currentTimeMillis();
truckSourceDao.batchInsertList(truckSources);
System.out.println("batchInsert:"+(System.currentTimeMillis() - beginTime));
beginTime = System.currentTimeMillis();
truckSourceDao.insertList(truckSources);
System.out.println("foreach insert:"+(System.currentTimeMillis() - beginTime));
beginTime = System.currentTimeMillis();
truckSourceDao.batchInsertLists(truckSources);
System.out.println("batch foreach insert:"+(System.currentTimeMillis() - beginTime));
}
}
|
測試方法種類,3種:
1.batchInsertList 插入時單獨開一個session用
1
|
SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH,false);
|
而後下面for循環依次單個插入
2.在mapper.xml裏面寫foreach sql插入
3.加上ExecutoerType.Batch,再在mapper.xml裏面寫foreach sql插入
最後實驗結果是第三種執行時間最短:
{insert3=5128, insert1=146085, insert2=5625}
上面是數據量爲5000狀況下的實驗結果。
在數據量爲1000的狀況下,第2種是2000多毫秒,第三種是800多毫秒,因此差距仍是比較大的。