• MySql分页查询


    limit     (pageSize-1)*每页显示的个数,每页显示的个数

    SELECT id,NAME,typename,health,love,birthday FROM pet

    WHERE owner_id=(SELECT id FROM petowner WHERE NAME=?)

    ORDER BY id ASC LIMIT ? ,?";

    DaoImpl层

     1 public class PetDaoImpl extends BaseDao implements IPetDao {
     2 
     3     /**
     4      * 根据宠物主人查询其拥有的宠物信息
     5      * @param name 宠物主人姓名
     6      * @return List<Pet>
     7      */
     8     @Override
     9     public List<Pet> getPetByPoname(String poname,int startCount, int pageSize) {
    10         // TODO Auto-generated method stub
    11         List<Pet> petList = new ArrayList<Pet>();
    12         Pet pet = null;
    13         
    14         String sql="SELECT id,NAME,typename,health,love,birthday FROM pet WHERE owner_id=(SELECT id FROM petowner WHERE NAME=?) ORDER BY id  ASC LIMIT ? ,?";
    15         Object[] params= {poname,startCount,pageSize};
    16         res=super.excuteSelect(sql, params);
    17         try {
    18             while(res.next()){
    19                 pet=new Pet();
    20                 pet.setId(res.getInt("id"));
    21                 pet.setName(res.getString("name"));
    22                 pet.setTypename(res.getString("typename"));
    23                 pet.setHealth(res.getInt("health"));
    24                 pet.setLove(res.getInt("love"));
    25                 pet.setBirthday(res.getDate("birthday"));
    26                 //把宠物对象添加到集合中
    27                 petList.add(pet);
    28                 
    29             }
    30         } catch (SQLException e) {
    31             // TODO Auto-generated catch block
    32             e.printStackTrace();
    33         }
    34         return petList;
    35     }
    36 
    37     /**
    38      * 根据宠物主人的名字,获取宠物的个数
    39      * @param poname
    40      * @return
    41      */
    42     @Override
    43     public int getCountByPoname(String poname) {
    44         // TODO Auto-generated method stub
    45         int count=0;
    46         String sql ="SELECT COUNT(1) FROM pet WHERE owner_id=(SELECT id FROM petowner WHERE name=?)";
    47         Object[] params= {poname};
    48         res=super.excuteSelect(sql, params);
    49         try {
    50             if(res.next()) {
    51                 count=res.getInt(1);
    52             }
    53         } catch (SQLException e) {
    54             // TODO Auto-generated catch block
    55             e.printStackTrace();
    56         }
    57         
    58         return count;
    59     }
    60 
    61 }

    Servlet层

     1 @WebServlet("/searchPet.do")
     2 public class SearchPetServlet extends HttpServlet {
     3     //
     4     IPetBiz petBiz=new PetBizImpl();
     5 
     6     
     7     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     8         this.doPost(request, response);
     9     }
    10 
    11     
    12     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    13         //
    14         request.setCharacterEncoding("UTF-8");
    15         response.setCharacterEncoding("UTF-8");
    16         HttpSession session=request.getSession();
    17         String poname=String.valueOf(session.getAttribute("poname2"));
    18         System.out.println(poname);
    19         
    20         //实现分页显示数据列表(1.页码变量,2.每页显示的条目数变量,3,总页数变量[通过数据的总行数与条目数计算])
    21         //注意点:用户第一次请求,不会在该servlet中不会接收到页面参数,所以会有空异常,如果有异常默认页码为1
    22         //创建页码变量
    23         int pageNo = 0;
    24         //创建页大小变量[显示的条目数]
    25         int pageSize = 3;
    26         //每页开始的编号
    27         int startCount=0;
    28         //创建数据的总行数变量[通过数据库聚合函数获得]
    29         int count=petBiz.getCountByPoname(poname);
    30         System.out.println("宠物的总个数:"+count);
    31         
    32         //计算总页数
    33         int maxPage = 0;
    34         if(count%pageSize==0){
    35             maxPage = count/pageSize;
    36         }else{
    37             maxPage = count/pageSize+1;
    38         }
    39         //通过request接收页面传递过来的页码
    40         
    41         try {
    42             pageNo=Integer.parseInt(request.getParameter("pageNo"));
    43             startCount=(pageNo-1)*pageSize;
    44         } catch (NumberFormatException e) {
    45             startCount=0;
    46         }
    47         //根据宠物主人名,查询主人拥有的的宠物信息
    48         List<Pet> petList=new ArrayList<Pet>();
    49         petList=petBiz.searchPetByPetname(poname,startCount,pageSize);
    50         System.out.println("petList"+petList.size());
    51         //把集合放入request作用域
    52         request.setAttribute("petList",petList);
    53         request.setAttribute("pageNo", pageNo);
    54         request.setAttribute("maxPage", maxPage);
    55         //将数据列表放入Request作用域
    56         //创建转发对象
    57         RequestDispatcher rd = request.getRequestDispatcher("searchpet.jsp");
    58         rd.forward(request, response);
    59         
    60     }
    61 
    62 }

    jsp

     1     <table>
     2         <tr>
     3             <td colspan="4" >您的宠物列表如下:</td>
     4         </tr>
     5         <tr>
     6             <td>宠物名字</td>
     7             <td>宠物品种</td>
     8             <td>健康值</td>
     9             <td>亲密度</td>
    10             <td>出生日期</td>
    11         </tr>
    12         <c:forEach items="${petList}" var="pet">
    13             <tr>
    14                 <td> ${pet.name}</td>
    15                 <td> ${pet.typename}</td>
    16                 <td> ${pet.health}</td>
    17                 <td> ${pet.love}</td>
    18                 <td> ${pet.birthday}</td>
    19                 
    20             </tr>
    21         </c:forEach>
    22         <tr>
    23             <td>
    24                 总页数:${maxPage }&nbsp;&nbsp;&nbsp;
    25                 <c:if test="${pageNo>1}">
    26                 <a href="searchPet.do?pageNo=${pageNo -1}">上一页</a>
    27                 </c:if>
    28                 <c:if test="${pageNo<requestScope.maxPage}">
    29                 <a href="searchPet.do?pageNo=${pageNo +1}">下一页</a>
    30                 </c:if>
    31             </td>
    32         </tr>
    33     </table>

          

    年轻人能为世界年轻人能为世界做些什么
  • 相关阅读:
    Docker服务启动报错:Job for docker.service failed because the control process exited with error code.
    mysql忘记密码如何重置及修改密码
    linux下的/opt目录作用
    linux防火墙查看状态firewall、iptable
    nmap基本使用方法
    HTTP响应码大全
    端口镜像
    查看占用端口
    restful规范 APIview 解析器组件 Postman
    状态码301和302的区别
  • 原文地址:https://www.cnblogs.com/twinkle-star/p/9464930.html
Copyright © 2020-2023  润新知