一、问题
在Mybatis中如何使用if-else判断条件?
二、解答
在Java代码中,我们使用if–else来判断某种情况–其他情况,在Mybatis中也有这样的逻辑判断;
mybatis中使用的是
<select id="selectUserBySts" resultMap="BaseResultMap" parameterType = "java.util.Map">
select
name
from USER
<where>
<if test="userId!=null&&userId!="" ">
and USER_ID = #{userId,jdbcType=VARCHAR}
</if>
<choose>
<when test = "userSts!=null&&userSts!="" ">
and USER_STS = #{userSts,jdbcType=VARCHAR}
</when>
<otherwise>
and USER_STS in("01","02")
<otherwise>
</choose>
</where>
</select>
当用户的状态userSts不为空,并且不为“”的时候,我们使用when里面的判断条件,否则,使用otherwise标签里面的判断条件;
PS:Oracle语句中有个in,表示传入的数据在in后小括号里的某一种;适合数据量比较小的查询,数据量太大会非常影响效率;
开源不易,欢迎扫码领红包: