Mybatis in查询List或数组 场景实例
1.mybatis in 查詢List時
業務代碼示例如下:
List<String> list=new ArrayList<String>();
...;//向list中填裝參數值
//list為必傳參數集時,判斷如果該list為空,沒有參數值,則填裝一個-1或其他保證該表不會查詢出的參數值;
//如果list為非必傳參數集時,則下面if判斷可以省去;
if(list.size()==0){
? ? list.add("-1");
}
HashMap<String,Object> params=new HashMap<String,Object>();
params.put("list",list);
List<HashMap<String,Object>> rList=dao.queryParams(params);
mybatis中相應sql寫法示例如下:
<select id="queryParams" resultType="HashMap">
? ? select * from cga_case a?
? ? <where>
? ? ? ? <if test="list != null and list.size()>0">//注意:此處不能寫list!=''要寫成list.size()>0,不然會報錯
? ? ? ? AND a.case_id IN
? ? ? ? <foreach item="item" index="index" collection="list" open="(" ?close=")" separator=","> ?
? ? ? ? ? #{item} ??
? ? ? ? </foreach> ?
? ? </if>
? ? </where>
</select>
2.mybatis in查詢 數組時
業務代碼示例如下:
String[] arr=new String[]{...};
//arr為必傳參數集時,判斷如果該arr為空,沒有參數值,則填裝一個-1或其他保證該表不會查詢出數據的參數值;
//如果arr為非必傳參數集時,則下面if判斷可以省去;
if(arr.length==0){
? ? arr=new String[]{"-1"};
}
HashMap<String,Object> params=new HashMap<String,Object>();
params.put("arr",arr);
List<HashMap<String,Object>> rList=dao.queryParams(params);
mybatis中相應sql寫法示例如下:
<select id="queryParams" resultType="HashMap">
? ? select * from cga_case a?
? ? <where>
? ? ? ? <if test="arr != null and arr.length>0">
? ? ? ? AND a.case_id IN
? ? ? ? <foreach item="item" index="index" collection="arr" open="(" ?close=")" separator=","> ?
? ? ? ? ? #{item} ??
? ? ? ? </foreach> ?
? ? </if>
? ? </where>
</select>
?
總結
以上是生活随笔為你收集整理的Mybatis in查询List或数组 场景实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Gstreamer中一些gst-laun
- 下一篇: 详解网络摄像机中的IR-CUT