Mybatis批处理—foreach的用法


Mybatis批处理(查询、更新、插入)

批量更新

Mapper.java

void UpdateImprovement(List<ImproveReportModel> list);

Mapper.xml

 <update id="UpdateImprovement" parameterType="com.tfjybj.typing.model.ImproveReportModel">
        update
        ty_improve_report
        set
        is_query=1
        where
        user_id
        in
        (
        <foreach collection="list" item="ImproveReportModel" separator=",">
            #{ImproveReportModel.userId}
        </foreach>
        )
    </update>

批量查询

Mapper.java

List<TeamGrowthChartModel> selectTeamGrowthChart(@Param("className") List<String> className);

Mapper.xml

    <select id="selectTeamRanking" resultType="com.tfjybj.typing.model.TeamRankingModel">
        SELECT
        t.team_id As teamId,
        GROUP_CONCAT(distinct
        CONCAT_WS( ",", p.`user_name` )) As userNames,
        t.team_name As teamName,
        t.group_results As teamGrade,
        p.class_name as className
        FROM
        `ty_person` p
        RIGHT JOIN `ty_team` t ON p.team_id = t.team_id
        WHERE
        t.is_delete = 0
        AND p.is_delete = 0
        <if test="className!=null and className.size() > 0">
            AND p.`class_name` in
            <foreach item="item" collection="className" index="index" open="(" separator="," close=")">#{item}</foreach>
        </if>
        GROUP BY
        t.team_id
        ORDER BY
        t.group_results DESC
    </select>

批量插入

Mapper.java

void RedisToDb(List<TeamDetailResultModel> list);

Mapper.xml

    <insert id="RedisToDb">
        INSERT INTO team_detail_result
        (id, team_id, user_name,speed,right_rate,set_time,real_time,article_name,score)
        VALUES
        <foreach collection="list" item="teamDetailResultModel" separator=",">
            (#{teamDetailResultModel.id}, #{teamDetailResultModel.teamName}, #{teamDetailResultModel.speed}, #{teamDetailResultModel.rightRate}, #{teamDetailResultModel.setTime},#{teamDetailResultModel.real_time}, #{teamDetailResultModel.articleName}, #{teamDetailResultModel.score})
        </foreach>
    </insert>

注意:这里的 in<trim prefix="(" suffix=")"> 以及 in ()的三种方式等价使用

<trim prefix="(" suffix=")"> 的用法

List<ReturnCodeEntity> selectByIds(@Param("ids") List<Long> ids);
<select id="selectByIds" parameterType="java.util.List"
            resultType="com.paic.ocss.gateway.model.entity.ReturnCodeEntity">
        SELECT
        id,
        code,
        message,
        recommendation,
        status
        FROM openapi_return_code
        WHERE id in
        <trim prefix="(" suffix=")">
            <foreach collection="ids" index="index" item="id" separator=",">
                #{id}
            </foreach>
        </trim>
    </select>

感谢阅读~

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张_Laura

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值