天天看點

mybatis plus分頁查詢之掉坑裡

MP(不是MMP ^_^)--》mybatis plus,通常我們用ssm的時候會用到這個,但是裡面也有一些小坑(如果研究透徹不算是坑)需要填一下

分頁查詢

通常我們的調用分頁查詢時候Mapper是這麼寫的

List<Student> queryPage(Page<Student> page, Map<String,Object> params);
           

然後再正常寫sql查詢,沒毛病。

可是

當我們調用他service自帶的selectPage(Page<T> page, Wrapper<T> wrapper)時候,有可能會有驚喜。比如

@RequestMapping("query-page")
    public ResultModel queryPage(@RequestBody Page<Student> page, @RequestParam(name = "name", required = false) String name) {
        EntityWrapper<Student> entityWrapper = new EntityWrapper<>();
        entityWrapper.like("NAME", name);
        return ResultModel.ok(scoreGoodsService.selectPage(page, entityWrapper));
    }
           

我們本意是查詢名稱含有{name}的學生,但是通常我們為了友善,會把查詢參數封裝到page.condition中,此時如果page.condition中包含其他參數,則會一起被拼接在整個sql後面,是以查詢的結果不隻是按name查詢。

需要注意的就是條件查詢時候一定弄清楚condition中都包含了什麼

挖坑不止,踩坑不斷