mybatis 多条update同时执行

23 篇文章 1 订阅

想在mapper的一个更新节点进行多条update语句的操作:

<update id="cleanUserByPhone" parameterType="java.lang.String">
  update user set valid_status = 1 where mobile_phone = #{mobilePhone};
  update user_account set valid_status = 1 where mobile_phone = #{mobilePhone} ;
</update>

foreach 方式断句,用分号分隔每个update语句,注意<> 等需要转义的符号

<update id="updateMore" parameterType="java.util.List">
        <foreach collection="articleList" item="item" index="index" open="" close="" separator=";">
            update article
            <set>
                <if test="item.contentAuthor != null">content_author = #{item.contentAuthor}, </if>
                <if test="item.contentTitle != null">content_title = #{item.contentTitle}, </if>
                <if test="item.contentLink != null"> content_link = #{item.contentLink}, </if>
                update_date = #{item.updateDate}
            </set>
            where article_id = #{item.articleId} and staff_id = #{item.staffId} and status &lt; 5
        </foreach>
    </update>

 

mybatis是默认不支持多条语句拼接插入或更新的,需要在数据库配置中配置相关参数  以允许使用分号分隔sql语句:

propertes 或者yml配置 文件中的jdbc后追加&allowMultiQueries=true​​​​​​​

application-prod.yml 数据源配置参考

spring:
    # 数据源配置
    datasource:
        type: com.alibaba.druid.pool.DruidDataSource
        driverClassName: com.mysql.cj.jdbc.Driver
        druid:
            # 主库数据源
            master: #localhost
                url: jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
                username: root
                password: aaaa@123

参考:

springboot+Mybatis+MySql 一个update标签中执行多条update sql语句_yjl33的博客-CSDN博客

  • 6
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
MyBatis 中,可以使用 `Mapper` 接口或者 `SqlSession` 直接执行多条 SQL 语句。 1. Mapper 接口方式 如果你使用的是 `Mapper` 接口方式,那么可以在 Mapper 接口中定义多个方法,每个方法对应一个 SQL 语句。然后在 Java 代码中调用这些方法即可执行多条 SQL 语句。 例如: ``` public interface UserMapper { void insertUser(User user); void updateUser(User user); void deleteUser(int userId); } ``` 在 Java 代码中,可以依次调用这三个方法来执行多条 SQL 语句: ``` UserMapper mapper = sqlSession.getMapper(UserMapper.class); mapper.insertUser(user); mapper.updateUser(user); mapper.deleteUser(userId); ``` 2. SqlSession 方式 如果你使用的是 `SqlSession` 方式,那么可以调用 `SqlSession` 的 `selectList` 方法来执行多条 SQL 语句。在这个方法中,可以传入一个包含多条 SQL 语句的 XML 文件,并且在每条 SQL 语句之间使用分号分隔。 例如,假设你的 XML 文件名为 `multiSql.xml`,内容如下: ``` <sql> INSERT INTO user (name, age) VALUES ('John', 25); </sql> <sql> UPDATE user SET name = 'Peter' WHERE age = 25; </sql> ``` 那么在 Java 代码中,可以这样调用 `selectList` 方法来执行这两条 SQL 语句: ``` String statement = "multiSql"; sqlSession.selectList(statement); ``` 注意,在使用 `SqlSession` 执行多条 SQL 语句时,需要将 `ExecutorType` 设置为 `BATCH`,这样可以让 MyBatis 将多个 SQL 语句一起提交到数据库中,从而提高执行效率。例如: ``` SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); ```
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值