簡體   English   中英

為什么 isIn 條件在 MyBatis 中不起作用

[英]Why the isIn condition doesn't work in MyBatis

我寫了兩個select語句,第一個生成了一個id列表,在第二個中我希望idList內有id的視頻會被選中。 我打印了 idList 並確保它為,但下面的 select 語句返回了表中的所有視頻

SelectStatementProvider selectStatementProvider = selectDistinct(
        VideoHitDynamicSqlSupport.videoId
)
        .from(VideoHitDynamicSqlSupport.videoHit)
        .where(VideoHitDynamicSqlSupport.hitterId, isEqualTo(uid))
        .build()
        .render(RenderingStrategies.MYBATIS3);

List<VideoHit> videoHits = this.videoHitMapper.selectMany(selectStatementProvider);
ArrayList<Integer> idList = new ArrayList<>();
videoHits.forEach(e -> idList.add(e.getVideoId()));

List<Video> videos = this.videoMapper.select(c -> c
        .where(VideoDynamicSqlSupport.videoId, isIn(idList))
);

return videos;

這是預期的行為。 如果返回的 ID 列表為空,則不會呈現 IsIn 條件。

您必須考慮實際的 SQL 是什么。 如果條件呈現,則生成的 SQL 將如下所示:

select * from video where videoId in ()

這是無效的 SQL。 該庫不會呈現無效的 SQL,在這種情況下,這意味着不會呈現任何where子句 - 因此返回所有行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM