package entity;
import java.util.List;
/**
* 分页结果类
*/
public class PageResult<T> {
private Long total;
private List<T> rows;
public PageResult() {
}
public PageResult(long total, List<T> rows) {
this.total = total;
this.rows = rows;
}
public Long getTotal() {
return total;
}
public void setTotal(Long total) {
this.total = total;
}
public List<T> getRows() {
return rows;
}
public void setRows(List<T> rows) {
this.rows = rows;
}
}
controller
/**
* 条件分页查询
* {total:10,rows:[]}
*/
@RequestMapping("/search")
public PageResult search(@RequestBody TbBrand brand, Integer pageNum, Integer pageSize{
return brandService.search(brand,pageNum,pageSize);
}
service:
@Autowired
private TbBrandMapper brandMapper;
public PageResult search(TbBrand brand, Integer pageNum, Integer pageSize) {
//分页条件设置
PageHelper.startPage(pageNum,pageSize);
//设置条件的对象
TbBrandExample example = new TbBrandExample();
//组装查询条件的对象
TbBrandExample.Criteria criteria = example.createCriteria();
//设置条件查询
if(brand!=null){
//获取品牌名称查询条件
String brandName = brand.getName();
if(brandName!=null && !"".equals(brandName)){
//页面输入了品牌名称查询条件,组装条件查询操作 %%
criteria.andNameLike("%"+brandName+"%");
}
//获取品牌首字母
String firstChar = brand.getFirstChar();
if(firstChar!=null && !"".equals(firstChar)){
//页面输入了品牌首字母查询条件,组装条件查询操作
criteria.andFirstCharEqualTo(firstChar);
}
}
Page<TbBrand> page = (Page<TbBrand>) brandMapper.selectByExample(example);
return new PageResult(page.getTotal(),page.getResult());
}