mybatis中使用not?in與?in的寫法說明
使用not in與 in的寫法
首先聲明我不是很喜歡用foreach,所以我的代碼中很少出現(xiàn)foreach。不廢話了,上代碼:
in的用法
我的id是Long類型的
service方法,有一個Long的集合:
public List<RbacMenu> listByPackageId(List<Long> ids, String systemCode) {
? ? Map<String, Object> map = new HashMap<String, Object>();
? ? if(ids.size()!=0) {
? ? ? ? StringBuilder sbd = new StringBuilder();
? ? ? ? for(Long cateIds:ids){
? ? ? ? ? ? sbd.append(cateIds+",");
? ? ? ? }
? ? ? ? String idStr = sbd.toString();
? ? ? ? idStr = idStr.substring(0,idStr.length()-1);
? ? ? ? map.put("ids", idStr);
? ? }實體類.xml
select * from xxx where?
<if test="ids != null"> FIND_IN_SET(id,#{ids}) ? </if>not in的用法
service方法,有一個Long的集合:
public List<RbacMenu> listByPackageId(List<Long> ids, String systemCode) {
? ? Map<String, Object> map = new HashMap<String, Object>();
? ? if(ids.size()!=0) {
? ? ? ? StringBuilder sbd = new StringBuilder();
? ? ? ? for(Long cateIds:ids){
? ? ? ? ? ? sbd.append(cateIds+",");
? ? ? ? }
? ? ? ? String idStr = sbd.toString();
? ? ? ? idStr = idStr.substring(0,idStr.length()-1);
? ? ? ? map.put("notids", idStr);
? ? }實體類.xml
select * from xxx where?
<if test="notids != null"> NOT FIND_IN_SET(id,#{notids}) ? </if>使用in查詢時的注意事項
當查詢的參數(shù)只有一個時
findByIds(List<Long> ids)
a 如果參數(shù)的類型是List, 則在使用時,collection屬性要必須指定為 list
?<select id="findByIdsMap" resultMap="BaseResultMap">
? ? ? ? ?Select
? ? ? ? ?<include refid="Base_Column_List" />
? ? ? ? ?from jria where ID in
? ? ? ? ? ? ? ? ? <foreach item="item" index="index" collection="list"?
? ? ? ? ? ? ? ? ? ? ? ? ?open="(" separator="," close=")">
? ? ? ? ? ? ? ? ? ? ? ? #{item}
? ? ? ? ? ? ? ? </foreach>
? </select>??findByIds(Long[] ids)
b 如果參數(shù)的類型是Array,則在使用時,collection屬性要必須指定為 array
? <select id="findByIdsMap" resultMap="BaseResultMap">
? ? ? ? ? ? ? ? ?select
? ? ? ? ? ? ? ? ?<include refid="Base_Column_List" />
? ? ? ? ? from jria where ID in
? ? ? ? ? ? ? ? ? <foreach item="item" index="index" collection="array"?
? ? ? ? ? ? ? ? ? ? ? ? ?open="(" separator="," close=")">
? ? ? ? ? ? ? ? ? ? ? ? #{item}
? ? ? ? ? ? ? ? </foreach>
? </select>當查詢的參數(shù)有多個時
例如 findByIds(String name, Long[] ids)
這種情況需要特別注意,在傳參數(shù)時,一定要改用Map方式, 這樣在collection屬性可以指定名稱
下面是一個示例
? ? ? ? ?Map<String, Object> params = new HashMap<String, Object>(2);
? ? ? ? params.put("name", name);
? ? ? ? ?params.put("ids", ids);
? ? ? ? mapper.findByIdsMap(params);
?
?<select id="findByIdsMap" resultMap="BaseResultMap">
? ? ? ? ? ? ? ? ?select
? ? ? ? ? ? ? ? ?<include refid="Base_Column_List" />
? ? ? ? ? from jria where ID in
? ? ? ? ? ? ? ? ? <foreach item="item" index="index" collection="ids"?
? ? ? ? ? ? ? ? ? ? ? ? ?open="(" separator="," close=")">
? ? ? ? ? ? ? ? ? ? ? ? #{item}
? ? ? ? ? ? ? ? </foreach>
? ?</select>?完整的示例如下:
例如有一個查詢功能,Mapper接口文件定義如下方法:
List<Jria> findByIds(Long... ids);
使用 in 查詢的sql拼裝方法如下:
?<select id="findbyIds" resultMap="BaseResultMap">
? ? ? ? ? ? ? ? ?select
? ? ? ? ? ? ? ? ?<include refid="Base_Column_List" />
? ? ? ? ? from jria where ID in
? ? ? ? ? ? ? ? ? <foreach item="item" index="index" collection="array"?
? ? ? ? ? ? ? ? ? ? ? ? ?open="(" separator="," close=")">
? ? ? ? ? ? ? ? ? ? ? ? #{item}
? ? ? ? ? ? ? ? </foreach>
? </select>?以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java網(wǎng)絡編程之URL+URLconnection使用方法示例
這篇文章主要介紹了Java網(wǎng)絡編程之URL+URLconnection使用方法示例,還是比較不錯的,這里分享給大家,供需要的朋友參考。2017-11-11
Spring Boot整合EasyExcel(完整版包含上傳解析excel和下載模板)
這篇文章主要介紹了Spring Boot整合EasyExcel(完整版包含上傳解析excel和下載模板),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-12-12
feignclient?https?接口調用報證書錯誤的解決方案
這篇文章主要介紹了feignclient?https?接口調用報證書錯誤的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
Struts2學習筆記(6)-簡單的數(shù)據(jù)校驗
這篇文章主要介紹Struts2中的數(shù)據(jù)校驗,通過一個簡單的例子來說明,希望能給大家做一個參考。2016-06-06

