日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

mybatis if test 用法_SpringBoot整合Mybatis-Plus 实战之动态SQL,Mybatis拿得出手的功能之一...

發(fā)布時(shí)間:2025/4/17 javascript 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mybatis if test 用法_SpringBoot整合Mybatis-Plus 实战之动态SQL,Mybatis拿得出手的功能之一... 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

MyBatis的動態(tài)SQL是最令人喜歡的功能

在了解 動態(tài)SQL之前,你首先得知道一個(gè)表達(dá)式 OGNL,這個(gè)是基礎(chǔ)!

  • 面試常問問題 : Mybatis 中$與#的區(qū)別?
  • 是將傳入的值當(dāng)做字符串的形式,select id,name,age from test where id =#{id},
    當(dāng)把id值傳入到后臺的時(shí)候,就相當(dāng)于 select id,name,age from test where id =‘1’.
  • ""是將傳入的數(shù)據(jù)直接顯示生成sql語句,selectid,name,agefromtestwhereid="是將傳入的數(shù)據(jù)直接顯示生成sql語句,select id,name,age from test where id = "是將傳入的數(shù)據(jù)直接顯示生成sql語句,selectid,name,agefromtestwhereid={id},
    當(dāng)把id值1,傳入到后臺的時(shí)候,就相當(dāng)于 select id,name,age from test where id = 1.
  • 使用#可以很大程度上防止sql注入。(語句的拼接)

if 標(biāo)簽

  • mapper
    select from test where 1=1 and username like concat('%', #{username}, '%') and ip=#{ip}
  • 在mapper 接口中映射這個(gè)方法
    List selectByTestSelective(Test example);

下面每個(gè)標(biāo)簽都會有對應(yīng)的方法,但下文沒有一一寫出,現(xiàn)參考如下

List<Test> selectByExample(TestExample example); List<Test> selectByTestSelective(Test example); List<Test> selectByIdOrUserName(Test example); List<Test> selectByTestSelectiveWhereTag(Test example); List<Test> selectByTestIdList(List<Integer> ids); int insertList(List<Test> students); int updateTestSetTag(Test example); int selectSelectiveTrim(Test example);
  • 測試
@RequestMapping(value = "/dongtaiSql") @ResponseBody public void dongtaiSql() {Test example = new Test();example.setUsername("周");List<Test> selectByTestSelective = testMapper.selectByTestSelective(example);for (Test test : selectByTestSelective) {System.out.println(test.getUsername());} }
  • 打印結(jié)果

  • 也就是說,你傳什么值 它會根據(jù)你傳的值來拼接sql,不傳值則不拼接,這種相對來說比較簡單,易于理解。

【注意】 下文所有的請求都是通過postman發(fā)出的。

include標(biāo)簽

  • 一個(gè)非常好用的輔助性標(biāo)簽,用于放一些公共的返回結(jié)果集,方便其他的查詢方法使用,比如在mapper中使用方式如下:
    username, lastloginTime select from test where id = #{id,jdbcType=BIGINT}

choose標(biāo)簽 ,配合when ,otherwise 標(biāo)簽使用

choose when otherwise 標(biāo)簽可以幫我們實(shí)現(xiàn) if else 的邏輯。一個(gè) choose 標(biāo)簽至少有一個(gè) when,
最多一個(gè)otherwise。
  • mapper
    select from test where 1=1 and id=#{id} and username=#{username} and 1=2
  • 打印結(jié)果

找不到 周 ,因?yàn)槲抑挥兄芙軅惢蛘咧芙?。 這個(gè)choose和 if 的功能有點(diǎn)類似,但是和if 不同的是choose 有點(diǎn)你有什么我就根據(jù)你給的查,而if 則是你傳了所有條件,我逐個(gè)判斷你的條件然后給你查。if 更多適用于表單查詢的時(shí)候用。而choose 更多的時(shí)候。。。其實(shí)這兩個(gè)達(dá)到的目的是一樣的,我更喜歡用choose.

where 標(biāo)簽

  • mapper
    select from test and username like concat('%', #{username}, '%') and ip=#{ip}
  • 結(jié)果

  • 我什么條件也沒傳,他在where中找不到匹配的條件就查找了全部給了我,這種其實(shí)和上面choose 中的最后那個(gè)條件有異曲同工之處,上變改成1=1 一樣的效果。

foreach 標(biāo)簽

  • mapper
    select from test where id in #{id}
  • 代碼
@RequestMapping(value = "/dongtaiSql3") @ResponseBody public void dongtaiSql3() {ArrayList<Integer> arrayList = new ArrayList<Integer>();arrayList.add(6);arrayList.add(5);List<Test> selectByTestSelective = testMapper.selectByTestIdList(arrayList);for (Test test : selectByTestSelective) {System.out.println(test.getUsername());} }
  • 結(jié)果

  • 這個(gè)標(biāo)簽太好用了,foreach 也可以用來批量插入數(shù)據(jù),比如:
  • mapper
    insert into test(username, gender, ip) values ( #{test.username}, #{test.gender},#{test.ip} )
  • 代碼
Test example2 = new Test();example2.setUsername("郭富城");example2.setGender(1);example2.setIp("123232113122");Test example3 = new Test();example3.setUsername("邱淑貞");example3.setGender(0);example3.setIp("123232113333");ArrayList<Test> arrayList = new ArrayList<Test>();arrayList.add(example);arrayList.add(example2);arrayList.add(example3);int selectByTestSelective = testMapper.insertList(arrayList);if (selectByTestSelective == 1) {System.out.println("批量插入:"+arrayList.size()+"條數(shù)據(jù)");} }
  • 結(jié)果

  • .........這里的mapper 每次修改都要重新啟動,很是麻煩。注意這里 #{test.username}, #{test.gender},#{test.ip} 最后不要有逗號,否則會報(bào)一個(gè)sql語法錯(cuò)誤,原因是多了,。還有就是這里如果傳的值是list等非實(shí)體類的參數(shù)的時(shí)候,是不用聲明parameterType 的。
  • foreach 的變量說明
    collection: 必填, 集合/數(shù)組/Map的名稱. item: 變量名。即從迭代的對象中取出的每一個(gè)值 index: 索引的屬性名。當(dāng)?shù)膶ο鬄?Map 時(shí), 該值為 Map 中的 Key. open: 循環(huán)開頭的字符串 close: 循環(huán)結(jié)束的字符串 separator: 每次循環(huán)的分隔符

bind 標(biāo)簽

  • 使用 bind 來讓該 SQL 達(dá)到支持兩個(gè)數(shù)據(jù)庫的作用
  • mapper
    select from test where 1=1 <bind name="nameLike" value="'%'+username+'%'"/> and username like #{nameLike} </if> <if test="ip != null"> and ip=#{ip} </if>
  • 代碼
    @RequestMapping(value = "/dongtaiSql") @ResponseBody public void dongtaiSql() { Test example = new Test(); example.setUsername("周"); List selectByTestSelective = testMapper.selectByTestSelective(example); for (Test test : selectByTestSelective) { System.out.println(test.getUsername()); } }
  • 結(jié)果

發(fā)現(xiàn)依然可以。 說明這個(gè)bind 就是綁定一些變量的 ,nameLike 就代表了username 的模糊搜索,就是如果別的地方用得到它的模糊搜索,拿來用即可。用法是 like 后面直接加上 #{nameLike }。

set 標(biāo)簽

這個(gè)標(biāo)簽常用于做修改語句,比如

  • mapper
    UPDATE Products username = #{username}, ip = #{ip}, id = #{id}
  • 代碼
    @RequestMapping(value = "/dongtaiSql5") @ResponseBody public void dongtaiSql5() { Test example = new Test(); example.setUsername("周"); example.setIp("cium"); example.setId(27); int selectByTestSelective = testMapper.updateTestSetTag(example); System.out.println(selectByTestSelective);
    }
  • 結(jié)果

  • 這個(gè)set 說白了就是update語句的 set 時(shí)候的一個(gè)靈活操作。

trim 標(biāo)簽

  • mapper
    select * from test          AND username=#{username}      AND ip=#{ip}
  • 【注意】
  • 這里有很多坑,首先mybatis-plus 中不是 prefixoverride 而是prefixOverrides
  • 然后"AND |OR" 必須有空格,原因如下圖
  • 如果 ip 不是字符串就不能用length() 方法

trim標(biāo)簽各參數(shù)的說明

prefix:在trim標(biāo)簽內(nèi)sql語句加上前綴。 suffix:在trim標(biāo)簽內(nèi)sql語句加上后綴。 prefixOverrides:指定去除多余的前綴內(nèi)容 suffixOverrides:指定去除多余的后綴內(nèi)容,如:suffixOverrides=",",去除trim標(biāo)簽內(nèi)sql語句多余的后綴","。 復(fù)制代碼

然而我在配置的時(shí)候卻遇到了更坑的問題,遲遲得不到解決…歡迎有興趣的朋友一起交流下解決最后這個(gè)問題。

最后

感謝大家看到這里,文章有不足,歡迎大家指出;如果你覺得寫得不錯(cuò),那就給我一個(gè)贊吧。

作者:程序員麥冬
鏈接:https://juejin.cn/post/6901900140655673357
來源:掘金

總結(jié)

以上是生活随笔為你收集整理的mybatis if test 用法_SpringBoot整合Mybatis-Plus 实战之动态SQL,Mybatis拿得出手的功能之一...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。