ibatis批量插入-iterate标签应用

来源:互联网 发布:如何用c语言求素数 编辑:程序博客网 时间:2024/06/06 03:29

      项目开发中在很多地方可能会遇到同时插入多条记录到数据库的业务场景,如果业务级别循环单条插入数据会不断建立连接且有多个事务,这个时候如果业务的事务执行频率相当较高的话(高并发),对数据库的性能影响是比较大的;为了提高效率,批量操作会是不错的选择,一次批量操作只需要建立一次连接且一个事务,能很大程度上提高数据库的效率。

      批量插入操作的sql语句原型如下:

  1. insert  into    
  2.     wsjiang_test(col1, col2, col3)   
  3. values    
  4.     (col1_v, col2_v, col3_v),   
  5.     (col1_v, col2_v, col3_v),  
  6.      ... 

      这里我们以ibatis为例,进行应用说明!

 

     一、 ibatis iterate标签配置说明

  1. < iterate   
  2.     property =""  /*可选,   
  3.         从传入的参数集合中使用属性名去获取值,   
  4.         这个必须是一个List类型,   
  5.         否则会出现OutofRangeException,   
  6.         通常是参数使用java.util.Map时才使用,   
  7.         如果传入的参数本身是一个java.util.List, 不能只用这个属性.  
  8.         不知道为啥官网: http://ibatis.apache.org/docs/dotnet/datamapper/ch03s09.html#id386679  
  9.         说这个属性是必须的, 但是测试的时候是可以不设置这个属性的, 还望那位大虾知道, 讲解一下.  
  10.         */  
  11.     conjunction =""  /*可选,   
  12.         iterate可以看作是一个循环,   
  13.         这个属性指定每一次循环结束后添加的符号,   
  14.          比如使每次循环是OR的, 则设置这个属性为OR*/  
  15.     open =""  /*可选, 循环的开始符号*/  
  16.     close =""  /*可选, 循环的结束符号*/  
  17.     prepend =""  /*可选, 加在open指定的符号之前的符号*/  
  18. > </ iterate >

 

       二、 ibatis iterate标签使用示例

              1、批量查询

  1. < select  id ="iterate_query"  parameterClass ="java.util.List" >   
  2.     <![CDATA[  
  3.         selelct * from wsjiang_test where id=1 
  4.     ]]>   
  5.     < iterate  prepend ="prepend"  conjunction ="conn"  open ="open"  colse ="close" >   
  6.         /*使用java.util.List作为参数不能设置property属性*/  
  7.         <![CDATA[  
  8.             #v[]#  
  9.         ]]> /*这里的"[]"是必须的, 要不然ibatis会把v直接解析为一个String*/  
  10.     </ iterate >   
  11. </ select >

             如果传入一个List为[123,234,345], 上面的配置将得到一个sql语句:

                   select * from wsjiang_test where id=1 prepend open 123 conn 234 conn 345 close

 

            2、批量插入

               A、不使用open/close属性

  1. < insert  id =" iterate_insert1 "  parameterClass ="java.util.List" >   
  2.     <![CDATA[  
  3.         insert into wsjinag_test( col1 , col2 , col3 ) values  
  4.     ]]>    
  5.     < iterate  conjunction ="," >   
  6.         <![CDATA[  
  7.             (#test[]. col1 #, # test []. col2 #, # test []. col3 #)  
  8.         ]]>   
  9.     </ iterate >   
  10. </ insert > 

              上面的配置将得到一个sql语句:

                   insert  into wsjiang_test( col1, col2, col3 ) values  (?, ?, ?) , (?, ?, ?) , (?, ?, ?) 

 

              B、使用open/close属性

  1. < insert  id ="betchAddNewActiveCode"  parameterClass ="java.util.List" >    
  2.    <![CDATA[  
  3.         insert into wsjinag_test( col1 , col2 , col3 ) values  
  4.     ]]> 
  5.     < iterate  conjunction =","  open ="("  close =")" >   
  6.         <![CDATA[  
  7.             /*这里不加"("和")"*/  
  8.             #test[]. col1 #, # test []. col2 #, # test []. col3 #
  9.         ]]>   
  10.     </ iterate >   
  11. </ insert > 

             上面的配置将得到一个sql语句:

                  insert  into wsjiang_test( col1, col2, col3 ) values  (?, ?, ?    ,     ?, ?, ?     ,     ?, ?, ?)

 

         这两种使用方式区别是相当大的. conjunction, open 和close这几个属性需小心使用,将其区分开.

 

    三、单条插入返回新增记录主键

          通常情况,ibatis的insert方法需要返回新增记录的主键,但并非任何表的insert操作都会返回主键(这是一个陷阱);要返回这个新增记录的主键,前提是表的主键是自增型的,或者是Sequence的;且必须启用ibatis的selectKey 标签; 否则获取新增记录主键的值为0或者null。

         ibatis的配置:

  1. < insert  id =" iterate_insert1 "  parameterClass ="Object" >   
  2.     <![CDATA[  
  3.         insert into wsjinag_test( col1 , col2 , col3 ) 
  4.         values   (# col1 #, # col2 #, # col3 #)  
  5.     ]]>    
  6.     < selectKey   keyProperty ="id" resultClass= "Long" >   
  7.         <![CDATA[  
  8.             SELECT LAST_INSERT_ID() AS value  
  9.         ]]>   
  10.     </ selectKey >   
  11. </ insert >

 

  四、 插入返回 新增记录数

      在第三节中已经讲清楚通过ibatis的insert方法只能得到新增记录的ID; 如果对于无需知道新增记录ID,只需要知道有没有插入成功的业务场景时,特别是对于批量插入,配置的selectKey 可能会有问题时,一次插入多条,拿不到新增的ID,这时我们就只能返回插入成功的记录数来区分是否新增成功!但是insert方法是不会返回记录数;于是我们可以使用ibatis的update方法来调用没有配置 selectKey 标签的insert语句,这样就能返回影响(插入)的记录数了!





mybatis批量插入和删除


Mybatis 插入与批量插入以及多参数批量删除

实体类:

复制代码
import java.io.Serializable;public class AttachmentTable implements Serializable {    private static final long serialVersionUID = 8325882509007088323L;    private Integer id;    // 附件名称    private String name;    // 日志ID    private Integer logid;    // 附件URL    private String url;    // getter/setter.......}
复制代码

Mapper接口:

复制代码
import java.util.List;import model.AttachmentTable;public interface AttachmentTableMapper {  int insert(AttachmentTable record);  void insertByBatch(List<AttachmentTable> attachmentTables);}
复制代码

Mapper.xml:

复制代码
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="mapper.AttachmentTableMapper">    <resultMap id="BaseResultMap" type="model.AttachmentTable">        <id column="id" jdbcType="INTEGER" property="id" />        <result column="name" jdbcType="VARCHAR" property="name" />        <result column="logID" jdbcType="INTEGER" property="logid" />    </resultMap>    <resultMap extends="BaseResultMap" id="ResultMapWithBLOBs" type="model.AttachmentTable">        <result column="url" jdbcType="LONGVARCHAR" property="url" />    </resultMap>    <sql id="Base_Column_List">        id, name, logID    </sql>    <sql id="Blob_Column_List">        url    </sql>    <insert id="insert" parameterType="model.AttachmentTable">        insert into attachment_table (id, name, logID,url)        values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{logid,jdbcType=INTEGER},#{url,jdbcType=LONGVARCHAR})    </insert>    <insert id="insertByBatch" parameterType="java.util.List">        insert into attachment_table (name, logID,url)        values        <foreach collection="list" item="item" index="index" separator=",">            (#{item.name,jdbcType=VARCHAR}, #{item.logid,jdbcType=INTEGER},#{item.url,jdbcType=LONGVARCHAR})        </foreach>    </insert></mapper>
复制代码

【注:标红的地方是需要注意的地方,我第一次做时直接“#{name,jdbcType=VARCHAR}”,没有加前缀“item”,导致报错“找不到name”】

 (二)多参数批量删除示例

package com.vrv.linkdood.app.workreport.demomodule.mapper;import org.apache.ibatis.annotations.Param;public interface AttachmentTableMapper {    void deleteByLogIdAndNames(@Param("logid") Integer logID, @Param("names") String[] names);}
复制代码
    <delete id="deleteByLogIdAndNames">        delete from attachment_table        where logid = #{logid,jdbcType=INTEGER} AND NAME IN        <foreach collection="names" item="item" index="index" open="(" close=")" separator=",">            #{item}        </foreach>    </delete>
复制代码

 

复制代码
属性描述item循环体中的具体对象。支持属性的点路径访问,如item.age,item.info.details。
具体说明:在list和数组中是其中的对象,在map中是value。
该参数为必选。collection

要做foreach的对象,作为入参时,List<?>对象默认用list代替作为键,数组对象有array代替作为键,Map对象没有默认的键。
当然在作为入参时可以使用@Param("keyName")来设置键,设置keyName后,list,array将会失效。 除了入参这种情况外,还有一种作为参数对象的某个字段的时候。举个例子:
如果User有属性List ids。入参是User对象,那么这个collection = "ids"
如果User有属性Ids ids;其中Ids是个对象,Ids有个属性List id;入参是User对象,那么collection = "ids.id"
上面只是举例,具体collection等于什么,就看你想对那个元素做循环。
该参数为必选。

separator元素之间的分隔符,例如在in()的时候,separator=","会自动在元素中间用“,“隔开,避免手动输入逗号导致sql错误,如in(1,2,)这样。该参数可选。openforeach代码的开始符号,一般是(和close=")"合用。常用在in(),values()时。该参数可选。closeforeach代码的关闭符号,一般是)和open="("合用。常用在in(),values()时。该参数可选。index在list和数组中,index是元素的序号,在map中,index是元素的key,该参数可选。

0 0
原创粉丝点击