------------恢复内容开始------------
旅游线路-类别id传递
点击了不同的分类后,将来看到的旅游线路不一样的。通过分析数据库表结构,发现旅游线路表和分类表是一个多对一的关系
CategoryServiceImpl实现类
package cn.itcast.travel.service.impl; import cn.itcast.travel.dao.CategoryDao; import cn.itcast.travel.dao.impl.CategoryDaoImpl; import cn.itcast.travel.domain.Category; import cn.itcast.travel.service.CategoryService; import cn.itcast.travel.util.JDBCUtils; import cn.itcast.travel.util.JedisUtil; import redis.clients.jedis.Jedis; import redis.clients.jedis.Tuple; import java.util.ArrayList; import java.util.List; import java.util.Set; public class CategoryServiceImpl implements CategoryService { private CategoryDao categoryDao = new CategoryDaoImpl(); @Override public List<Category> findAll() { //1. 从redis中查询 //1.1获取jedis客户端 Jedis jedis = JedisUtil.getJedis();//工具类 //1.2可使用sortedset排序查询 //Set<String> categorys = jedis.zrange("category", 0, -1);//范围查询 //1.3查询sortedset中的分数(cid)和值(cname) Set<Tuple> categorys = jedis.zrangeWithScores("category", 0, -1); //2.判断查询的集合是否为空 List<Category> cs = null; if (categorys == null || categorys.size() ==0){ System.out.println("从数据库查询..."); //3. 如果为空,需要从数据库查询,在将数据存入redis //3.1 从数据库查询 cs = categoryDao.findAll(); //3.2 将集合数据存储到redis中的 category的key for (int i = 0; i < cs.size(); i++) { jedis.zadd("category",cs.get(i).getCid(),cs.get(i).getCname()); } }else {// 进行数据结构的统一 set集合中的缓存数据存储到list集合当中 (转换代码) System.out.println("从redis中查询..."); //4.如果不为空,将set数据存入list cs = new ArrayList<Category>(); for (Tuple tuple : categorys) { Category category = new Category(); category.setCname(tuple.getElement());// 设置名字 category.setCid((int) tuple.getScore());// 设置id cs.add(category); } } return cs; } }
header.html页面
<script> $(function () { //查询用户登录名字信息 $.get("user/findOne",{},function (data) { //{uid:1,name:'李四'} var msg = "欢迎回来,"+data.name; $("#span_username").html(msg); }); //查询分页数据 $.get("category/findAll",{},function (data) { // [{cid,1},{cname,国内游},{}] 数组 var lis = '<li class="nav-active"><a href="index.html">首页</a></li>'; //遍历数组,拼接字符串(<li>) for (let 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; } // 拼接收藏排行榜的li,<li><a href="favoriterank.html">收藏排行榜</a></li> lis+= '<li><a href="favoriterank.html">收藏排行榜</a></li>'; //将list字符串,设置到ul的html内容中 $("#category").html(lis); }); }); </script>
route_list.html页面
<script> $(function () { let search = location.search;// 查询参数 //alert(search);// ?cid=5 // 切割字符串,拿到第二个值 let cid = search.split("=")[1];//截取 //alert(cid); }) </script>
旅游线路-分页数据展示分析
创建PageBean对象:
package cn.itcast.travel.domain; import java.util.List; /** * 分页对象 */ 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类
package cn.itcast.travel.web.servlet; import cn.itcast.travel.domain.PageBean; import cn.itcast.travel.domain.Route; import cn.itcast.travel.service.RouteService; import cn.itcast.travel.service.impl.RouteServiceImpl; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/route/*") public class RouteServlet extends BaseServlet{ private RouteService routeService = new RouteServiceImpl(); /** * 分页查询 * @param req * @param resp * @throws ServletException * @throws IOException */ public void pageQuery(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //1.接收参数 String currentPageStr = req.getParameter("currentPage");// 当前页码 String pageSizeStr = req.getParameter("pageSize");// 每页显示的条数 String cidStr = req.getParameter("cid");// 分页id int cid = 0;// 类别id //2.处理参数 if (cidStr != null && cidStr.length() > 0){ cid = Integer.parseInt(cidStr); } int currentPage = 0;//当前页码 如果不传递,则默认为第一页 if (currentPageStr != null && currentPageStr.length() > 0){ // 等于null 或者长度小于0 else 让他为1 currentPage = Integer.parseInt(currentPageStr); }else { currentPage = 1; } int pageSize = 0;// 每页显示条数,如果不传递,默认每页显示五条记录 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, resp); } }
RouteService接口
package cn.itcast.travel.service; import cn.itcast.travel.domain.PageBean; import cn.itcast.travel.domain.Route; /** * 线路Service */ public interface RouteService { /** * 分页查询 根据类别进行分页查询 * @param cid * @param currentPage * @param pageSize * @return */ public PageBean<Route> pageQuery(int cid,int currentPage,int pageSize); }
RouteServiceImpl实现类
package cn.itcast.travel.service.impl; import cn.itcast.travel.dao.RouteDao; import cn.itcast.travel.dao.impl.RouteDaoImpl; import cn.itcast.travel.domain.PageBean; import cn.itcast.travel.domain.Route; import cn.itcast.travel.service.RouteService; import java.util.List; 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 totalCont = routeDao.findTotalCont(cid); pb.setTotalCount(totalCont); //设置当前页显示的数据集合 int start = (currentPage - 1) * pageSize;//开始记录数 List<Route> list = routeDao.findByPage(cid, start, pageSize); pb.setList(list); //设置总页数 = 总记录数 / 每页显示条数 int totalPage = totalCont % pageSize == 0 ? totalCont / pageSize : (totalCont / pageSize) +1; pb.setTotalPage(totalPage); return pb; } }
RouteDao接口
package cn.itcast.travel.dao; import cn.itcast.travel.domain.Route; import java.util.List; /** * 分页查询 */ public interface RouteDao { /** * 根据cid查询总记录数 */ public int findTotalCont(int cid); /** * 根据cid,start,pageSize查询当前页的数据集合 */ public List<Route> findByPage(int cid,int start,int pageSize); }
RouteDaoImpl实现类
package cn.itcast.travel.dao.impl; import cn.itcast.travel.dao.RouteDao; import cn.itcast.travel.domain.Route; import cn.itcast.travel.util.JDBCUtils; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import java.util.List; public class RouteDaoImpl implements RouteDao { private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource()); /** * 查询总记录数 * @param cid * @return */ @Override public int findTotalCont(int cid) { String sql = "select count(*) from tab_route where cid = ?"; return template.queryForObject(sql, Integer.class,cid); } /** * 分页查询 * @param cid * @param start * @param pageSize * @return */ @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); } }
旅游线路-分页展示前台代码
route_list.html页面
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>黑马旅游-搜索</title> <link rel="stylesheet" type="text/css" href="css/common.css"> <link rel="stylesheet" href="css/search.css"> <script src="js/jquery-3.3.1.js"></script> <script> $(function () { let search = location.search;// 查询参数 //alert(search);// ?cid=5 // 切割字符串,拿到第二个值 let cid = search.split("=")[1];//截取 //alert(cid); //当页码加载完成后,调用load方法,发送ajax请求加载数据 laod(cid); }); function laod(cid,currentPage) { //发送ajax请求,请求route/pageQuery,传递cid $.get("route/pageQuery",{cid:cid,currentPage:currentPage},function (pb) { //解析pageBean数据,展示到页面上 //1.分页工具条数据展示 //1.1展示总页码和总记录数 $("#totalPage").html(pb.totalPage); $("#totalCount").html(pb.totalCount); /* <li><a href="">首页</a></li> <li class="threeword"><a href="#">上一页</a></li> <li class="curPage"><a href="#">1</a></li><!-- curPage:当前页码 --> <li><a href="#">2</a></li> <li><a href="#">3</a></li> <li><a href="#">4</a></li> <li><a href="#">5</a></li> <li><a href="#">6</a></li> <li><a href="#">7</a></li> <li><a href="#">8</a></li> <li><a href="#">9</a></li> <li><a href="#">10</a></li> <li class="threeword"><a href="javascript:;">下一页</a></li> <li class="threeword"><a href="javascript:;">末页</a></li> */ var lis = ""; var frisPage = '<li onclick="laod('+cid+')"><a href="javascript:void(0)">首页</a></li>'; //计算上一页的页码 var beforeNum = pb.currentPage - 1; if (beforeNum <= 0){ beforeNum = 1; } var beforePage = '<li onclick="laod('+cid+','+beforeNum+')" class="threeword"><a>上一页</a></li>'; lis += frisPage; lis += beforePage; //1.2展示分页页码 /* 1.一共展示10个页码,能够达到前5后4的效果 2.如果前边不够5个,后边补齐10个 3.如果后边不足4个,前边补齐10个 */ // 定义开始位置begin,结束位置 end var begin; var end; //1.要显示10个页码 if (pb.totalPage < 10){ //总页码不够10页 begin = 1; end = pb.totalPage; }else { //总页码超过10页 begin = pb.currentPage - 5; end = pb.currentPage + 4; //2.如果前边不够5个,后边补齐10个 if (begin < 1){ begin = 1; end = begin + 9; } //3.如果后边不足4个,前边补齐10个 if (end > pb.totalPage){ end = pb.totalPage; begin = end - 9; } } for (let i = begin; i <= end; i++) { var li; //判断当前页码是否等于i if (pb.currentPage == i){ li = '<li class="curPage" onclick="javascript:laod('+cid+','+i+')"><a href="javascript:vo(0)">'+i+'</a></li>'; }else { //创建一个页面li li = '<li onclick="javascript:laod('+cid+','+i+')"><a href="javascript:vo(0)">'+i+'</a></li>'; } //拼接字符串 lis += li; } // //1.2展示分页页码 // for (let i = 1; i <= pb.totalPage; i++) { // var li; // //判断当前页码是否等于i // if (pb.currentPage == i){ // li = '<li class="curPage" onclick="javascript:laod('+cid+','+i+')"><a href="javascript:vo(0)">'+i+'</a></li>'; // }else { // //创建一个页面li // li = '<li onclick="javascript:laod('+cid+','+i+')"><a href="javascript:vo(0)">'+i+'</a></li>'; // } // // //拼接字符串 // lis += li; // } //计算下一页的页码 var afterNum = pb.currentPage + 1; if (afterNum > pb.totalPage){ afterNum = pb.totalPage; } var nextPage = '<li onclick="laod('+cid+','+afterNum+')"><a href="javascript:void(0)">下一页</a></li>'; var lastPage = '<li onclick="laod('+cid+','+pb.totalPage+')" ><a href="javascript:void(0);">末页</a></li>'; lis += nextPage; lis += lastPage; //将lis内容设置到ul页面中 $("#pageNum").html(lis); /* <li> <div class="img"><img src="images/04-search_03.jpg" alt=""></div> <div class="text1"> <p>【减100元 含除夕/春节出发】广州增城三英温泉度假酒店/自由行套票</p> <br/> <p>1-2月出发,网付立享¥1099/2人起!爆款位置有限,抢完即止!</p> </div> <div class="price"> <p class="price_num"> <span>¥</span> <span>299</span> <span>起</span> </p> <p><a href="route_detail.html">查看详情</a></p> </div> </li> */ //2.列表数据展示 遍历输出 var route_lis = ""; for (let i = 0; i < pb.list.length; i++) { //获取[rid:rname:"xxx"] var route = pb.list[i]; var li = '<li>\n' + ' <div class="img"><img src="'+route.rimage+'" style="width: 299px;height: 169px"></div>\n' + ' <div class="text1">\n' + ' <p>'+route.rname+'</p>\n' + ' <br/>\n' + ' <p>'+route.routeIntroduce+'</p>\n' + ' </div>\n' + ' <div class="price">\n' + ' <p class="price_num">\n' + ' <span>¥</span>\n' + ' <span>'+route.price+'</span>\n' + ' <span>起</span>\n' + ' </p>\n' + ' <p>< a href="route_detail.html">查看详情</ a></p>\n' + ' </div>\n' + '</li>'; //拼接 route_lis += li; } $("#route").html(route_lis); //定位到页面顶部 window.scrollTo(0,0); }); } </script> </head> <body> <!--引入头部--> <div id="header"></div> <div class="page_one"> <div class="contant"> <div class="crumbs"> <img src="images/search.png" alt=""> <p>黑马旅行><span>搜索结果</span></p> </div> <div class="xinxi clearfix"> <div class="left"> <div class="header"> <span>商品信息</span> <span class="jg">价格</span> </div> <ul id="route"> <li> <div class="img"><img src="images/04-search_03.jpg" alt=""></div> <div class="text1"> <p>【减100元 含除夕/春节出发】广州增城三英温泉度假酒店/自由行套票</p> <br/> <p>1-2月出发,网付立享¥1099/2人起!爆款位置有限,抢完即止!</p> </div> <div class="price"> <p class="price_num"> <span>¥</span> <span>299</span> <span>起</span> </p> <p><a href="route_detail.html">查看详情</a></p> </div> </li> <li> <div class="img"><img src="images/04-search_03.jpg" alt=""></div> <div class="text1"> <p>浪花朵朵旅行普吉岛丛林飞跃空中飞人探险游中文服务泰国旅游</p> <br/> <p>1-2月出发,网付立享¥1099/2人起!爆款位置有限,抢完即止!</p> </div> <div class="price"> <p class="price_num"> <span>¥</span> <span>899</span> <span>起</span> </p> <p><a href="route_detail.html">查看详情</a></p> </div> </li> <li> <div class="img"><img src="images/04-search_03.jpg" alt=""></div> <div class="text1"> <p>黑妞皇家旅行普吉岛攀牙湾大船星光之旅皮划艇日落休闲特色体验</p> <br/> <p>1-2月出发,网付立享¥1099/2人起!爆款位置有限,抢完即止!</p> </div> <div class="price"> <p class="price_num"> <span>¥</span> <span>999</span> <span>起</span> </p> <p><a href="route_detail.html">查看详情</a></p> </div> </li> <li> <div class="img"><img src="images/04-search_03.jpg" alt=""></div> <div class="text1"> <p>浪花朵朵旅行普吉岛皇帝岛珊瑚岛香蕉船拖拽伞水上项目</p> <br/> <p>1-2月出发,网付立享¥1099/2人起!爆款位置有限,抢完即止!</p> </div> <div class="price"> <p class="price_num"> <span>¥</span> <span>99</span> <span>起</span> </p> <p><a href="route_detail.html">查看详情</a></p> </div> </li> <li> <div class="img"><img src="images/04-search_03.jpg" alt=""></div> <div class="text1"> <p>环游记 泰国清迈Lila massage女子监狱spa 丽菈泰式按摩马杀鸡</p> <br/> <p>1-2月出发,网付立享¥1099/2人起!爆款位置有限,抢完即止!</p> </div> <div class="price"> <p class="price_num"> <span>¥</span> <span>199</span> <span>起</span> </p> <p><a href="route_detail.html">查看详情</a></p> </div> </li> <li> <div class="img"><img src="images/04-search_03.jpg" alt=""></div> <div class="text1"> <p>【减100元 含除夕/春节出发】广州增城三英温泉度假酒店/自由行套票</p> <br/> <p>1-2月出发,网付立享¥1099/2人起!爆款位置有限,抢完即止!</p> </div> <div class="price"> <p class="price_num"> <span>¥</span> <span>899</span> <span>起</span> </p> <p><a href="route_detail.html">查看详情</a></p> </div> </li> <li> <div class="img"><img src="images/04-search_03.jpg" alt=""></div> <div class="text1"> <p>【减100元 含除夕/春节出发】广州增城三英温泉度假酒店/自由行套票</p> <br/> <p>1-2月出发,网付立享¥1099/2人起!爆款位置有限,抢完即止!</p> </div> <div class="price"> <p class="price_num"> <span>¥</span> <span>1199</span> <span>起</span> </p> <p><a href="route_detail.html">查看详情</a></p> </div> </li> <li> <div class="img"><img src="images/04-search_03.jpg" alt=""></div> <div class="text1"> <p>泰国芭提雅三合一日游芭提雅蒂芬妮人妖秀成人门票bigeye含接送</p> <br/> <p>1-2月出发,网付立享¥1099/2人起!爆款位置有限,抢完即止!</p> </div> <div class="price"> <p class="price_num"> <span>¥</span> <span>1589</span> <span>起</span> </p> <p><a href="route_detail.html">查看详情</a></p> </div> </li> </ul> <div class="page_num_inf"> <i></i> 共 <span id="totalPage">12</span>页<span id="totalCount">132</span>条 </div> <div class="pageNum"> <ul id="pageNum"> <li><a href="">首页</a></li> <li class="threeword"><a href="#">上一页</a></li> <li class="curPage"><a href="#">1</a></li><!-- curPage:当前页码 --> <li><a href="#">2</a></li> <li><a href="#">3</a></li> <li><a href="#">4</a></li> <li><a href="#">5</a></li> <li><a href="#">6</a></li> <li><a href="#">7</a></li> <li><a href="#">8</a></li> <li><a href="#">9</a></li> <li><a href="#">10</a></li> <li class="threeword"><a href="javascript:;">下一页</a></li> <li class="threeword"><a href="javascript:;">末页</a></li> </ul> </div> </div> <div class="right"> <div class="top"> <div class="hot">HOT</div> <span>热门推荐</span> </div> <ul> <li> <div class="left"><img src="images/04-search_09.jpg" alt=""></div> <div class="right"> <p>清远新银盏温泉度假村酒店/自由行套...</p> <p>网付价<span>¥<span>899</span>起</span> </p> </div> </li> <li> <div class="left"><img src="images/04-search_09.jpg" alt=""></div> <div class="right"> <p>清远新银盏温泉度假村酒店/自由行套...</p> <p>网付价<span>¥<span>899</span>起</span> </p> </div> </li> <li> <div class="left"><img src="images/04-search_09.jpg" alt=""></div> <div class="right"> <p>清远新银盏温泉度假村酒店/自由行套...</p> <p>网付价<span>¥<span>899</span>起</span> </p> </div> </li> <li> <div class="left"><img src="images/04-search_09.jpg" alt=""></div> <div class="right"> <p>清远新银盏温泉度假村酒店/自由行套...</p> <p>网付价<span>¥<span>899</span>起</span> </p> </div> </li> <li> <div class="left"><img src="images/04-search_09.jpg" alt=""></div> <div class="right"> <p>清远新银盏温泉度假村酒店/自由行套...</p> <p>网付价<span>¥<span>899</span>起</span> </p> </div> </li> </ul> </div> </div> </div> </div> <!--引入头部--> <div id="footer"></div> <!--导入布局js,共享header和footer--> <script type="text/javascript" src="js/include.js"></script> </body> </html>
标签:itcast,分页,cid,int,代码,展示,import,travel,cn From: https://www.cnblogs.com/qihaokuan/p/17141111.html