第4章 MyBatis 动态SQL
前言


一. if 标签
1. 基本用法




2. 在update更新列中使用if



3. 在insert动态插入列中使用if



二. choose 标签




三. where、set、trim 标签用法

1. where 标签

2. set 标签

3. trim 标签_万能

实战

/**
* 批量更新下一轮计划时间
*
* @param nextCycleList 更新的数据源
* @return 结果
*/
public int updateCheckPlanByCodes(@Param("nextCycleList") List<CheckPlan> nextCycleList);
update ps_check_plan
<trim prefix="set" suffixOverrides=",">
<trim prefix="next_cycle =case" suffix="end,">
<foreach collection="nextCycleList" item="item" index="index">
when code=#{item.code} then #{item.nextCycle}
</foreach>
</trim>
<trim prefix="activation_status =case" suffix="end,">
<foreach collection="nextCycleList" item="item" index="index">
when code=#{item.code} then #{item.activationStatus}
</foreach>
</trim>
</trim>
where code in
<foreach collection="nextCycleList" index="index" item="item" separator="," open="(" close=")">
#{item.code}
</foreach>
解析的sql
UPDATE ps_check_plan
SET next_cycle =
CASE
WHEN CODE = "DJ24081269921248" THEN "2024-09-15 00:01:00"
WHEN CODE = "DJ24082977320152" THEN "2024-09-15 00:02:00"
WHEN CODE = "DJ24090275682393" THEN "2024-09-15 00:03:00"
END,
activation_status =
CASE
WHEN CODE = "DJ24081269921248" THEN 1
WHEN CODE = "DJ24082977320152" THEN 1
WHEN CODE = "DJ24090275682393" THEN 1
END
WHERE
CODE IN ("DJ24081269921248","DJ24082977320152","DJ24090275682393")

四. foreach

作用: 遍历dao层方法传递过来的数组、集合、javaBean对象
总结 : 遍历功能
1. foreach实现in集合

2. foreach包含的属性




3. foreach实现批量插入
批量插入javaBean集合



3.1 批量返回插入的主键

4. foreach实现动态update


五. bind用法

六. OGNL用法


七. 其他




1136




被折叠的 条评论
为什么被折叠?



