MyBatis动态SQL<choose>标签的使用

来源:互联网 发布:中国大数据信息 编辑:程序博客网 时间:2024/05/19 19:15

需求:查询的时候,带了带了哪个条件就用哪个条件进行查询,条件之间互斥

可以通过<choose>标签实现:

<select id="getEmpsByConditionChoose" resultType="com.test.beans.Employee">        SELECT * FROM tb1_emplyee        <where>        /*带了那个字段就通过哪个字段进行查询*/        <choose>            <when test="id!=null">              id=#{id}            </when>            <when test="lastName!=null">                last_name like #{lastName}            </when>            <when test="email!=null">                email=#{email}            </when>            <otherwise>                1=1            </otherwise>        </choose>        </where>    </select>


原创粉丝点击