赞
踩
https://blog.csdn.net/weixin_40699504/article/details/107552749
模糊分页查询
1.具体的Bean类型
一个pageinfo类型,来存储页相关的数据结构。
package com.whut.bean;
import java.util.List;
public class PageInfo<T> {
private List<T> list;
private int size;
private int totalPage;
private int totalCount;
private int currentPage;
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
@Override
public String toString() {
return "PageInfo{" +
"list=" + list +
", size=" + size +
", totalPage=" + totalPage +
", totalCount=" + totalCount +
", currentPage=" + currentPage +
'}';
}
}
2.Controller类中增加一个findAll方法
@RequestMapping("/findAll.do")
public ModelAndView findAll(@RequestParam(defaultValue = "1")int currentPage, String username, @RequestParam(defaultValue = "0")int type, HttpSession session){
if(type==1){
session.setAttribute("searchName",username);
}else{
username=(String)session.getAttribute("searchName");
}
PageInfo<User> pageInfo=userService.findAll(currentPage,username);
ModelAndView mv=new ModelAndView();
mv.addObject("pageInfo",pageInfo);
mv.setViewName("user-list");
return mv;
}
2.在userDao中改变findAll接口,添加一个start参数,表明模糊查询到的数据在数据库中的起始位置;并添加一个getTotalCount的接口。并在UserMapper.xml文件中改变相应的数据库查询语句,和添加相应的查询语句
<mapper namespace="com.huxiaoheng.dao.UserDao" >
<select id="findAll" resultType="User">
select * from user
<if test="username!=null and username!=''">
where username like concat("%",#{username},"%")
</if>
limit #{start},5
</select>
<select id="getTotalCount" resultType="int">
select count(*) from user
<if test="username!=null and username!=''">
where username like concat("%",#{username},"%")
</if>
</select>
</mapper>
3.2
2.在userDao中改变findAll接口,添加一个start参数,表明模糊查询到的数据在数据库中的起始位置;并添加一个getTotalCount的接口。并在UserMapper.xml文件中改变相应的数据库查询语句,和添加相应的查询语句
4.在UserService中改变查询接口
5.在UserServiceImpl中改变接口实现方法,使其由原先的数据列表展示变为分页展示,规定每页展示五行数据
6.改变UserController中的findAll方法
4.具体的逻辑写到service层中
@Override
public PageInfo<User> findAll(int currentPage, String username) {
PageInfo<User> pageInfo=new PageInfo<>();
pageInfo.setSize(5);
//tc为查询数据的总行数
int tc=userDao.getTotalCount(username);
pageInfo.setTotalCount(tc);
//tp为总页数
int tp=(int)Math.ceil(tc/5.0);
pageInfo.setTotalPage(tp);
if(currentPage<1){
pageInfo.setCurrentPage(1);
}else if(currentPage>tp){
pageInfo.setCurrentPage(tp);
}else {
pageInfo.setCurrentPage(currentPage);
}
int start=(pageInfo.getCurrentPage()-1)*5;
List<User> userList=userDao.findAll(start,username);
pageInfo.setList(userList);
return pageInfo;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。