mybatis动态sql-新增与更新

本文详细展示了如何在MyBatis中使用动态SQL进行用户数据的插入和更新操作,包括if条件判断和参数化占位符的应用。

记录一个简单的mybatis动态sql例子

新增

<?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="org.ruiskey.mapper.UserMapper">
    <insert id="save">
        insert into user
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="id != null and id != ''">
                    id,
                </if>
                <if test="username != null and username != ''">
                    username,
                </if>
                <if test="password != null and password != ''">
                    password,
                </if>
                <if test="nickname != null and nickname != ''">
                    nickname,
                </if>
                <if test="email != null and email != ''">
                    email,
                </if>
                <if test="phone != null and phone != ''">
                    phone,
                </if>
                <if test="address != null and address != ''">
                    address
                </if>
            </trim>
            <trim prefix="values (" suffix=")" suffixOverrides=",">
                <if test="id != null and id != ''">
                    #{id},
                </if>
                <if test="username != null and username != ''">
                    #{username},
                </if>
                <if test="password != null and password != ''">
                    #{password},
                </if>
                <if test="nickname != null and nickname != ''">
                    #{nickname},
                </if>
                <if test="email != null and email != ''">
                    #{email},
                </if>
                <if test="phone != null and phone != ''">
                    #{phone},
                </if>
                <if test="address != null and address != ''">
                    #{address}
                </if>
            </trim>
    </insert>
</mapper>

更新

<update id="update">
    update user
    <set>
        <if test="username != null and username != ''">
            username = #{username}
        </if>
        <if test="password != null and password != ''">
            password = #{password}
        </if>
        <if test="nickname != null and nickname != ''">
            nickname = #{nickname}
        </if>
        <if test="email != null and email != ''">
            email = #{email}
        </if>
        <if test="phone != null and phone != ''">
            phone = #{phone}
        </if>
        <if test="address != null and address != ''">
            address = #{address}
        </if>
    </set>
    <where>
        id = #{id}
    </where>
</update>
<think>好的,我现在需要回答用户关于如何使用MyBatis动态SQL实现批量插入的问题。用户已经提供了几个引用内容,我需要仔细分析这些引用,找到相关的信息,并组织成一个清晰的回答。 首先,查看用户提供的引用。引用[1]提到MyBatis动态SQL功能,特别是<foreach>标签,用于遍历集合生成SQL语句。引用[3]也强调了<foreach>在批量插入和动态更新中的应用。引用[4]则给出了Mapper接口和XML配置的示例,其中有一个批量插入的方法定义。这些引用都指向使用<foreach>标签来处理批量插入。 接下来,我需要回忆MyBatis的基本用法。批量插入通常涉及在INSERT语句中插入多个值,而<foreach>标签可以用来遍历集合,生成多个VALUES子句。例如,INSERT INTO table (col1, col2) VALUES (a1, a2), (b1, b2)...这样的结构。 根据引用[4],Mapper接口中定义了一个方法batchInsert,参数是一个用户列表,并用@Param注解指定了名称。对应的XML文件中,应该使用<insert>标签,并在VALUES部分使用<foreach>来循环遍历users集合,生成多个(#{item.col1}, #{item.col2})。 需要注意的点包括:在SQL语句中正确的占位符写法,比如使用#{item.属性名},并且确保<foreach>的collection属性@Param注解的名称一致。另外,separator属性应该设置为",",以便在每个值之间添加逗号分隔。 此外,可能还需要考虑数据库连接配置是否允许批量操作,比如在JDBC URL中添加rewriteBatchedStatements参数以提高性能,但用户的问题主要是关于动态SQL的实现,所以这部分可能不需要详细展开,但可以作为优化点提及。 现在需要将步骤分点说明,先给出接口定义,再展示XML配置,并提供代码示例。同时,要解释各个部分的作用,比如@Param注解、<foreach>的属性含义等。最后,可以补充注意事项,比如集合名称一致性和批量插入的性能优化。 需要确保回答结构清晰,步骤明确,让用户能够按照示例实现批量插入功能。同时,要使用中文,并正确应用LaTeX格式,比如变量名用$...$包裹,但根据用户要求,可能在这个问题中涉及代码示例较多,数学公式较少,但如果有变量参数名可能需要用行内格式。 最后,生成相关问题,可能涉及其他动态SQL标签、性能优化方法,批量插入相关的其他操作,比如批量更新删除。</think>### MyBatis动态SQL实现批量插入的方法 以下是使用MyBatis动态SQL实现批量插入的完整步骤及示例代码: #### 1. Mapper接口定义 在Mapper接口中添加批量插入方法,使用`@Param`注解指定集合参数名称: ```java public interface UserMapper { void batchInsert(@Param("users") List<User> users); } ``` 说明:`@Param("users")`表示XML中通过`users`引用该集合[^4]。 #### 2. XML映射文件配置 在Mapper XML中使用`<foreach>`标签遍历集合生成SQL: ```xml <insert id="batchInsert"> INSERT INTO user (name, age, email) VALUES <foreach collection="users" item="item" separator=","> (#{item.name}, #{item.age}, #{item.email}) </foreach> </insert> ``` - `collection="users"`:对应接口方法中的`@Param("users")`[^4] - `item="item"`:集合元素的临时变量名 - `separator=","`:每次循环后添加逗号分隔符[^3] #### 3. Java调用示例 ```java List<User> userList = new ArrayList<>(); // 添加多个User对象到列表 userMapper.batchInsert(userList); ``` #### 4. 最终生成的SQL 若插入3条记录,生成的SQL为: ```sql INSERT INTO user (name, age, email) VALUES ('张三', 25, 'zhangsan@example.com'), ('李四', 30, 'lisi@example.com'), ('王五', 28, 'wangwu@example.com') ``` ### 注意事项 1. **集合名称一致性**:XML中的`collection`值必须`@Param`注解的名称完全一致 2. **批量优化**:大数据量时建议在JDBC URL中添加`rewriteBatchedStatements=true`启用批量优化 3. **事务控制**:批量操作应在事务中执行以保证原子性 ### 其他实现方式 对于**特别大的数据集**(如10万+),可采用分批次批量插入: ```xml <insert id="batchInsert"> <foreach collection="users" item="item" separator=";"> INSERT INTO user (name, age, email) VALUES (#{item.name}, #{item.age}, #{item.email}) </foreach> </insert> ``` 此方式生成多条INSERT语句,需确保数据库支持多语句执行[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值