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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

06.动态SQL和foreach

發布時間:2024/7/5 数据库 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 06.动态SQL和foreach 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

動態sql:

映射文件代碼:

1 <!-- 動態sql,根據名字和年齡查詢,where標簽會處理第一個and,其他位置的and不會自動處理 --> 2 <select id="queryStudentByNameAndAge" parameterType="student" resultMap="student1"> 3 select * from student 4 <!-- 1.使用where 1=1 ,避免where后直接跟and --> 5 <!-- 2.使用where關鍵字,避免where后直接跟and --> 6 <where> 7 <if test="sname != null and sname != '' "> 8 and sname = #{sname} 9 </if> 10 <if test="age != null and age != '' "> 11 and age = #{age} 12 </if> 13 </where> 14 </select>

foreach循環屬性集合:

映射文件:

<!-- foreach循環屬性集合--><select id="queryStudentsByHomeAddress" parameterType="Home" resultMap="student1">select * from student <where><if test="homeAddress != null and homeAddress != '' "><!-- colleaction是屬性中的集合,item是遍歷出來的每個元素,open是循環結果前邊的東西,close是循環結果后邊要加的東西,separator是循環結果用什么符號分隔 --><foreach collection="homeAddress" item="homeAr" open=" and homeaddress in (" close=" )" separator=",">#{homeAr} </foreach></if> </where></select>

測試類:

1 Home home = new Home(); 2 List<String> homeAddress = new ArrayList<String>(); 3 homeAddress.add("北京"); 4 homeAddress.add("南京"); 5 home.setHomeAddress(homeAddress); 6 List<Student> stu = studentMapper.queryStudentsByHomeAddress(home);

foreach循環集合:

映射文件:(需要注意的是循環要循環list)

1 <!-- foreach循環集合--> 2 <select id="queryStudentsWithList" parameterType="list" resultMap="student1"> 3 select * from student 4 <where> 5 <!-- 必須寫list --> 6 <if test="list != null and list != '' "> 7 <foreach collection="list" item="homeAr" open=" and homeaddress in (" close=" )" separator=","> 8 #{homeAr} 9 </foreach> 10 </if> 11 </where> 12 </select>

測試類:

1 List<String> list = new ArrayList<String>(); 2 list.add("北京"); 3 list.add("南京"); 4 List<Student> stu = studentMapper.queryStudentsWithList(list);

foreach循環數組:

映射文件:(注意循環必須都寫array,輸入參數不是簡單類型時都寫Object[])

1 <!-- foreach循環數組--> 2 <select id="queryStudentsWithArray" parameterType="Object[]" resultMap="student1"> 3 select * from student 4 <where> 5 <!-- 必須寫array --> 6 <if test="array != null and array != '' "> 7 <foreach collection="array" item="homeAr" open=" and homeaddress in (" close=" )" separator=","> 8 #{homeAr} 9 </foreach> 10 </if> 11 </where> 12 </select>

測試類:

1 String[] str = {"北京","南京"}; 2 List<Student> stu = studentMapper.queryStudentsWithArray(str);

foreach循環對象數組:

映射文件:(需要注意的是這時的item要寫循環出來的對象了,不能瞎寫名字了,然后通過對象點的方式獲取值)

1 <!-- foreach循環對象數組--> 2 <select id="queryStudentsWithObjectArray" parameterType="Object[]" resultMap="student1"> 3 select * from student 4 <where> 5 <!-- 必須寫array --> 6 <if test="array != null and array != '' "> 7 <!-- item里邊寫的是循環出來的對象 --> 8 <foreach collection="array" item="address" open=" and homeaddress in (" close=" )" separator=","> 9 <!-- 對象點屬性獲取值 --> 10 #{address.homeAddress} 11 </foreach> 12 </if> 13 </where> 14 </select>

測試類:

1 Address addr1 = new Address(); 2 addr1.setHomeAddress("北京"); 3 Address addr2 = new Address(); 4 addr2.setHomeAddress("南京"); 5 Address[] address = {addr1,addr2}; 6 List<Student> s = studentMapper.queryStudentsWithObjectArray(address);

?

轉載于:https://www.cnblogs.com/man-tou/p/11343800.html

總結

以上是生活随笔為你收集整理的06.动态SQL和foreach的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。