解決jdbcTemplate處理sql帶in的多個引數問題
阿新 • • 發佈:2018-11-30
問題描述:在利用jdbcTemplate做查詢處理的時候,有個引數有多個值,sql如下
String sql = "select * from MNJX_PSG_SEG_xxx a, MNJX_xxx b " + " where a.flt_no = ? " + " and a.flt_date = ? " + " and a.aborad_no in ?" + " and a.nm_id = b.nm_id"; Object[] params = { memoryData.getFltNo(), Dater.com2ymd(memoryData.getDate()),number }; List<Map<String, Object>> result = xDao.query(sql, params);
這裡的aborad_no會傳多個值進來,首先想到的最笨的方法是寫個迴圈去一個個查,但是這樣很浪費資源。網上查閱了下自資料,
有人說直接放list進去,會報錯
select * from MNJX_PSG_SEG_XXX a, MNJX_XXX b where a.flt_no = ? and a.flt_date = ? and a.aborad_no in ? and a.nm_id = b.nm_id]; SQL state [null]; error code [17004]; 無效的列型別;
解決:這裡可以把aboard_no直接作為一個引數傳進去,而不使用“?”佔位符.
sql為:
String sql = "select * from MNJX_PSG_SEG_xxx a, MNJX_xxx b "
+ " where a.flt_no = ? "
+ " and a.flt_date = ? "
+ " and a.aborad_no in +("+nums+")";
+ " and a.nm_id = b.nm_id";
問題解決!