mybatis中foreach詳解(傳參Map型別)
阿新 • • 發佈:2019-02-01
專案遇到:需要根據一個欄位的集合遍歷查詢資料,需要在mybatis使用傳入個Map<String Object>引數進行foreach遍歷查詢。
xml程式碼如下:
其中,collection的值是coverage,是傳入的引數Map的key。<select id="selectByMr" resultMap="BaseResultMap"> select <include refid="Base_Column_List"/> from cell_info <where> <foreach collection="coverage" item="item" index="index" open="and objectid in(" separator="," close=")"> #{item} </foreach> </where> </select>
mapper程式碼如下:
List<Cell_info> selectByMr(Map<String, Object> map);
serviceImpl程式碼如下:
@Override public List<Cell_info> searchWeakCoverage(Map<String, Object> map,Page page) { List<Cell_info> cellList = null; Map<String, Object> mapL = new HashMap<String, Object>(); List<String> list = complaintsAnalysisDao.searchWeakCoverage(map);//查詢多條結果,當做查詢條件 if(0!=list.size()){ mapL.put("coverage", list); mapL.put("page", page);//分頁用的 cellList = cellInfoDao.selectByMr(mapL); } return cellList; }