mybatis like模糊查詢,mybatis_05動態SQL_if和where

 2023-10-15 阅读 29 评论 0

摘要:? If標簽:作為判斷入參來使用的,如果符合條件,則把if標簽體內的SQL拼接上。   注意:用if進行判斷是否為空時,不僅要判斷null,也要判斷空字符串‘’; Where標簽:會去掉條件中的第一個and符號。? mybatis like模糊查

?

  • If標簽:作為判斷入參來使用的,如果符合條件,則把if標簽體內的SQL拼接上。

   注意:用if進行判斷是否為空時,不僅要判斷null,也要判斷空字符串‘’;

  • Where標簽:會去掉條件中的第一個and符號。

?

mybatis like模糊查詢。通過if和where通過判斷可以選擇那些語句來執行,那些語句不執行,生成最終SQL語句

?

在第一個底層if判斷中,SQL語句前面加上and也可以,系統會自動去掉

<resultMap id="userByresultmap" type="user"><id property="id" column="id_"></id><result property="username" column="username_"></result><result property="birthday" column="birthday_"></result><result property="sex" column="sex_"></result><result property="address" column="address_"></result>
</resultMap><select id="findUserByifwhere" parameterType="userQueryVO" resultMap="userByresultmap">select * from USER<where><if test="user!=null and user!=''"><if test="user.sex!=null and user.sex!=''">
<!—也可以寫成and sex=#{user.sex},系統會自動去掉-->sex=#{user.sex}</if><if test="user.username!=null and user.username!=''">and username LIKE "%${user.username}%"</if></if></where></select>

?

mybatis foreach,轉載于:https://www.cnblogs.com/aihuadung/p/10467550.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/138627.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息