Mybatis--动态SQL(choose--when--oth

2022-07-30  本文已影响0人  何以解君愁

choose--when--otherwise:相当于if...else if...else
(与if标签的区别:用if时,条件都要执行,choose,when,otherwise只要一个满足条件后面就不再执行因此不需要and)

<select id="getEmpBycondition" resultType="Emp">
    select*from t_emp
    <trim prefix=where>
        <choose>
            <when test="empName != null and empName != ''">
                emp_name =#{empName} 
            </when>
            <when test="age != null and age != "'">
                age = #{age} 
            </when>
            <when test="empName != null and empName != ''">
                emp_name =#{ empName} 
            </when>
            <otherwise>
                id=1
            </otherwise>
        </choose>
    </trim>
</select>
上一篇 下一篇

猜你喜欢

热点阅读