mybatis 批量插入、批量修改多个字段、批量更新状态-Mybatis

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 16:13   791   0

转自:https://blog.csdn.net/han12398766/article/details/85240815

[Mybatis]

1、批量插入

Mapper接口

void saveHyTemplate(List<HyTemplate> hyTemplate);

Mapper配置文件(xml)

<insert id="saveHytemplate" parameterType="java.util.List">
INSERT INTO hy_template (ID,TEMPLATE_ID,ITEM_NAME,SORT_CODE,
ITEM_TYPE,MODEL_NAME,SCORE,PARENT_ID,LEVEL,MEMO,REVISION,CREATED_BY,
CREATED_TIME,UPDATED_BY,UPDATED_TIME,SYSTEM_RULE)
VALUES
<foreach collection="list" item="hytemplate" separator=",">
(#{hytemplate.id,jdbcType=VARCHAR},
#{hytemplate.templateId,jdbcType=VARCHAR},
#{hytemplate.itemName,jdbcType=VARCHAR},
#{hytemplate.sortCode,jdbcType=VARCHAR},
#{hytemplate.itemType,jdbcType=VARCHAR},
#{hytemplate.modelName,jdbcType=VARCHAR},
#{hytemplate.score,jdbcType=VARCHAR},
#{hytemplate.parentId,jdbcType=VARCHAR},
#{hytemplate.level,jdbcType=VARCHAR},
#{hytemplate.memo,jdbcType=VARCHAR},
#{hytemplate.revision,jdbcType=VARCHAR},
#{hytemplate.createdBy,jdbcType=VARCHAR},
#{hytemplate.createdTime,jdbcType=VARCHAR},
#{hytemplate.updatedBy,jdbcType=VARCHAR},
#{hytemplate.updatedTime,jdbcType=VARCHAR},
#{hytemplate.systemRule,jdbcType=VARCHAR})
</foreach>
</insert>

2、批量修改

mysql的URL配置添加 allowMultiQueries=true 否则无法支持批量修改语句

// 请勿复制下面配置,例子展示,仅供参考:
jdbc:mysql:///quality_check?characterEncoding=utf8&useSSL=false&allowMultiQueries=true

Mapper接口

/*
* 批量修改质检项
*/
Long updateHytemplateBatch(@Param(value = "hytemplateList")List<Hytemplate> hytemplateList);

Mapper配置文件(xml)

<update id="updatehytemplateBatch" parameterType="java.util.List">
<foreach collection="hytemplateList" item="hytemplate" index="index" open="" close="" separator=";">
UPDATE hy_template
<set>
<if test="hytemplate.templateId != null and hytemplate.templateId != ''">
TEMPLATE_ID = #{hytemplate.templateId,jdbcType=VARCHAR},
</if>
<if test="hytemplate.itemName != null and hytemplate.itemName != ''">
ITEM_NAME = #{hytemplate.itemName,jdbcType=VARCHAR},
</if>
<if test="hytemplate.sortCode != null and hytemplate.sortCode != ''">
SORT_CODE = #{hytemplate.sortCode,jdbcType=VARCHAR},
</if>
<if test="hytemplate.itemType != null and hytemplate.itemType != ''">
ITEM_TYPE = #{hytemplate.itemType,jdbcType=VARCHAR},
</if>
<if test="hytemplate.modelName != null and hytemplate.modelName != ''">
MODEL_NAME = #{hytemplate.modelName,jdbcType=VARCHAR},
</if>
<if test="hytemplate.score != null and hytemplate.score != ''">
SCORE = #{hytemplate.score,jdbcType=VARCHAR},
</if>
<if test="hytemplate.parentId != null and hytemplate.parentId != ''">
PARENT_ID = #{hytemplate.parentId,jdbcType=VARCHAR},
</if>
<if test="hytemplate.level != null and hytemplate.level != ''">
LEVEL = #{hytemplate.level,jdbcType=VARCHAR},
</if>
<if test="hytemplate.memo != null and hytemplate.memo != ''">
MEMO = #{hytemplate.memo,jdbcType=VARCHAR},
</if>
<if test="hytemplate.revision != null and hytemplate.revision != ''">
REVISION = #{hytemplate.revision,jdbcType=VARCHAR},
</if>
<if test="hytemplate.createdBy != null and hytemplate.createdBy != ''">
CREATED_BY = #{hytemplate.createdBy,jdbcType=VARCHAR},
</if>
<if test="hytemplate.createdTime != null and hytemplate.createdTime != ''">
CREATED_TIME = #{hytemplate.createdTime,jdbcType=VARCHAR},
</if>
<if test="hytemplate.updatedBy != null and hytemplate.updatedBy != ''">
UPDATED_BY = #{hytemplate.updatedBy,jdbcType=VARCHAR},
</if>
<if test="hytemplate.updatedTime != null and hytemplate.updatedTime != ''">
UPDATED_TIME = #{hytemplate.updatedTime,jdbcType=VARCHAR},
</if>
<if test="hytemplate.systemRule != null and hytemplate.systemRule != ''">
SYSTEM_RULE = #{hytemplate.systemRule,jdbcType=VARCHAR}
</if>
</set>
<where>
ID = #{hytemplate.id,jdbcType=VARCHAR}
</where>
</foreach>
</update>

3、批量修改状态

Mapper接口

void updateRuleBoById(RuleBo ruleBo);

注意:我的RuleBo里面有存储一个集合:private List<String> ruleIds;//规则Id集合

Mapper配置文件(xml)

<update id="updateRuleBoById" parameterType="com.hbsc.domain.RuleBo">
UPDATE ruleBo
<set>
Status = #{status,jdbcType=VARCHAR}
</set>
<where>
ID IN
<foreach collection="ruleIds" index="index" item="id" separator="," open="(" close=")">
#{id,jdbcType=VARCHAR}
</foreach>
</where>
</update>
————————————————

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP