Jeesite 批量更新和批量插入

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_44369725/article/details/85624875

Jeesite 批量更新和批量插入

xml配置

<insert id="batchInsertList">
INSERT INTO t_dc_bug(
	zt_bug_id,
	product_id
	) VALUES 
	<foreach item="item" index="index" collection="insertBugList" separator=",">
	(
		#{item.ztBugId},
		#{item.productId}
	)
	</foreach>
</insert>
<update id="batchUpdateList">
	UPDATE t_dc_bug
	<trim prefix="set" suffixOverrides=",">
		<trim prefix="product_id =case" suffix="end,">
            <foreach collection="updateBugList" item="item" index="index"><!-- collection 要 dao 设置@param来识别 -->
                <if test="item.productId != null">
                 	when zt_bug_id = #{item.ztBugId} then #{item.productId}
                </if>
            </foreach>
        </trim>
        <trim prefix="group_id =case" suffix="end,">
            <foreach collection="updateBugList" item="item" index="index">
                <if test="item.groupId != null">
                 	when zt_bug_id = #{item.ztBugId} then #{item.groupId}
                </if>
            </foreach>
        </trim>
    </trim>
     WHERE
    <foreach collection="updateBugList"  separator="or" item="item" index="index">
        zt_bug_id = #{item.ztBugId}
    </foreach>
</update>

jeesite.properties 配置数据库

jdbc链接加allowMultiQueries=true,表示允许批量操作
db1.url=jdbc:mysql://IP地址:端口/数据库名?useUnicode=true&allowMultiQueries=true&characterEncoding=utf-8&allowMultiQueries=true

Dao层

dao层要加@param(“list”)参数给list声明

/**
  * 批量插入
  * @param bugList
  */
void batchInsertList(@Param("insertBugList") List<DcBug> insertBugList);
	
/**
  * 批量更新
  * @param updateBugList
  */
void batchUpdateList(@Param("updateBugList") List<DcBug> updateBugList);

service层

/**
  * 批量插入
  * @param bugList
  */
@Transactional(readOnly = false)
public void batchInsertList(List<DcBug> insertBugList) {
	dao.batchInsertList(insertBugList);
}
	
/**
 * 批量更新
 * @param updateBugList
 */
@Transactional(readOnly = false)
public void batchUpdateList(List<DcBug> updateBugList) {
	dao.batchUpdateList(updateBugList);
}

controller层

调用service层的方法即可

猜你喜欢

转载自blog.csdn.net/weixin_44369725/article/details/85624875