*一、if和where标签
如果传入属性,就判断相等。不传入不加对应的条件。
if
判断传入的参数,最终是否添加语句
test
属性 : 内部做比较运算,最终TRUE将标签内的sql语句进行拼接,FALSE不拼接
判断语句:“key 比较运算符 值 and | or key 比较运算符 值”
大于和小于 不推荐直接写符号-> 使用 大于(>)>
, 小于(<)<
where 标签 作用:
1. 自动添加where 关键字 , where内部有任何一个if 满足,就自动添加 where关键字,不满足就会去掉where。
2. 自动去掉多余的 and 和 or 关键字
- 接口
/**
* 根据名字或薪资查询 员工表
* @param name
* @param salary
* @return
*/
List<Employee> query(@Param("name") String name, @Param("salary") Double salary);
- xml
where
标签会自动去掉“标签体内前面多余的and/or”
<mapper namespace="com.wake.mapper.EmployeeMapper">
<select id="query" resultType="employee">
select * from t_emp
<where>
<if test="name != null">
emp_name = #{name}
</if>
<if test="salary != null and salary > 100">
and emp_salary = #{salary}
</if>
</where>
</select>
</mapper>
- 测试
@Test
public void testDynamicCondition(){
EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
List<Employee> query = employeeMapper.query(null, 147.60000);
System.out.println(query);