1、将查询条件封装到一个对象中 MasoctBroadcast对象,前端使用的vue.js,所以需要CommonResult是统一返回的工具类
CommonResult 工具类
public class CommonResult implements Serializable{
/**
*
*/
private static final long serialVersionUID = -1752989852101213829L;
public static final String SUCCESS = "2000"; // 返回成功
public static final String FAIL = "1000"; // 返回失败
private String result;
private String mess;
private Object data;
public CommonResult() {
}
public CommonResult(String result, String mess, Object data) {
this.result = result;
this.mess = mess;
this.data = data;
}
// 成功
public static CommonResult success() {
return new CommonResult(SUCCESS, null, null);
}
public static CommonResult successWithMessage(String mess) {
return new CommonResult(SUCCESS, mess, null);
}
public static CommonResult successWithData(String mess,Object data) {
return new CommonResult(SUCCESS, mess, data);
}
/*public static CommonResult successWithMessage(String mess, Object... objects) {
return new CommonResult(SUCCESS, String.format(mess, objects), null);
}*/
public static CommonResult successWithData(Object data) {
return new CommonResult(SUCCESS, null, data);
}
// 失败
public static CommonResult fail() {
return new CommonResult(FAIL, null, null);
}
public static CommonResult failWithMessage(String mess) {
return new CommonResult(FAIL, mess, null);
}
public static CommonResult failWithMessage(String mess,Object data) {
return new CommonResult(FAIL, mess, data);
}
/*public static CommonResult failWithMessage(String mess, Object... objects) {
return new CommonResult(FAIL, String.format(mess, objects), null);
}*/
//
public boolean isSuccess() {
return SUCCESS.equals(getResult());
}
public boolean isFailure() {
return FAIL.equals(getResult());
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
public String getMess() {
return mess;
}
public void setMess(String mess) {
this.mess = mess;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
controller
@RequestMapping(value="list")
@ResponseBody
public CommonResult index(MasoctBroadcast mBroadcast,@RequestParam(defaultValue="1") Integer pageCode,@RequestParam(defaultValue="10") Integer sizePage,
Model model, HttpServletResponse response) throws Exception{
//List
PageBean page = boradcastServive.findAllMasoctBroadcastsList(mBroadcast,pageCode, sizePage);
return CommonResult.successWithData("请求成功", page);
}
service接口层
public PageBean findAllMasoctBroadcastsList(MasoctBroadcast mBroadcast,Integer pageCode,Integer pageSize)throws Exception;
service 实现层
public PageBean findAllMasoctBroadcastsList(MasoctBroadcast mBroadcast,Integer pageCode,Integer pageSize)throws Exception{
//设置分页信息
PageHelper.startPage(pageCode,pageSize);
// 自定义的mapper文件
List
for (MasoctBroadcast masoctBroadcast : list) {
String areaId = masoctBroadcast.getAreaId();
List
masoctBroadcast.setList(areaIdList);
}
//创建返回结果集
PageBean bean = new PageBean();
bean.setRows(list); //封装查询结果
//取分页信息
PageInfo
//设置总记录数
bean.setTotalCount((int)pageInfo.getTotal());
bean.setSizePage(pageSize); //设置每页条数
bean.setPageCode(pageCode);
System.out.println(pageInfo.getTotal());
return bean;
}
PageBean 分页对象类
public class PageBean {
/**
* 总条数
*/
private int totalCount;
/**
* 总页数 总条数/每页条数
*/
private int totalPage;
/**
* //每页显示数 默认
*/
private int sizePage=20;
/**
* 第几页 默认
*/
private int pageCode;
/**
* //查询返回的结果集
*/
private List rows;
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getTotalPage() {
return totalCount%sizePage==0?totalCount/sizePage:totalCount/sizePage+1; //总页数 总条数/每页条数
}
省略 get和set方法
// 自定义的mapper文件