-- DUAL 方式批量插入
<insert id="batchInsert" parameterType="java.util.List"> INSERT INTO 表A(字段A,字段B) <foreach collection="list" index="" item="别名A" separator="union all"> (select #{别名A.ckht},#{别名A.B} from dual) </foreach> </insert>
oreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。foreach元素的属性主要有 item,index,collection,open,separator,close。item表示集合中每一个元素进行迭代时的别名,index指 定一个名字,用于表示在迭代过程中,每次迭代到的位置,open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为分隔 符,close表示以什么结束,在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况 下,该属性的值是不一样的,主要有一下3种情况:
如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
如果传入的参数是多个的时候,我们就需要把它们封
<insert id="batchInsert" parameterType="java.util.List"> INSERT INTO 表A (EXAMINE_STATUS,BANK_ACCOUNT) <foreach collection="list" index="" item="test" separator="union all"> (select #{test.examineStatus},#{test.bankAccount} from dual) </foreach> </insert> <update id="batchUpdate" parameterType="java.util.List"> <foreach collection="list" item="item" index="index" open="begin" close=";end;" separator=";"> <if test="item.nsrsbh!=null and item.nsrsbh!='' and item.taxCreditRating!=null"> update 表A set TAX_CREDIT_RATING = #{item.taxCreditRating} where NSRSBH=#{item.nsrsbh} </if> </foreach> </update> <delete id="deleteByIds" parameterType="java.lang.String"> delete from 表A where 字段a in <foreach collection="array" item="ckht" index="index" open="(" close=")" separator=","> #{ckht} </foreach> </delete>
mybatis 批量操作表
最新推荐文章于 2022-10-11 18:00:11 发布