mybatis批量插入和批量修改删除

第一, 是组装自己的的LIST 传入参数,再循环得到对象

先贴 xml代码   

update 的时候, mysql数据库, 一定要加上双线单引号,否则会报错,找不到数据库属性

<!-- 增加SNMP -->
<insert id="insertAllSnmp" parameterType="java.util.List">
    insert into tb_device_snmp_new(id,community,ip,fac_id,fac_name,region_id,region_name,createTime,benable,port,poll_time)
    values
    <foreach collection="list" item="snmp" index="index" separator=",">
        (
        #{snmp.id},#{snmp.community},#{snmp.ip},#{snmp.fac_id},#{snmp.fac_name},#{snmp.region_id},
        #{snmp.region_name},#{snmp.createTime},#{snmp.benable},#{snmp.port},#{snmp.poll_time}
        )
    </foreach>
</insert>

<update id="updateSnmp" parameterType="java.util.List">

        <foreach collection="list" separator=";" item="cus">
            update tb_device_snmp_new
            set
            `community`=#{cus.community},
            `ip`=#{cus.ip},
            `fac_id`=#{cus.fac_id},
            `fac_name`=#{cus.fac_name},
            `region_id`=#{cus.region_id},
            `region_name`=#{cus.region_name},
            `createTime`=#{cus.createTime},
            `benable`=#{cus.benable},
            `port`=#{cus.port},
            `poll_time`=#{cus.poll_time}
            where `id` = #{cus.id}
        </foreach>

    </update>

<delete id="deleteSnmp" parameterType="java.util.List">
    DELETE FROM tb_client_flow_port WHERE id in
    <foreach collection="list" item="item" open="(" separator="," close=")">
        #{item.id}
    </foreach>

</delete>

这种条件也是可以的

jfinal 条件原因最终批量更新不行  只能循环方法体

        JSONObject save = (JSONObject) obj;
                    int id = save.getInteger("id");
                    String community = save.getString("community");
                    String fac_id = save.getString("fac_id");
                    String fac_name = save.getString("fac_name");
                    String region_name = save.getString("region_name");
                    String region_id = save.getString("region_id");
                    String ip = save.getString("ip");
                    String port = save.getString("port");
                    String benable = save.getString("benable");
                    String poll_time = save.getString("poll_time");
                    String createTime = utils.getLocalTime();
                    snmpMapper.updateSnmp(id, community, fac_id, fac_name, region_name, region_id, ip, createTime, benable, port,
                            poll_time);
                });

    public void updateSnmp(@Param("id") int id, @Param("community") String community, @Param("fac_id") String fac_id,
                           @Param("fac_name") String fac_name, @Param("region_name") String region_name,
                           @Param("region_id") String region_id, @Param("ip") String ip, @Param("createTime") String createTime,
                           @Param("benable") String benable, @Param("port") String port, @Param("poll_time") String poll_time);


    <update id="updateSnmp" parameterType="map">
        update tb_device_snmp_new
        <trim prefix="set" suffixOverrides=",">
            <if test="community!=null">community=#{community},</if>
            <if test="ip!=null">ip=#{ip},</if>
            <if test="fac_id!=null">fac_id=#{fac_id},</if>
            <if test="fac_name!=null">fac_name=#{fac_name},</if>
            <if test="region_id!=null">region_id=#{region_id},</if>
            <if test="region_name!=null">region_name=#{region_name},</if>
            <if test="createTime!=null">createTime=#{createTime},</if>
            <if test="benable!=null">benable=#{benable},</if>
            <if test="port!=null">port=#{port},</if>
            <if test="poll_time!=null">poll_time=#{poll_time},</if>
        </trim>
        <where>
            id = #{id}
        </where>
    </update>


---------------------

 update tb_device_snmp_new
            <trim prefix="set" suffixOverrides=",">
                <if test="community!=null">community=#{community},</if>
                <if test="ip!=null">ip=#{ip},</if>
                <if test="fac_id!=null">fac_id=#{fac_id},</if>
                <if test="fac_name!=null">fac_name=#{fac_name},</if>
                <if test="region_id!=null">region_id=#{region_id},</if>
                <if test="region_name!=null">region_name=#{region_name},</if>
                <if test="createTime!=null">createTime=#{createTime},</if>
                <if test="benable!=null">benable=#{benable},</if>
                <if test="port!=null">port=#{port},</if>
                <if test="poll_time!=null">poll_time=#{poll_time},</if>
            </trim>
            <where>
                id = #{id}
            </where>

第二步, 映射写的文件方法参数 

public int insertAllSnmp(@Param("list") List obj);

public int deleteSnmp(@Param("list") List obj);

public void updateSnmp(@Param("list") List objs);

 

第三步, 后台调用参数了


传入参数都是 JSONArray
JSONArray addList = new JSONArray();
snmpMapper.insertAllSnmp(addList);

其它一些更有用的方法

public List<User> findByIdList(@Param("idList")List<Integer> idList);
<select id="findByIdList" resultType="com.example.bean.User">
	SELECT id, name, password, address, enable
	FROM user
	<where>
        <if test="idList!= null and idList.size() > 0">
            id IN
            <foreach collection="idList" item="ietm" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
	</where>
</select>

查询再删除的操作等

   <insert id="AddClientFlowGroup" parameterType="com.zenlayer.ad.mapper.SnmpMapper">
        <selectKey keyProperty="count" resultType="int" order="BEFORE">
            select count(*) from tb_client_flow_group where id = #{id}
        </selectKey>
        <if test="count > 0">
            update tb_client_flow_group
            <trim prefix="set" suffixOverrides=",">
                <if test="name!=null">name=#{name},</if>
                <if test="status!=null">status=#{status},</if>
                <if test="client_id!=null">client_id=#{client_id},</if>
            </trim>
            <where>
                id = #{id}
            </where>
        </if>
        <if test="count==0">
            insert into tb_client_flow_group(id,name,status,client_id)
            values (#{id},#{name},#{status},#{client_id})
        </if>
    </insert>

 

©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值