Why the isIn condition doesn't work in MyBatis

Jettie :

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;
Jeff Butler :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=170021&siteId=1