通过三层架构的方式使用SERVLET实现jsp页面的分页模糊查询

jsp页面的代码:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>





    
    
    
    
    
    
    
    用户信息管理系统

    
    
    
    
    
    
    

    


用户信息列表

编号 姓名 性别 年龄 籍贯 QQ 邮箱 操作
${s.count} ${user.name} ${user.gender} ${user.age} ${user.address} ${user.qq} ${user.email} 修改  删除

SERVLET的代码编写



import Domain.PageBean;
import Domain.User;
import Service.UserService;
import Service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet("/AthPageServlet")
public class AthPageServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取前台传来当前页码和行数
        String currentPage = request.getParameter("currentPage");
        String rows = request.getParameter("rows");
        //2.对前两个参数进行判断,对当前页数进行判断,小于0则赋值为1
        if (currentPage == null || "".equals(currentPage)) {
            currentPage = "1";
        } else if( Integer.parseInt(currentPage) <=0 ){
            currentPage = "1";
        }
        System.out.println("???"+currentPage);
        if (rows == null || "".equals(rows)) {
            rows = "5";
        }
        System.out.println(rows);
        //获取传递的查询参数
        /*
            key         value
            current     1
            rows        5
            name        kobe
            team        laker
         */
        Map map = request.getParameterMap();
        //获得PageBean
        UserService service = new UserServiceImpl();
        PageBean pageBean = service.getPageBean(currentPage, rows, map);


        //转发到前台界面进行展示
        request.setAttribute("pageBean", pageBean);
        request.setAttribute("map", map);
        request.getRequestDispatcher("list.jsp").forward(request, response);
    }
}

service层的代码

@Override
    public PageBean getPageBean(String _currentPage, String _rows, Map map) {
        int currentPage = Integer.parseInt(_currentPage);
        int rows = Integer.parseInt(_rows);

        PageBean pageBean = new PageBean<>();
        pageBean.setCurrentPage(currentPage);
        pageBean.setRows(rows);

        //获得总记录数
        AthDao dao = new AthDaoImpl();
        int totalCount = dao.getToatalCount(map);
        pageBean.setTotalCount(totalCount);

        //获得总页数
        int totalPage = totalCount % rows == 0 ? totalCount / rows : (totalCount / rows) + 1;
        pageBean.setTotalPage(totalPage);

        // 定义Start
        int start = rows * (currentPage - 1);
        if(currentPage > totalPage) {
            currentPage = totalPage;
            start = rows * (totalPage - 1);
        }

        //获得单页数据
        List list = dao.getAthList(start, rows, map);
        pageBean.setList(list);
        System.out.println(list);
        return pageBean;
    }

dao层代码的编写:

@Override
    public int getToatalCount(Map map) {
        String sql = "select count(*) from athlete where 1 = 1 ";
        //1.定义StringBuilder 用来拼接我们的sql语句
        StringBuilder sb = new StringBuilder(sql);
        //2.map的遍历
        Set set = map.keySet();
        //3.准备list集合以存储查询条件
        List list = new ArrayList<>();
        /*
            key         value
            current     1
            rows        5
            name        kobe
            team        laker
         */
        for (String key : set) {
            if ("currentPage".equals(key) || "rows".equals(key)) {
                continue;
            }
            String value = map.get(key)[0];
            if (value != null && !"".equals(value)) {
                //sql= "and name like %%"
                // and name like ?    % ko %
                // sql = "select * from athlete where 1 = 1 and name like ?"
                //(sql , "%ko%")
                sb.append(" and " + key +  " like ? ");
                //       %kobe%
                list.add("%" + value + "%");
            }
        }
        //StringBUilder转化sql
        sql = sb.toString();
        System.out.println(sql);
        System.out.println(list);

        // ["%kobe%", "%laker%"]
        return template.queryForObject(sql, Integer.class, list.toArray());
    }

    @Override
    public List getAthList(int start, int rows, Map map) {
        String sql = "select * from athlete where 1 = 1";
        StringBuilder sb = new StringBuilder(sql);
        Set set = map.keySet();
        List list = new ArrayList<>();

        for (String key : set) {
            if ("currentPage".equals(key) || "rows".equals(key)) {
                continue;
            }
            String value = map.get(key)[0];
            if (value != null && !"".equals(value)) {
                sb.append(" and " + key +  " like ? ");
                list.add("%" + value + "%");
            }
        }
        sb.append(" limit ? , ?");
        list.add(start);
        list.add(rows);
        sql = sb.toString();
        System.out.println(sql);
        System.out.println(list);
        return template.query(sql, new BeanPropertyRowMapper(User.class), list.toArray());
    }
 
  

                            
                        
                    
                    
                    

你可能感兴趣的:(java,分页,模糊查询,三层架构,SERVLET,java)