# 常用MyBatisXml之MySQL版

作者:Odboy (opens new window)

本站地址:https://blog.odboy.cn (opens new window)

# 批量插入

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.odboy.demo.mapper.DemoMapper">
    <insert id="insertBatch">
        <if test="records != null and records.size > 0">
            insert into demo_app_relation(user_id,app_name,create_time,create_user_id,create_user_name)
            values
            <foreach collection="records" item="item" separator=",">
                (#{item.userId},#{item.appName},#{item.createTime},#{item.createUserId},#{item.createUserName})
            </foreach>
        </if>
    </insert>
</mapper>
1
2
3
4
5
6
7
8
9
10
11
12
13

# 批量更新

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.odboy.demo.mapper.DemoMapper">
    <update id="updateBatch">
        <if test="records != null and records.size > 0">
            <foreach collection="records" item="item" separator=";">
                update demo_app_relation
                <set>
                    <if test="item.userId != null and item.userId != ''">
                        user_id = #{item.userId},
                    </if>
                    <if test="item.appName != null and item.appName != ''">
                        app_name = #{item.appName},
                    </if>
                </set>
                where id = #{item.id}
            </foreach>
        </if>
    </update>
</mapper>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# 批量删除

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.odboy.demo.mapper.DemoMapper">
    <delete id="deleteBatch">
        <if test="ids != null and ids.size > 0">
            delete from demo_app_relation where id in
            <foreach collection="ids" item="id" separator="," open="(" close=")">
                #{id}
            </foreach>
        </if>
    </delete>
</mapper>
1
2
3
4
5
6
7
8
9
10
11
12
最近更新: 2025-03-31
常用MyBatisXml之MySQL版

2017 - 武林秘籍   |