前些天按照视频里讲的做一个分页功能,可是不知道什么原因在页面就是不显示数据。昨天碰巧发现了一个分页插件,只需一些设置就可以完成分页,非常方便。不过由于是新手,其中遇到了很多很多麻烦,不过幸好得到大神的帮助,最终完成了功能,非常感谢他十分耐心地帮我,给你一个么么哒(づ ̄ 3 ̄)づ
没想到做一个小小的分页功能也有这么多的不懂的地方,感觉要学的东西太多太多,加油努力学习吧~
首先给出项目Github地址:Mybatis通用分页插件
然后按步骤给出各部分代码(其他无关代码和配置文件省略)
此处要导入两个jar包:pagehelper-5.0.0.jar和jsqlparser-0.9.5.jar
<!-- 配置分页插件 --><!-- 指定数据库方言 -->
Country.java
public class Country { private Integer id; private String countryname; private String countrycode; public Integer getId() { return id;
} public void setId(Integer id) { this.id = id;
} public String getCountryname() { return countryname;
} public void setCountryname(String countryname) { this.countryname = countryname == null ? null : countryname.trim();
} public String getCountrycode() { return countrycode;
} public void setCountrycode(String countrycode) { this.countrycode = countrycode == null ? null : countrycode.trim();
} @Override
public String toString() { // return (this.getId()+" "+this.getCountryname()+" "+this.getCountrycode());
return "Country [id=" + id +", countryname=" + countryname + ", countrycode=" + countrycode + "]";
}
}
EasyUIDataGridResult.java
public class EasyUIDataGridResult { // easyUI dataGrid 返回结果封装
Long total; // 总的记录数
List> rows; // 数据集
public Long getTotal() { return total;
} public void setTotal(Long total) { this.total = total;
} public List> getRows() { return rows;
} public void setRows(List> rows) { this.rows = rows;
}
}
CountryService.java
public interface CountryService { public EasyUIDataGridResult getCountryList(int page, int rows);
}
CountryServiceImpl.java
@Servicepublic class CountryServiceImpl implements CountryService{ @Autowired
private CountryMapper countryMapper; public EasyUIDataGridResult getCountryList(int page, int rows) { //分页处理
PageHelper.startPage(page, rows); //查询结果
CountryExample example = new CountryExample();
List list = countryMapper.selectByExample(example); //获取分页信息
PageInfo info = new PageInfo(list);
EasyUIDataGridResult result = new EasyUIDataGridResult(); long total = info.getTotal(); //封装分页信息
List row = info.getList();
result.setRows(row);
result.setTotal(total); return result;
}
}
CountryController.java
@Controllerpublic class CountryController { @Autowired
private CountryService countryService; @RequestMapping("/country/list") @ResponseBody
private EasyUIDataGridResult getItemList(Integer page, Integer rows){
EasyUIDataGridResult countrylists = countryService.getCountryList(page,rows); return countrylists;
}
}
在原来的基础上增加json convertors,以便将List集合转换成json对象,可以使用fastjson或者jackson库,二者择其一就好
注意Jar的版本,我选择的是jackson-core-2.8.7.jar jackson-databind-2.8.7.jar jackson-annotations-2.8.7.jar,三者缺一不可,而且不同的版本可能会导致一些异常
countryList.jsp
列表分页展示 <!--必须引入jQuery和easyUI库-->
最后成果如下所示~

作者:野狗子嗷嗷嗷
链接:https://www.jianshu.com/p/a61650c2a71b
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/336/viewspace-2809488/ ,如需转载,请注明出处,否则将追究法律责任。