Solution Mybatis xml query condition and the first condition is empty

When using mybatis write SQL statements, frequently encountered conditions to the value of the parameter to query different fields, to make a summary here:

  <where>
    <!-- 判断条件参数是否为空 -->
    <if test="orgId != 'null' and orgId != ''">
        and user.org_id = #{orgId}
    </if>
     <!-- 判断条件参数是否为某值 -->
    <if test="searchCon == 'cn'">
        and opxuser.name like CONCAT(#{searchCon},'%') 
    </if>
  </where>

As used herein, Tags can be avoided for the first query SQL error empty case,
more elegant than adding 1 = 1 is written in the back where.

Guess you like

Origin www.cnblogs.com/yzhu/p/10981011.html
Recommended