简体   繁体   English

为什么 isIn 条件在 MyBatis 中不起作用

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

I wrote the two select statements, the first produced a list of id, in the second one I hope videos has id within idList will be selected.我写了两个select语句,第一个生成了一个id列表,在第二个中我希望idList内有id的视频会被选中。 I had the idList printed and make sure it's empty , but the select statement below returned all of the videos in the table .我打印了 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;

This is expected behavior.这是预期的行为。 If the list of IDs returned is empty, then the IsIn condition will not render.如果返回的 ID 列表为空,则不会呈现 IsIn 条件。

You have to think about what the actual SQL would be.您必须考虑实际的 SQL 是什么。 If the condition rendered, then the resulting SQL would be like this:如果条件呈现,则生成的 SQL 将如下所示:

select * from video where videoId in ()

This is invalid SQL.这是无效的 SQL。 The library won't render invalid SQL which, in this case, means that no where clause will be rendered - so all rows are returned.该库不会呈现无效的 SQL,在这种情况下,这意味着不会呈现任何where子句 - 因此返回所有行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM