简介
- 显示的列表信息过多时,进行分页显示,可进行分页跳转
-模糊查询
过程记录
use-list.jsp内此部分页面代码:
<div class="box-tools pull-right">
<ul class="pagination">
<li><a href="/user/findAll.do" aria-label="Previous">首页</a></li>
<li><a href="/user/findAll.do?currentPage=${pageInfo.currentPage-1}">上一页</a></li>
<c:forEach begin="1" end="${pageInfo.totalPage}" var="pageNum">
<li><a href="/user/findAll.do?currentPage=${pageNum}">${pageNum}</a></li>
</c:forEach>
<li><a href="/user/findAll.do?currentPage=${pageInfo.currentPage+1}">下一页</a></li>
<li><a href="/user/findAll.do?currentPage=${pageInfo.totalPage}" aria-label="Next">尾页</a></li>
</ul>
</div>
在bean实体层我们新建了pageInfo对象,该实体类记录了显示的用户数、当前页、总页数、用户总数、每页显示用户数和各用户的信息。
package com.zr.bean;
import java.util.List;
public class PageInfo<T> {
private List<T> list;//用户信息
private int totalCount;//用户总数
private int size; //每页展示条数
private int totalPage;//总页数
private int currentPage;//当前页
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
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 getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
@Override
public String toString() {
return "PageInfo{" +
"list=" + list +
", totalCount=" + totalCount +
", size=" + size +
", totalPage=" + totalPage +
", currentPage=" + currentPage +
'}';
}
}
在service层我们新建一个方法,使用两个参数返回得到一个对象,记录要展示的信息。这两个参数是当前页和用户名
@Override
public PageInfo<User> findAll(int currentPage,String username) {
PageInfo pageInfo = new PageInfo();
int totalCount = userDao.getTotalCount(username);
pageInfo.setTotalCount(totalCount);
double d=totalCount/5.0;
int tp = (int) Math.ceil(d);
pageInfo.setTotalPage(tp);
pageInfo.setSize(5);
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, 5,username);
pageInfo.setList(userList);
return pageInfo;
}
dao层添加接口方法getTotalCount(username),通过模糊查找用户名得到查询总列表,username为空就是总列表。再修改findAll方法,使用三个参数得到当前页显示的用户。
package com.zr.dao;
import com.zr.bean.User;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface IUserDao {
User findUserByUserName(String username);
List<User> findAll(@Param("start") int start,@Param("size") int size,@Param("username") String username);
void deleteById(int id);
void add(User user);
User selectUserById(int id);
void update(User user);
int getTotalCount(@Param("username") String username);
}
最后再Mapper层的映射文件中添加具体实现。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.zr.dao.IUserDao">
<!--别名user小写-->
<select id="findUserByUserName" parameterType="String" resultType="user">
select * from tb_user where username=#{username}
</select>
<select id="findAll" resultType="user">
select * from tb_user
<if test="username!=null and username !=''">
where username like concat("%",#{username},"%")
</if>
limit #{start},#{size}
</select>
<select id="deleteById" parameterType="int">
delete from tb_user where id=#{id}
</select>
<select id="add" resultType="user">
insert into tb_user(username,password) values (#{username},#{password})
</select>
<select id="selectUserById" parameterType="int" resultType="user">
select * from tb_user where id=#{id}
</select>
<update id="update" parameterType="user">
update tb_user set username=#{username},password=#{password}
where id=#{id}
</update>
<select id="getTotalCount" resultType="int">
select count(*) from tb_user
<if test="username!=null and username !=''">
where username like concat("%",#{username},"%")
</if>
</select>
</mapper>
之后在控制层我们就可以从service层拿到pageInfo对象。因为模糊搜索时需要用不完整用户名,在findAll.do处的方法传递一个type和HttpSession参数。
@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 if(type==0) {
username= (String) session.getAttribute("searchname");
}else if(type==2){
session.removeAttribute("searchname");
}
PageInfo<User> pageInfo = userService.findAll(currentPage,username);
ModelAndView mv=new ModelAndView();
mv.setViewName("user-list");
mv.addObject("pageInfo",pageInfo);
return mv;
}
此时界面就可以实现跳转了。
结果演示
分页
模糊查询