文章目录
前言`
动态 SQL是MyBatis强大特性之一。极大的简化我们拼装SQL的操作。
一、动态SQL是什么?
- 动态 SQL 元素和使用 JSTL 或其他类似基于 XML 的文本处 理器相似。
- MyBatis 采用功能强大的基于 OGNL 的表达式来简化操作。
– if
– choose (when, otherwise)
– trim (where, set)
– foreach
二、使用动态SQL
1.if标签–单条件判断
//如果name不为null则按照name查询 如果为null则查询所有
public List<Account> findById(@Param("name")String name,@Param("money") Double money);
<select id="findById" resultType="com.zcp.entity.Emp">
select * from tb_emp where 1=1
<if test="name!=null and name!=''">
and name=#{name}
</if>
<if test="money!=null">
and money=#{money}
</if>
</select>
2.choose标签–多条件分支判断
<select id="findById02