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后面。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升