前言
Mybatis 对于大家来说真是再熟悉不过了,ORM映射框架,目前在国内项目中备受青睐,
适合国内复杂的业务场景以及敏捷开发的需要,不过其参数的使用有事让人迷糊,今天就好好的讲一下,可以做个参考节省时间。
批量更新
#dao 层
void batchUpdateTargetData(@Param(value = "encryptListData") List encryptListData,@Param(value = "yearNum") Integer yearNum);
## 第一种更新 (优点:
1.书写方便
2.不用考虑条数限制
缺点:
1. 性能差
2. 数据库连接配置多条查询
)
update tableName_${yearNum}
prefix="set" suffixOverrides=",">
receiver_name =#{encryptData.receiverName},
receiver_address =#{encryptData.receiverAddress},
receiver_mobile =#{encryptData.receiverMobile},
receiver_phone=#{encryptData.receiverPhone}
where id = #{encryptData.id}
### 第二种更新(优点:
1.性能好
2.数据库连接不用配置多条查询
缺点:
1. 书写复杂
2. 注意每个数据库in 数据的限制
3. 建议分批
)
update tableName_${yearNum}
when id = #{encryptData.id} then #{encryptData.receiverName}
when id = #{encryptData.id} then #{encryptData.receiverAddress}
when id = #{encryptData.id} then #{encryptData.receiverMobile}
where id in
#{encryptData.id}
对应标签解释
@Param(value = "encryptListData") List encryptListData
@Param 代表着绑定字段 dao层只有一个参数可以不写,多个必须写,不然找不到对应的参数
collection ————> 当前传参传入的集合类型参数
item ————> 当前集合对应的别名,也就是下面操作的对象
open ————> 起始的标志
close ————> 关闭的标志
separator ————> 数据分割的标志
index ————> 索引可写可不写 基本用不到 可以直接写index ,
tirm 语法格式 类似Java StringBuilder append() 拼接的作用
prefix ————> 前缀
suffix ————> 后缀
suffixOverrides ————> 去掉重复的后缀 比如 or
prefixOverrides ————> 去掉重复的前缀 比如 and 和 where 一起使用不用考虑 and 的位置