简体   繁体   中英

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. I had the idList printed and make sure it's empty , but the select statement below returned all of the videos in the table .

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.

You have to think about what the actual SQL would be. If the condition rendered, then the resulting SQL would be like this:

select * from video where videoId in ()

This is invalid 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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