Mabits的PageHelper分页插件的使用和Jsp分页页面展示(带源码)
1.在Maven使用分页插件,修改pom.xml文件 导入包,本次使用的是4.0.0的版本
4.0.0 com.github.pagehelperpagehelper${pagehelper.version}
2.修改MyBatis配置文件(我的配置文件名称为SqlMapConfig.xml)
3.创建PageBean对象,用于封装查询条件
import java.util.List;public class Pagebean {private int totalCount; //总条数private int totalPage;//总页数 总条数/每页条数private int sizePage=5;//每页显示数 默认 private int pageCode;//第几页 默认private List rows;//查询返回的结果集public List getRows() {return rows;}public void setRows(List rows) {this.rows = 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;//总页数 总条数/每页条数}public void setTotalPage(int totalPage) {this.totalPage = totalPage;}public int getSizePage() {return sizePage;}public void setSizePage(int sizePage) {this.sizePage = sizePage;}public int getPageCode() {return pageCode;}public void setPageCode(int pageCode) {this.pageCode = pageCode;}}
4.Jsp页面核心代码编写
index.jsp分页查看 list.jsp当前位置: 品牌管理 - 列表
5.Controller类编写
@Controllerpublic class BrandController {@Autowiredprivate BrandService brandService;@RequestMapping("/list")public String list(int pageCode, int sizePage,ModelMap model){//创建一个传递参数的对象BbsBrand brand = new BbsBrand();//这个是一个查询返回的对象//设置显示或不显示的查询条件 1:显示 0: 不显示brand.setName("name");//通过name查询//service层方法处理,分页进行查询Pagebean page = brandService.getBrandList(pageCode, sizePage, brand);model.addAttribute("page",page);//将结果返回页面 ,并取名 brands//model.addAttribute("brands", brands);return "list";}}
6.写Service层
在service类中PageHelper.startPage(pageCode, sizePage);方法,第一个参数为查询页数,第二个为每页条数接口:public interface BrandService {public Pagebean getBrandList(int pageCode,int sizePage,BbsBrand brand); }实现类:@Servicepublic class BrandServiceImpl implements BrandService{@Autowired private BbsBrandMapper brandMapper;//分页对品牌进行查询public Pagebean getBrandList(int pageCode, int sizePage,BbsBrand brand) {System.out.println("service"+pageCode);PageHelper.startPage(pageCode, sizePage);List list = brandMapper.selectBrandList(brand);//调用dao进行查询//创建返回结果集Pagebean bean=new Pagebean();bean.setRows(list);//将结果封装//获取总记录数PageInfo pageInfo=new PageInfo(list);bean.setTotalCount((int)pageInfo.getTotal());//返回总条数bean.setSizePage(sizePage);//设置每页条数bean.setPageCode(pageCode);//设置第几页return bean;}}
7.Mapper的创建
public interface BbsBrandMapper {public List selectBrandList(BbsBrand brand);}select id,name,description,img_url,sort,is_display from bbs_brand where is_display = #{isDisplay}
8.创建数据库 插入数据
CREATE TABLE `bbs_brand` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID', `name` varchar(40) NOT NULL COMMENT '名称', `description` varchar(80) DEFAULT NULL COMMENT '描述', `img_url` varchar(80) DEFAULT NULL COMMENT '图片Url', `sort` int(11) DEFAULT NULL COMMENT '排序:最大最排前', `is_display` tinyint(1) DEFAULT NULL COMMENT '是否可见 1:可见 0:不可见', PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COMMENT='品牌';insert into `bbs_brand`(`id`,`name`,`description`,`img_url`,`sort`,`is_display`) values (1,'依琦莲','依琦莲','image/123.jpg',1,1),
(2,'凯速(KANSOON)','凯速(KANSOON)','image/123.jpg',2,1),(3,'梵歌纳(vangona)','梵歌纳(vangona)','image/123.jpg',3,1),
(4,'伊朵莲','伊朵莲','image/123.jpg',4,1),(5,'菩媞','菩媞','image/123.jpg',5,1),(6,'丹璐斯','丹璐斯','image/123.jpg',6,1),
(7,'喜悦瑜伽','喜悦瑜伽','image/123.jpg',7,1),(8,'金乐乐','金乐乐','image/123.jpg',8,1);
源码下载地址:PageHelper分页插件的使用和Jsp分页页面展示源码_jsp分页插件-Java代码类资源-CSDN下载