Mybatis批量操作demo

夢想型對手發表於2020-11-20

1.批量刪除

1.真刪除

<delete id="delSysRoles" parameterType="int">
        delete from sysrole where rid in
        <foreach collection="array" open="(" close=")" separator="," item="rid">
            #{rid}
        </foreach>
</delete>

表示刪除id在int型別陣列裡的集合
2.軟刪除

<update id="delSysUsers" parameterType="string">
        update sysuser
        set delflag='2'
        where user_id in
        <foreach item="userId" collection="array" open="(" separator="," close=")">
            #{userId}
        </foreach>
</update>

2.批量修改

跟批量軟刪除類似

3.批量插入資料

<insert id="addSysUsers" parameterType="sysuser">
        insert into sysuser(uid,uname,upwd) values
        <foreach collection="list" separator="," item="sysuer">
            (#{sysuer.uid},#{sysuer.uname},#{sysuer.upwd})
        </foreach>
</insert>

傳入的是list物件集合

4.模糊查詢,帶排序

<select id="querySysRoleList" parameterType="sysrole" resultType="sysrole">
        select *  from sysrole
        <where>
            <if test="rname!=null and rname!=''">
                rname  like  "%"#{rname}"%"
            </if>
        </where>
        order by ${sortString}
</select>

相關文章