如题所述
whereæ ç¾ä¸ºMyBatisçå¨æè¯å¥ã
<select id="selectByParams" parameterType="map" resultType="user">
select * from user
<where>
<if test="id != null ">id=#{id}</if>
<if test="name != null and name.length()>0">
and name=#{name}
</if>
<if test="gender != null and gender.length()>0">
and gender = #{gender}
</if>
</where>
</select>ããããã
ä¸è¿°ä»£ç ä¸è¥whereæ ç¾éçifå ¨ä¸æç«ï¼åä¸èµ°whereè¯å¥ã
è¥ç¬¬ä¸ä¸ªifæ ç¾éIDçå¼ä¸ºnullçè¯ï¼é£ä¹æå°åºæ¥çSQL为ï¼select * from user where name=âxxâ and gender=âxxâ
ä¼æAND/ORèªå¨å¿½ç¥æã
è¥ç´æ¥ç¨whereåå¥çè¯å¯è½ä¼å¯¼è´sqlè¯æ³é误ï¼æ¥è¯¢å¤±è´¥ã
<select id="selectByParams" parameterType="map" resultType="user">
select * from user
<where>
<if test="id != null ">id=#{id}</if>
<if test="name != null and name.length()>0">
and name=#{name}
</if>
<if test="gender != null and gender.length()>0">
and gender = #{gender}
</if>
</where>
</select>ããããã
ä¸è¿°ä»£ç ä¸è¥whereæ ç¾éçifå ¨ä¸æç«ï¼åä¸èµ°whereè¯å¥ã
è¥ç¬¬ä¸ä¸ªifæ ç¾éIDçå¼ä¸ºnullçè¯ï¼é£ä¹æå°åºæ¥çSQL为ï¼select * from user where name=âxxâ and gender=âxxâ
ä¼æAND/ORèªå¨å¿½ç¥æã
è¥ç´æ¥ç¨whereåå¥çè¯å¯è½ä¼å¯¼è´sqlè¯æ³é误ï¼æ¥è¯¢å¤±è´¥ã
温馨提示:答案为网友推荐,仅供参考