mybatis where if and and消失

复现:

    <sql id="selectScoreScenseVo">
        select SCENSE_ID, SCENSE_TYPE, SCENSE_NAME, SCENSE_DES, SCENSE_SCORE_NAME, SCENSE_BGIMG_URL, VALID_STATUS, CREATE_TIME, UPDATE_TIME, FLAG from score_scense
    </sql>  
  <select id="selectScoreScenseList" parameterType="ScoreScense" resultMap="ScoreScenseResult">
        <include refid="selectScoreScenseVo"/>
        <where>  
            <if test="scenseType != null  and scenseType != ''"> and SCENSE_TYPE = #{scenseType}</if>
            <if test="scenseName != null  and scenseName != ''"> and SCENSE_NAME like concat('%', #{scenseName}, '%')</if>
            <if test="scenseDes != null  and scenseDes != ''"> and SCENSE_DES = #{scenseDes}</if>
            <if test="scenseScoreName != null  and scenseScoreName != ''"> and SCENSE_SCORE_NAME like concat('%', #{scenseScoreName}, '%')</if>
            <if test="scenseBgimgUrl != null  and scenseBgimgUrl != ''"> and SCENSE_BGIMG_URL = #{scenseBgimgUrl}</if>
            <if test="validStatus != null  and validStatus != ''"> and VALID_STATUS = #{validStatus}</if>
            <if test="createTime != null  and createTime != ''"> and CREATE_TIME = #{createTime}</if>
            <if test="updateTime != null  and updateTime != ''"> and UPDATE_TIME = #{updateTime}</if>
            <if test="flag != null  and flag != ''"> and FLAG = #{flag}</if>
        </where>
            order by CREATE_TIME DESC
    </select>

 

打印sql

select SCENSE_ID, SCENSE_TYPE, SCENSE_NAME, SCENSE_DES, SCENSE_SCORE_NAME, SCENSE_BGIMG_URL, VALID_STATUS, CREATE_TIME, UPDATE_TIME, FLAG from score_scense WHERE SCENSE_NAME like concat('%', ?, '%') order by CREATE_TIME DESC LIMIT ?

 

and 怎么没了?

    where标签会自动判断前面是否有字段,如果有字段会使用 and SCENSE_NAME 这个语句。

  如果没有字段,and会被忽略直接跟在where后面。

 

 

参考: mybatis if where标签怎么使用?

posted @ 2022-07-14 16:32  BBS_自律  阅读(285)  评论(0)    收藏  举报