Mybatis 动态 sql
元素
| 元素 | 作用 | 备注 |
|---|---|---|
if |
判断语句 | 单条件分支判断 |
choose、when、otherwise |
相当于 java 的 case when | 多条件分支判断 |
Trim、where、set |
辅助元素 | 用于处理 sql 拼装问题 |
foreach |
循环语句 | 在 in 语句等列举条件常用,常用于实现批量操作 |
if
<select id="selectIfOper" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from t_user a
where 1=1
<if test="email != null and email != ''">
and a.email like CONCAT('%', #{email}, '%')
</if>
<if test="sex != null ">
and a.sex = #{sex}
</if>
</select>
不够优雅,这种情况下应该使用 where 标签。
where
批量操作
- 通过 foreach 动态拼装 SQL 语句
- 使用 BATCH 类型的 excutor

浙公网安备 33010602011771号