- 定义
- 判断 if
- 简化条件 where
- 循环forEach
- if + forEach
- 动态SQL可以满足实际开发中多条件查询功能。
- 动态SQL:根据数据的不同,动态的拼凑SQL语句技术。
- 拼凑技术:
- 判断 if
- 循环forEach
- 简化条件 where
场景1:通过id查询用户详情,如果id不存在查询所有。
- 功能接口的功能方法
List selectBySQL(String uid);
xml配置
场景2:通过用户名和密码查询用户详情,如果用户名和密码不存在查询所有。
- 功能接口的功能方法
public List selectBySQL2(@Param(“username”) String username,@Param(“password”) String password);
xml配置
简化条件 where
循环forEach
-
使用forEach拼凑SQL片段uid in (1,2,3)
-
步骤:
- 编写条件查询封装对象:UserVo. ids 集合
- 编写功能接口:condition
- 编写XML,拼凑条件
-
实现:
- 编写条件查询封装对象:UserVo. ids 集合
package com.czxy.ssm.vo;
import java.util.ArrayList;
import java.util.List;
public class UserVo {
private List ids = new ArrayList<>();
public List getIds() {
return ids;
}
public void setIds(List ids) {
this.ids = ids;
}
}
- 编写功能接口:condition
public List condition(UserVo userVo);
- 编写XML,拼凑条件
select * from user
'${id}'
if + forEach使用forEach拼凑,如果是字符类型,需要使用引号。
通过size可以判断list计划的元素数量
select * from user
'${id}'



