首页 » 编程知识 » JAVA » Mybatis的批量操作

Mybatis的批量操作

 
文章目录

batchInsert

    <!-- 批量保存用户,并返回每个用户插入的ID -->
<insert id="batchInsert" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO `test`.`tb_user`(`username`, age)
    VALUES
    <foreach collection="list" item="item" separator=",">
        (#{item.username}, #{item.age})
    </foreach>
</insert>

batchDelete


<delete id="batchDelete" parameterType="java.util.List">
    DELETE FROM `test`.`tb_user` WHERE
    <foreach collection="list" item="item" open="(" close=")" separator="or">
        (id = #{item.id} and name = {item.name})
    </foreach>
</delete>
注意:separator="or"

batchUpdate

<update id="updateOilList"  parameterType="java.util.List">  
    <foreach collection="oilList" item="item" index="index" separator=";">
        update oilno
        <set >
            <if test="item.density != null" >
                density = #{item.density},
            </if>
            <if test="item.listprice != null" >
                listprice = #{item.listprice},
            </if>
        </set>
        where oilclass = #{item.oilclass}
    </foreach>       
</update>

selectByMultiParam

    // 1.参数存在param,参数必须全使用@param
    List<Person> selectPersionByMulti(@Param("name") String name,@Param("ids") List<Integer> ids);
 // 2. open close 都得存在,collection对应为@param的值
 <select id="selectPersionByMulti" resultType="cn.creditcrest.zhongkai.ncms.model.Person">
    SELECT * FROM person WHERE NAME = #{name,jdbcType=VARCHAR} AND id IN
    <foreach collection="ids" item="item" open="(" close=")" index="index" separator=",">
      #{item,jdbcType=INTEGER}
    </foreach>

原文链接:Mybatis的批量操作,转载请注明来源!

0