Mybatis批量更新三種方式的實(shí)現(xiàn)
Mybatis實(shí)現(xiàn)批量更新操作
方式一:
<update id="updateBatch" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" open="" close="" separator=";">
update tableName
<set>
name=${item.name},
name2=${item.name2}
</set>
where id = ${item.id}
</foreach>
</update>
但Mybatis映射文件中的sql語(yǔ)句默認(rèn)是不支持以" ; " 結(jié)尾的,也就是不支持多條sql語(yǔ)句的執(zhí)行。所以需要在連接mysql的url上加 &allowMultiQueries=true 這個(gè)才可以執(zhí)行。
方式二:
<update id="updateBatch" parameterType="java.util.List">
update tableName
<trim prefix="set" suffixOverrides=",">
<trim prefix="c_name =case" suffix="end,">
<foreach collection="list" item="cus">
<if test="cus.name!=null">
when id=#{cus.id} then #{cus.name}
</if>
</foreach>
</trim>
<trim prefix="c_age =case" suffix="end,">
<foreach collection="list" item="cus">
<if test="cus.age!=null">
when id=#{cus.id} then #{cus.age}
</if>
</foreach>
</trim>
</trim>
<where>
<foreach collection="list" separator="or" item="cus">
id = #{cus.id}
</foreach>
</where>
</update>
這種方式貌似效率不高,但是可以實(shí)現(xiàn),而且不用改動(dòng)mysql連接
效率參考文章:http://www.dbjr.com.cn/article/155835.htm
方式三:
臨時(shí)改表sqlSessionFactory的屬性,實(shí)現(xiàn)批量提交的java,但無(wú)法返回受影響數(shù)量。
public int updateBatch(List<Object> list){
if(list ==null || list.size() <= 0){
return -1;
}
SqlSessionFactory sqlSessionFactory = SpringContextUtil.getBean("sqlSessionFactory");
SqlSession sqlSession = null;
try {
sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH,false);
Mapper mapper = sqlSession.getMapper(Mapper.class);
int batchCount = 1000;//提交數(shù)量,到達(dá)這個(gè)數(shù)量就提交
for (int index = 0; index < list.size(); index++) {
Object obj = list.get(index);
mapper.updateInfo(obj);
if(index != 0 && index%batchCount == 0){
sqlSession.commit();
}
}
sqlSession.commit();
return 0;
}catch (Exception e){
sqlSession.rollback();
return -2;
}finally {
if(sqlSession != null){
sqlSession.close();
}
}
}
其中 SpringContextUtil 是自己定義的工具類 用來獲取spring加載的bean對(duì)象,其中g(shù)etBean() 獲得的是想要得到的sqlSessionFactory。Mapper 是自己的更具業(yè)務(wù)需求的Mapper接口類,Object是對(duì)象。
總結(jié)
- 方式一 需要修改mysql的連接url,讓全局支持多sql執(zhí)行,不太安全
- 方式二 當(dāng)數(shù)據(jù)量大的時(shí)候 ,效率明顯降低
- 方式三 需要自己控制,自己處理,一些隱藏的問題無(wú)法發(fā)現(xiàn)。
附件:SpringContextUtil.java
@Component
public class SpringContextUtil implements ApplicationContextAware{
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtil.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext(){
return applicationContext;
}
public static Object getBean(Class T){
try {
return applicationContext.getBean(T);
}catch (BeansException e){
return null;
}
}
public static Object getBean(String name){
try {
return applicationContext.getBean(name);
}catch (BeansException e){
return null;
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Mybatis pagehelper分頁(yè)插件使用過程解析
這篇文章主要介紹了mybatis pagehelper分頁(yè)插件使用過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
利用github搭建個(gè)人maven倉(cāng)庫(kù)的方法步驟
這篇文章主要介紹了利用github搭建個(gè)人maven倉(cāng)庫(kù)的方法步驟,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12
詳解Java中NullPointerException異常的原因詳解以及解決方法
這篇文章主要介紹了詳解Java中NullPointerException異常的原因詳解以及解決方法。文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
使用java實(shí)現(xiàn)telnet-client工具分享
這篇文章主要介紹了使用java實(shí)現(xiàn)telnet-client工具,需要的朋友可以參考下2014-03-03
安卓系統(tǒng)中實(shí)現(xiàn)搖一搖畫面振動(dòng)效果的方法
這篇文章主要介紹了安卓系統(tǒng)中實(shí)現(xiàn)搖一搖畫面振動(dòng)效果的方法,調(diào)用Android SDK中的SensorEventListener接口,需要的朋友可以參考下2015-07-07
Java語(yǔ)法基礎(chǔ)之運(yùn)算符學(xué)習(xí)筆記分享
這篇文章主要為大家分享了Java語(yǔ)法基礎(chǔ)之運(yùn)算符學(xué)習(xí)筆記,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09

