controller
// 品牌列表页面
@RequestMapping(value = "/brand/list.do")
// pageNo 传入的页面号码
public String list(String name, Integer isDisplay, Integer pageNo, ModelMap model) {
Brand brand = new Brand();
// sb 拼接页面请求参数
StringBuilder sb = new StringBuilder();
// 空串notblank "" null
if (StringUtils.isNotBlank(name)) {
brand.setName(name);
sb.append("name=").append(name);
}
if (isDisplay != null) {
brand.setIsDisplay(isDisplay);
sb.append("&").append("isDisplay=").append(isDisplay);
}else{
brand.setIsDisplay(1);
sb.append("&").append("isDisplay=1");
}
// 页号 如果pageNo<1 或为null 设置pageNo=1
brand.setPageNo(Pagination.cpn(pageNo));
// 每页数
brand.setPageSize(5);
// 分页对象,调用service层获取数据
Pagination pagination = brandService.getBrandListWithPage(brand);
// 页面展示
String url = "/brand/list.do";
pagination.pageView(url, sb.toString());
// 返回给页面
model.addAttribute("pagination", pagination);// 底层request.setAttribute实现
model.addAttribute("isDisplay", brand.getIsDisplay());// 底层request.setAttribute实现
model.addAttribute("name", name);// 底层request.setAttribute实现
return "brand/list";
}
Pagination pagination = brandService.getBrandListWithPage(brand);
public Pagination getBrandListWithPage(Brand brand) {
// 1:页号
// 2:每页数
// 3:总记录数
Pagination Pagination = new Pagination(brand.getPageNo(), brand.getPageSize(), brandDao.getBrandCount(brand));
// 品牌数据集合//调用dao层获取数据
Pagination.setList(brandDao.getBrandListWithPage(brand));
return Pagination;
}
brandDao.getBrandListWithPage(brand)
// list集合
public List<Brand> getBrandListWithPage(Brand brand);
调用mybatis
<!--#{isDisplay}获取对象字段值 -->
<!-- 查询品牌 get* -->
<select id="getBrandListWithPage" parameterType="Brand"
resultMap="brand">
select id,name,description,img_url,web_site,sort,is_display
from
bbs_brand
<where>
<if test="isDisplay != null">
is_display=#{isDisplay}
</if>
<if test="name != null">
and name=#{name}
</if>
</where>
order by id desc
limit #{startRow},#{pageSize}
</select>
最后将数据返回