mybatis xml常用写法总结

Published on 2023-08-21 14:51 in 分类: 随笔 with 狂盗一枝梅
分类: 随笔

总结下常用mybatis xml的写法

1、in语法

批量更新

<update id="batchUpdatePutwayStatus">
    update talent_course set putaway_status = #{putwayStatus} where id in
    <foreach collection="courseIds" item="value" separator="," open="(" close=")">
        #{value}
    </foreach>
</update>

批量查询

<select id="selectNoPeriodCourses" resultType="xxx">
    SELECT
        `talent_course`.*
    FROM `talent_course`
             LEFT JOIN `talent_course_chapter_period` ON `talent_course`.`id` = `talent_course_chapter_period`.`course_id`
    WHERE `talent_course`.`id` IN
    <foreach collection="courseIds" item="value" separator="," open="(" close=")">
        #{value}
    </foreach>
    GROUP BY `talent_course`.`id`
    HAVING COUNT(talent_course_chapter_period.`id`) = 0
</select>

2、sql引用

首先在xml文件中写一段如下复杂的xml

<sql id="commonQueryPageCourseUser">
        select * from demo
</sql>

在引用的地方这么写

<select id="queryPageCourseUser" resultType="xxx"
        parameterType="xxx">
    <include refid="commonQueryPageCourseUser"/>
    GROUP BY
    uc.user_id,
    uc.course_id
</select>

#mybatis
目录
复制 复制成功