使用mybatis-generator生成的Example,快速分頁查詢魔板代碼

controller接口

/**
 * 分頁列表查詢
 *
 * @param pageNum  頁號
 * @param pageSize 頁面大小
 * @param sortType 排序
 * @return BaseResult
 */
@GetMapping(value = "/getCustomerByPage")
public BaseResult getCustomerByPage(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, String sortType) {
	PageResult<Customer> pageResult = customerService.getCustomerByPage(pageNum, pageSize);
	return new BaseResult(200, "查詢成功", pageResult.getData(), pageResult.getTotalCount());
}

serviceImpl實現

@Override
public PageResult<Customer> getCustomerByPage(Integer pageNum, Integer pageSize, String name) {
	CustomerExample example = new CustomerExample();
	example.setOrderByClause(" update_time desc");
	CustomerExample.Criteria criteria = example.createCriteria();

	// TODO 處理查詢邏輯
	if (StringUtils.isNotEmpty(name)) {
		criteria.andNameLike("%" + name + "%");
	}
	//計算總數
	int totalCount = customerMapper.countByExample(example);
	if (pageSize != null && pageNum != null) {
		example.setLimitStart((pageNum - 1) * pageSize);
		example.setLimitEnd(pageSize);
	}
	//分頁查詢
	List<Customer> list = customerMapper.selectByExample(example);

	return new PageResult<>(totalCount, list);
}

PageResult

@Data
@NoArgsConstructor
@AllArgsConstructor
public class PageResult<T> {
    int totalCount;
    List<T> data;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章