当不确定查询条件有几个的时候,用动态sql判断效果很好。采用OGN表达式。
标签会动态去掉第一个 and/or,所以不用担心第一个条件不存在,会多一个 and/or的情况。
<select id="selectByContation" resultType="com.mybatis.demo.mybatis1.bean.TAdmin">
select * from admin
<where>
<if test="id!=null">
and id = #{id}
</if>
<if test="username!=null and username.trim()!=''">
and username = #{username}
</if>
<if test="gender==0 or gender==1">
and gender= #{gender}
</if>
<if test="email!=null and email.trim()!=''">
and email= #{email}
</if>
</where>
</select>