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 @   BBS_自律  阅读(272)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
点击右上角即可分享
微信分享提示