好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Mybatis批量更新三种方式的实现

mybatis实现 批量 更新 操作

方式一:

?

1

2

3

4

5

6

7

8

9

10

<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语句默认是不支持以" ; " 结尾的,也就是不支持多条sql语句的执行。所以需要在连接mysql的url上加 &allowmultiqueries=true 这个才可以执行。

方式二:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

<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>

这种方式貌似效率不高,但是可以实现,而且不用改动mysql连接

效率参考文章: http://www.tuohang.net/article/176279.html

方式三:

临时改表sqlsessionfactory的属性,实现批量提交的java,但无法返回受影响数量。

?

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

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 ; //提交数量,到达这个数量就提交

       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对象,其中getbean() 获得的是想要得到的sqlsessionfactory。mapper 是自己的更具业务需求的mapper接口类,object是对象。

总结

方式一 需要修改mysql的连接url,让全局支持多sql执行,不太安全 方式二 当数据量大的时候 ,效率明显降低 方式三 需要自己控制,自己处理,一些隐藏的问题无法发现。

附件:springcontextutil.java

?

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

@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 ;

     }

   }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:https://segmentfault.com/a/1190000018084851

查看更多关于Mybatis批量更新三种方式的实现的详细内容...

  阅读:16次