Mybatis批量插入(mapper文件)

Mapper文件代码

 <!--批量插入 如果主键自增,想要返回自增主键,添加 useGeneratedKeys="true"-->
  <insert id="multiplyInsertSelective" parameterType="java.util.List" >
    insert into ep_chapter
    <foreach collection="chapterList" separator="," index="index" item="item">
      <if test="index == 0">
        <trim prefix="(" suffix=") values " suffixOverrides="," >
          <if test="#{item.id} != null" >
            id,
          </if>
          <if test="#{item.chapterName} != null" >
            chapter_name,
          </if>
          <if test="#{item.courseId} != null" >
            course_id,
          </if>
          <if test="#{item.updateTime} != null" >
            update_time,
          </if>
          <if test="#{item.duration} != null" >
            duration,
          </if>
          <if test="#{item.status} != null" >
            status,
          </if>
          <if test="#{item.intro} != null" >
            intro,
          </if>
          <if test="#{item.url} != null" >
            url,
          </if>
          <if test="#{item.root} != null" >
            root,
          </if>
          <if test="#{item.free} != null" >
            free,
          </if>
          <if test="#{item.createTime} != null" >
            create_time,
          </if>
        </trim>
      </if>
      <trim prefix="(" suffix=")" suffixOverrides="," >
        <if test="#{item.id} != null" >
          #{item.id,jdbcType=BIGINT},
        </if>
        <if test="#{item.chapterName} != null" >
          #{item.chapterName,jdbcType=VARCHAR},
        </if>
        <if test="#{item.courseId} != null" >
          #{item.courseId,jdbcType=BIGINT},
        </if>
        <if test="#{item.updateTime} != null" >
          #{item.updateTime,jdbcType=TIMESTAMP},
        </if>
        <if test="#{item.duration} != null" >
          #{item.duration,jdbcType=VARCHAR},
        </if>
        <if test="#{item.status} != null" >
          #{item.status,jdbcType=INTEGER},
        </if>
        <if test="#{item.intro} != null" >
          #{item.intro,jdbcType=BIGINT},
        </if>
        <if test="#{item.url} != null" >
          #{item.url,jdbcType=VARCHAR},
        </if>
        <if test="#{item.root} != null" >
          #{item.root,jdbcType=BIGINT},
        </if>
        <if test="#{item.free} != null" >
          #{item.free,jdbcType=INTEGER},
        </if>
        <if test="#{item.createTime} != null" >
          #{item.createTime,jdbcType=TIMESTAMP},
        </if>
      </trim>
    </foreach>
  </insert>

dao(mapper)层写法

//传入一个list,返回受影响数(插入数), @Param("chapterList")对应xml的collection="chapterList"   
//这个方法如果传入的是null或list.size()==0,会运行时报错
int multiplyInsertSelective(@Param("chapterList") List<EpChapter> chapterList);

解释下几个标签

<foreach>
item:
    循环体中的具体对象。支持属性的点路径访问,如item.age,item.info.details。
具体说明:在list和数组中是其中的对象,在map中是value。
该参数为必选。
collection:
    要做foreach的对象,作为入参时,List<?>对象默认用list代替作为键,数组对象有array代替
作为键,Map对象没有默认的键。当然在作为入参时可以使用@Param("keyName")来设置键,设置keyName后,
list,array将会失效,如上面的 @Param("chapterList")对应xml的collection="chapterList"。
除了入参这种情况外,还有一种作为参数对象的某个字段的时候。举个例子:如果class有属性List students。
入参是User对象,那么这个collection = "students"如果class有属性student;其中
student是个对象,student有个属性List books;入参是User对象,那么collection ="student.books"
上面只是举例,具体collection等于什么,就看你想对那个元素做循环。
该参数为必选。
separator:
    元素之间的分隔符,例如在in()的时候,separator=","会自动在元素中
间用“,“隔开,避免手动输入逗号导致sql错误,如in(1,2,)这样。该参数可选。
open:
    foreach代码的开始符号,一般是(和close=")"合用。常用在in(),values()时。该参数可选。
close:
    foreach代码的关闭符号,一般是)和open="("合用。常用在in(),values()时。该参数可选。
index:
    在list和数组中,index是元素的序号,在map中,index是元素的key,一般在判断的情境下使用,该参数可选。
<if>
test:
    判断条件,如果真,执行标签里内容,例子如上
<trim>
prefix:
    在标签内容前填充指定前缀
suffix:
    在标签内容后填充指定后缀
prefixOverrides:
    去除标签内容前指定前缀,可选
suffixOverrides:
    去除标签内容后指定后缀,可选
如:<trim prefix="(" suffix=")" suffixOverrides=","  perfixOverrides=",">,1,2,3,</trim>
执行的结果是(1,2,3)


 

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值