亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

mybatis 中in 怎么用 ?

mybatis 中in 怎么用 ?

SSM
呼啦一陣風 2019-02-06 11:07:44
本來是一個String字符串 “1001,1002,1003” 然后我用split分割逗號 得到了articleIDArray 數組String[] articleIDArray = zt.getZtContentID().split(",");public List<Article> getArticleZTbyArticleIDArray(String[] articleIDArray) {return articleDao.getArticleZTbyArticleIDArray(articleIDArray);}我傳遞的是一個數組 articleIDArray 我參考著網上的寫法寫的 為什么有錯誤 <select id="findArticleZTbyArticleIDArray" resultType="article" >select articleID, articleTitle,date_format(createtime,'%Y-%m-%d %H:%i:%s') as createtime, tag from T_articlewhere flag='1' and articleID in <foreach item="articleIDArray" index="index" collection="array" open="(" separator="," close=")"> #{articleIDArray}</foreach></select>這是為什么 應該怎樣改
查看完整描述

2 回答

?
RISEBY

TA貢獻1856條經驗 獲得超5個贊

1. 當查詢的參數只有一個時

findByIds(List<Long> ids)
1.a 如果參數的類型是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)
1.b 如果參數的類型是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>

2. 當查詢的參數有多個時,例如 findByIds(String name, Long[] ids)
這種情況需要特別注意,在傳參數時,一定要改用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>



查看完整回答
反對 回復 2019-03-26
?
拉莫斯之舞

TA貢獻1820條經驗 獲得超10個贊

我的也是字符拆成的數組,你這樣:

*Controller類中部分代碼>>>

String[] section = names.getModule().split(",");

System.out.println("查詢條件參數:"+list);

List<EMS_TroubleShootingData> tsList = tsdService.getDataBySections(list);

req.setAttribute("list", tsList);

return "TroubleShootingList";

Dao,Service及其實現類中都使用:(List<String> list);作為參數

最后*Mapper.xml文件中:


查看完整回答
反對 回復 2019-03-26
  • 2 回答
  • 0 關注
  • 3252 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號