1.后台sql语句 -Dao层

这里标** 代表关键代码

  1. public List<Video> selectAll2(Map<String, String> parm, Integer pageNo, Integer pageSize) {
  2. ArrayList<Video> list = new ArrayList<>();
  3. Connection conn = JDBCUtils.getConnection();
  4. //**
  5. String sql="select * from t_video where 1=1";
  6. if(parm!=null ){
  7. //模糊匹配 按类别匹配
  8. if(parm.get("class")!=null && !parm.get("class").equals("")){
  9. sql+=" and v_class like '%"+parm.get("class")+"%'";
  10. }
  11. // 按类别进行排序搜索
  12. else if(parm.get("orderByType")!=null && !parm.get("orderByType").equals("")){
  13. sql+=" and v_class='"+parm.get("orderByType")+"' order by v_amount desc";
  14. }else if(parm.get("searchTitle")!=null && !parm.get("searchTitle").equals("")){
  15. sql+=" and v_title like '%"+parm.get("searchTitle")+"%'";
  16. }
  17. }
  18. // 拼接分页条件
  19. // 开始条数 zh
  20. //sql语句 这里是动态拼接 拼接好后 进行限制条数 比如
  21. // select * from t_video where 1=1 limit 0,5; 从第0条开始数5条数据 ,拼接结束后进行遍历
  22. Integer startIndex =pageSize *(pageNo-1);
  23. sql+=" limit "+startIndex+" ,"+pageSize+"";
  24. PreparedStatement pstm=null;
  25. ResultSet rs=null;
  26. try {
  27. pstm = conn.prepareStatement(sql);
  28. rs =pstm.executeQuery();
  29. while(rs.next()){
  30. // private String videoClass;
  31. // private int videoAmount;
  32. // private int videoLike;
  33. // private int userId;
  34. // private String url;
  35. // private String pic;
  36. int videoId = rs.getInt("v_id");
  37. String videoClass = rs.getString("v_class");
  38. int videoLike =rs.getInt("v_like");
  39. int videoAmount = rs.getInt("v_amount");
  40. String videoTitle=rs.getString("v_title");
  41. String videoIntro=rs.getString("v_introduce");
  42. Date videoPbtime=rs.getDate("v_pbtime");
  43. int userId=rs.getInt("u_id");
  44. String url=rs.getString("url");
  45. String pic=rs.getString("v_pic");
  46. Video video = new Video(videoId, videoTitle, videoIntro, videoPbtime, videoClass, videoAmount, videoLike, userId, url, pic);
  47. list.add(video);
  48. }
  49. } catch (SQLException e) {
  50. e.printStackTrace();
  51. }finally {
  52. JDBCUtils.closeAll(conn,pstm,rs);
  53. }
  54. return list;
  55. }

2.servlet部分

从前端接收到 当前页数和页尺寸后 进行判断,传到service层和dao层中。进行查找 并返回list集合

  1. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  2. resp.setContentType("text/html;charset=UTF-8");
  3. PrintWriter out = resp.getWriter();
  4. Map<String,Object> result=new HashMap<>();
  5. String oper=req.getParameter("oper");//获取操作码
  6. String msg = req.getParameter("msg");
  7. HashMap<String, String> map = new HashMap<>();//存储数据用
  8. if(oper!=null && oper.equals("searchVideo")){
  9. String pageNo = req.getParameter("pageNo");
  10. String pageSize = req.getParameter("pageSize");
  11. String titleName = req.getParameter("titleName");
  12. if(pageNo==null || pageNo.equals("")){
  13. pageNo="1";
  14. }
  15. if(pageSize==null || pageSize.equals("")){
  16. pageSize="5";
  17. }
  18. map.put("searchTitle",titleName);
  19. List<Video> videos = videoService.selectVideo(map, Integer.parseInt(pageNo), Integer.parseInt(pageSize));
  20. List<User> userList = videoService.queryUserByVideo(videos);//视频作者用户列表
  21. Integer count =videoService.selectCount(map);
  22. result.put("count",count);
  23. result.put("data", videos);
  24. result.put("userList",userList);
  25. }
  26. //对象转换为json串
  27. String json= JSON.toJSONString(result);
  28. out.print(json);
  29. out.flush();
  30. out.close();

3.前台动态对接分页数据

  1. function getSearchInfo(name) {
  2. if(name!=null && name!="")
  3. {
  4. page_no =1;
  5. }
  6. //使用ajax获取后台数据
  7. var oper ="searchVideo"
  8. $.ajax({
  9. url:"video.do",
  10. data:{titleName:name,oper:oper,"pageNo":page_no,"pageSize":page_size},
  11. dataType:"json",
  12. success:function (data) {
  13. alert("1124");
  14. //获取前台信息
  15. var list = data.data;
  16. var userList = data.userList;
  17. count =data.count;
  18. $("#pagelist").html("");
  19. $("#div4").html("");
  20. console.log(list);
  21. for (let i = 0; i <page_size && i<list.length; i++) {
  22. var item =list[i];
  23. var userItem =userList[i];
  24. var author =userItem.userName;
  25. var title =item.videoTitle;
  26. var pic =item.pic;
  27. var content="<div> \n" +
  28. " <img src=\"./img/"+pic+"\" alt=\"\" />\n" +
  29. " <p style=\"text-overflow: ellipsis\">"+title+"</p>\n" +
  30. "<em class=\"bott-1\">" +
  31. "<i class=\"fa fa-video\" style=\"color: white; margin-right: 5px\"></i>\n" +
  32. " <span style=\"margin-right: 15px\">43.5万</span>\n" +
  33. "<i class=\"fa fa-mercury\"></i>\n" +
  34. "<span>11.1万</span>\n" +
  35. "<i style=\"margin-left: 24px\">01:43</i>\n" +
  36. "</em>\n" +
  37. "<span class=\"up\" style=\"color: #999999; margin-top: 15px\">UP</span>"+author+"\n" +
  38. "</div>\n"
  39. $("#div4").append(content);
  40. }
  41. //===================== 分页部分===================================================
  42. //分页需要2.1 向上取整。对总数和页大小要取整 然后根据页数进行遍历
  43. var totalPageNum =Math.ceil(count/page_size);
  44. totalPage = totalPageNum;
  45. console.log(totalPageNum);
  46. for (let i = 0; i <totalPageNum; i++) {
  47. $("#pagelist").append("<button class='pagenum'>"+(i+1)+"</button>");
  48. }
  49. }
  50. })
  51. }

4.分页限制页的判断

在点击上一页和下一页时,要注意判断当前页是不是最后一页或者第一页 不能再往前和往后了

4.1上一页限制

  1. //上一页事件
  2. $(".pre_page").click(function () {
  3. page_no--;
  4. if(page_no==0){
  5. page_no=1;
  6. alert("没有上一页了")
  7. }
  8. // getSearchInfo();
  9. judgement();
  10. })

4.2下一页限制

  1. //下一页事件
  2. $(".next_page").click(function () {
  3. console.log("page_no="+page_no+",totalPage="+totalPage)
  4. if(page_no==totalPage){
  5. page_no==totalPage;
  6. // getSearchInfo();
  7. judgement();
  8. alert("当前页已是最后一页")
  9. }else{
  10. page_no++;
  11. // getSearchInfo();
  12. judgement();
  13. }
  14. })

5.分页问题

5.1 搜索后分页问题

在进行搜索后分页,点击下一页会刷新成 所有数据而不是搜索后数据,解决方法,写一个判断函数,对是否有参数进行判断,代码如下。

  1. function judgement() {
  2. if(titleInput==""){
  3. getSearchInfo()
  4. }else{
  5. getSearchInfo(titleInput);
  6. }
  7. }

对搜索框内容设置一个全局变量, 搜索时,传入 该参数。页面刷新时,对搜索框有无内容进行判断。

5.2 在第二页搜索时,搜索不到数据

原因是,数据不够。每次搜索后,结果都是从第一页开始计算。举例,有8条数据,按10页分页,在第二页搜索时,得不到数据,原因是 页面都是page_no ==1中。若是有15条数据,第二页只有5条。第一页铺满
解决方法:

  1. if(name!=null && name!="")
  2. {
  3. page_no =1;
  4. }

在有参数 的渲染函数中,设置页面为第一页。不过只适用于 数据条目少的时候。数据多的时候还没试验过,遇到再记录