首页 > 其他分享 >分页数据展示后台代码

分页数据展示后台代码

时间:2022-08-25 15:26:16浏览次数:58  
标签:return 分页 pageSize 展示 int cid 后台 currentPage public

类别id传递

点击了不同的分类后将来看到的旅游线路不一样的。通过分析数据库表结构,发现

旅游线路表和分类表是一个多对一的关系

 

 

 CategoryServiceImpl实现类:

public class CategoryServiceImpl implements CategoryService {
    private CategoryDao categoryDao = new CategoryDaoImpl();

    @Override
    public List<Category> findAll() {
        //1、从redis中查询
        //获取jedis客户端
        Jedis jedis = JedisUtil.getJedis();
        //可以使用sortedset排序查询
//        Set<String> categorys = jedis.zrange("category", 0, -1);
        //1.3查询sortedset中的分数(cid)和值(cname)
        Set<Tuple> categorys = jedis.zrangeWithScores("category", 0, -1);


        List<Category> cs = null;
        //2、判断查询的集合是否为空
        if (categorys == null || categorys.size()==0){
            System.out.println("从数据库查询.....");
            //3、如果为空,需要从数据库查询,再将数据存入redis
            //从数据库查询
            cs = categoryDao.findAll();
            //将数据集合存储到redis中,category的key
            for (int i = 0; i < cs.size(); i++) {
                jedis.zadd("category",cs.get(i).getCid(),cs.get(i).getCname());
            }
        }else {
            System.out.println("从redis查询.....");
            //4、如果不为空,直接返回
            cs = new ArrayList<>();
            for (Tuple tuple : categorys) {
                Category category = new Category();
                category.setCname(tuple.getElement());
                category.setCid((int) tuple.getScore());
                cs.add(category);
            }
        }
        return cs;
    }
}

 header.html类:

<script>
    $(function () {
        $.get("user/findOne",{},function (data) {
            var msg = "欢迎回来,"+data.name;
            $("#span_username").html(msg);
        });

        $.get("category/findAll",{},function (data) {
            var lis = '<li class="nav-active"><a href="index.html">首页</a></li>';
            //遍历数组,拼接字符串
            for (var i = 0; i < data.length; i++) {
                var li = '<li><a href="route_list.html?cid='+data[i].cid+'">'+data[i].cname+'</a></li>';
                lis+=li;
            }
            lis+='<li><a href="favoriterank.html">收藏排行榜</a></li>';
            //将lis字符串,设置到ul的html内容中
            $("#category").html(lis);
        });
    });
</script>

 

route_list.html页面:

    <script>
        $(function () {
            var search = location.search;
            //切割字符串,拿到第二个值
            var id = search.split("=")[1];
            alert(id)
        });
    </script>

 

 

 

 

分页数据展示分析

 

 

 

 

创建PageBean对象:

/**
 * 分页对象
 */
public class PageBean<T> {
    private int totalCount;//总记录数
    private int totalPage;//总页数
    private int currentPage;//当前页码
    private int pageSize;//每页显示的条数

    private List<T> list;//每页显示的数据集合

    @Override
    public String toString() {
        return "PageBean{" +
                "totalCount=" + totalCount +
                ", totalPage=" + totalPage +
                ", currentPage=" + currentPage +
                ", pageSize=" + pageSize +
                ", list=" + list +
                '}';
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    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;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }
}

RouteServlet类:

@WebServlet("/route/*")
public class RouteServlet extends BaseServlet {
    private RouteService routeService = new RouteServiceImpl();
    /**
     * 分页查找
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void pageQuery(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收参数
        String currentPageStr = request.getParameter("currentPage");
        String pageSizeStr = request.getParameter("pageSize");
        String cidstr = request.getParameter("cid");
        int cid = 0;
        //2、处理参数
        if (cidstr != null && cidstr.length()>0){
            cid = Integer.parseInt(cidstr);
        }
        int currentPage = 0;//当前页码,如果不传递,则默认为第一页
        if (currentPageStr!=null && currentPageStr.length()>0){
            currentPage = Integer.parseInt(currentPageStr);
        }else {
            currentPage = 1;
        }
        int pageSize = 0;//每页显示条数,如果不传递,默认每页显示5条记录
        if (pageSizeStr != null && pageSizeStr.length()==0){
            pageSize = Integer.parseInt(pageSizeStr);
        }else {
            pageSize = 5;
        }

        //3、调用service查询pageBean对象
        PageBean<Route> pb = routeService.pageQuery(cid, currentPage, pageSize);
        //4、将pageBean对象序列化为json,返回
        writeValue(pb,response);
    }
}

RouteService接口:

/**
 * 线路service
 */
public interface RouteService {
    /**
     * 根据类别进行分页查询
     * @param cid
     * @param currentPage
     * @param pageSize
     * @return
     */
    public PageBean<Route> pageQuery(int cid,int currentPage,int pageSize);
}

RouteServiceIpl实现类:

public class RouteServiceImpl implements RouteService {
    private RouteDao routeDao = new RouteDaoImpl();

    @Override
    public PageBean<Route> pageQuery(int cid, int currentPage, int pageSize) {
        //封装PageBean
        PageBean<Route> pb = new PageBean<>();
        //设置当前页码
        pb.setCurrentPage(currentPage);
        //设置每页显示条数
        pb.setPageSize(pageSize);

        //设置总记录数
        int totalCount = routeDao.findTotalCount(cid);
        pb.setTotalCount(totalCount);
        //设置当前页显示的数据集合
        int start = (currentPage-1)*pageSize;
        List<Route> list = routeDao.findByPage(cid, start, pageSize);
        pb.setList(list);

        //设置总页数 = 总记录数/每页显示条数
        int totalPage = totalCount%pageSize==0 ? totalCount/pageSize : (totalCount/pageSize)+1;
        pb.setTotalPage(totalPage);

        return pb;
    }
}

RouteDao接口:

public interface RouteDao {
    /**
     * 根据cid查询总记录数
     */
    public int findTotalCount(int cid);
    /**
     * 根据cid,start,pageSize查询当前页的数据集合
     */
    public List<Route> findByPage(int cid,int start,int pageSize);
}

RouteDaoImpl实现类:

public class RouteDaoImpl implements RouteDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public int findTotalCount(int cid) {
        String sql = "select count(*) from tab_route where cid = ?";
        return template.queryForObject(sql, Integer.class, cid);
    }

    @Override
    public List<Route> findByPage(int cid, int start, int pageSize) {
        String sql = "select * from tab_route where cid = ? limit ?,?";
        return template.query(sql, new BeanPropertyRowMapper<>(Route.class), cid, start, pageSize);
    }
}

 

标签:return,分页,pageSize,展示,int,cid,后台,currentPage,public
From: https://www.cnblogs.com/xjw12345/p/16624033.html

相关文章