通用mapper實現復雜單表查詢


 public PageResult<Brand> queryBrandByPage(Integer page, Integer rows, String sortBy, Boolean desc, String key) {
        //分頁
        PageHelper.startPage(page,rows);
        //過濾
        Example example = new Example(Brand.class);
        if (StringUtils.isNotBlank(key)){
            example.createCriteria().orLike("name","%"+key+"%")
                    .orEqualTo("letter",key.toUpperCase());
        }
        //排序
        if (StringUtils.isNotBlank(sortBy)){
            String orderByclause = sortBy+(desc?" DESC":" ASC");
            example.setOrderByClause(orderByclause);
        }
        //查詢
        List<Brand> list = brandMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(list)){
            throw new ShopException(ExceptionEnum.BRAND_NOT_FIND);
        }
        //解析分頁結果
        PageInfo<Brand> info = new PageInfo<>(list);

        return new PageResult<>(info.getTotal(),list);
    }

 

public PageResult<Brand> queryBrandByPage(Integer page, Integer rows, String sortBy, Boolean desc, String key) {
//分頁
PageHelper.startPage(page,rows);
//過濾
Example example = new Example(Brand.class);
if (StringUtils.isNotBlank(key)){
example.createCriteria().orLike("name","%"+key+"%")
.orEqualTo("letter",key.toUpperCase());
}
//排序
if (StringUtils.isNotBlank(sortBy)){
String orderByclause = sortBy+(desc?" DESC":" ASC");
example.setOrderByClause(orderByclause);
}
//查詢
List<Brand> list = brandMapper.selectByExample(example);
if (CollectionUtils.isEmpty(list)){
throw new ShopException(ExceptionEnum.BRAND_NOT_FIND);
}
//解析分頁結果
PageInfo<Brand> info = new PageInfo<>(list);

return new PageResult<>(info.getTotal(),list);
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM