mybatis注释函数,其中uuids列表中 我有一个用户列表,我需要以给定金额减少许多黄金 Mybatis功能 @更新( ”“” 更新用户 设置金=金 - #{量}

问题描述 投票:0回答:1

这是DB的外观(简化)

create TABLE IF NOT EXISTS users ( pk INT PRIMARY KEY AUTO_INCREMENT, uuid VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, email VARCHAR(255) NOT NULL, gold INT NOT NULL, unique (uuid), unique (email) );

每次我有下一个错误时,我的功能很舒服:
07:59:14.340 DEBUG c.s.g.d.d.U.reduceUsersGold - ==>  Preparing: UPDATE users SET gold = gold - ? <where> gold >= ? AND uuid IN <foreach collection="uuids" item="uuid" open="(" separator="," close=")" > ? </foreach> </where>
### Error updating database.  Cause: org.apache.ibatis.binding.BindingException: Parameter 'uuid' not found. Available parameters are [amount, param1, uuids, param2]
### The error may exist in com/simple/games/data/dao/UserDao.java (best guess)
### The error may involve com.simple.games.data.dao.UserDao.reduceUsersGold-Inline
### The error occurred while setting parameters
### SQL: UPDATE users         SET gold = gold - ?         <where>             gold >= ?             AND uuid IN              <foreach                  collection="uuids" item="uuid" open="(" separator="," close=")" > ?             </foreach>         </where>
### Cause: org.apache.ibatis.binding.BindingException: Parameter 'uuid' not found. Available parameters are [amount, param1, uuids, param2]

要在注释中使用动态标签,例如

<where>

<foreach>
,您需要将SQL包装在
kotlin annotations mybatis
1个回答
0
投票
.

中。 因此,看起来如下:

@Update(
    """
    <script>
    UPDATE users
    SET gold = gold - #{amount}
    <where>
        gold >= #{amount}
        AND uuid IN 
        <foreach 
            collection="uuids" item="uuid" open="(" separator="," close=")" > #{uuid}
        </foreach>
    </where>
    </script>
"""
)
fun reduceUsersGold(
    @Param("uuids") uuids: List<String>,
    @Param("amount") amount: Int,
): Int
the是文档:
https://mybatis.org/mybatis-3/dynamic-sql.html#script


最新问题
© www.soinside.com 2019 - 2024. All rights reserved.