点击了不同的分类后,将来看到的旅游线路不一样的。通过分析数据库表结构,发现,旅游线路表和分类表时一个多对一的关系
查询不同分类的旅游线路sql
Select * from tab_route where cid = ?;
类别id的传递
CategoryServiceImpl-findAll()改写
1 @Override
2 public List<Category> findAll() {
3 //1 从redis中查询
4 //1.1 获取jedis客户端
5 Jedis jedis = JedisUtil.getJedis();
6 //1.2 期望数据中存储的顺序就是将来展示的顺序,使用redis的sortedset
7 // Set<String> categorys = jedis.zrange("category", 0, -1);
8 //1.3 查询sortedset中的分数(cid)和值(cname)
9 Set<Tuple> categorys = jedis.zrangeWithScores("category", 0, -1);
10 /*
11 Tuple类内含两个成员变量
12 private byte[] element;
13 private Double score;
14 */
15
16 //2 判断查询结果集合是否为空
17 List<Category> cs = null;
18 if (categorys==null || categorys.size()==0){
19 // System.out.println("从mysql数据库中查询");
20
21 //3 若为空,则需要从数据库查询,并将结果数据存入redis
22 //3.1 从数据库查询
23 cs = categoryDao.findAll();
24 //3.2 将集合数据存储到redis中的 category的key
25 for (int i=0; i<cs.size(); i++){
26 jedis.zadd("category", cs.get(i).getCid(), cs.get(i).getCname()); //键名,分数,键值
27 }
28 }else {
29 // System.out.println("从redis缓存中查询");
30
31 //4 若不为空,直接返回redis缓存结果
32 //因为查询出来的是set集合,而返回结果为list集合,故将set数据存入list(转换)
33 cs = new ArrayList<>();
34 /*for (String name : categorys) {
35 Category category = new Category();
36 category.setCname(name);
37 cs.add(category);
38 }*/
39 for (Tuple tuple : categorys) {
40 Category category = new Category();
41 category.setCname(tuple.getElement());
42 category.setCid((int)tuple.getScore());
43 cs.add(category);
44 }
45 }
46
47 // return categoryDao.findAll();
48 return cs;
49 }
根据id查询不同类别的旅游线路数据(含分页)
1 package cn.haifei.travel.domain; 2 3 import java.util.List; 4 5 /** 6 * 分页对象 7 */ 8 public class PageBean<T> { 9 10 private int totalCount; //总记录数 11 private int totalPage; //总页数 12 private int currentPage; //当前页码 13 private int pageSize; //每页显示的记录条数 14 private List<T> list; //每页显示的数据集合 15 16 public int getTotalCount() { 17 return totalCount; 18 } 19 20 public void setTotalCount(int totalCount) { 21 this.totalCount = totalCount; 22 } 23 24 public int getTotalPage() { 25 return totalPage; 26 } 27 28 public void setTotalPage(int totalPage) { 29 this.totalPage = totalPage; 30 } 31 32 public int getCurrentPage() { 33 return currentPage; 34 } 35 36 public void setCurrentPage(int currentPage) { 37 this.currentPage = currentPage; 38 } 39 40 public int getPageSize() { 41 return pageSize; 42 } 43 44 public void setPageSize(int pageSize) { 45 this.pageSize = pageSize; 46 } 47 48 public List<T> getList() { 49 return list; 50 } 51 52 public void setList(List<T> list) { 53 this.list = list; 54 } 55 }
1 package cn.haifei.travel.web.servlet; 2 3 import cn.haifei.travel.domain.PageBean; 4 import cn.haifei.travel.domain.Route; 5 import cn.haifei.travel.service.RouteService; 6 import cn.haifei.travel.service.impl.RouteServiceImpl; 7 8 import javax.servlet.ServletException; 9 import javax.servlet.annotation.WebServlet; 10 import javax.servlet.http.HttpServlet; 11 import javax.servlet.http.HttpServletRequest; 12 import javax.servlet.http.HttpServletResponse; 13 import java.io.IOException; 14 15 @WebServlet("/route/*") 16 public class RouteServlet extends BaseServlet { 17 18 private RouteService service = new RouteServiceImpl(); 19 20 /** 21 * 分页查询 22 * @param request 23 * @param response 24 * @throws ServletException 25 * @throws IOException 26 */ 27 public void pageQuery(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 28 String currentPageStr = request.getParameter("currentPage"); 29 String pageSizeStr = request.getParameter("pageSize"); 30 String cidStr = request.getParameter("cid"); 31 32 int cid = 0; //category类别id 33 if (cidStr!=null && cidStr.length()>0){ 34 cid = Integer.parseInt(cidStr); 35 } 36 int currentPage = 0; //当前页码,如果不传递,则默认为第一页 37 if (currentPageStr!=null && currentPageStr.length()>0){ 38 currentPage = Integer.parseInt(currentPageStr); 39 }else { 40 currentPage = 1; //指定默认值(首次访问currentPageStr==null) 41 } 42 int pageSize = 0; //每页显示条数,如果不传递,默认每页显示5条记录 43 if (pageSizeStr!=null && pageSizeStr.length()>0){ 44 pageSize = Integer.parseInt(pageSizeStr); 45 }else { 46 pageSize = 5; //指定每页显示记录数的默认值 47 } 48 49 PageBean<Route> pb = service.pageQuery(cid, currentPage, pageSize); 50 51 writeValue(pb, response); 52 } 53 54 }
1 package cn.haifei.travel.service; 2 3 import cn.haifei.travel.domain.PageBean; 4 import cn.haifei.travel.domain.Route; 5 6 /** 7 * 旅游路线的service接口 8 */ 9 public interface RouteService { 10 11 /** 12 * 根据类别进行分页查询 13 * @param cid 14 * @param currentPage 15 * @param pageSize 16 * @return 17 */ 18 public PageBean<Route> pageQuery(int cid, int currentPage, int pageSize); 19 20 }
1 package cn.haifei.travel.service.impl; 2 3 import cn.haifei.travel.dao.RouteDao; 4 import cn.haifei.travel.dao.impl.RouteDaoImpl; 5 import cn.haifei.travel.domain.PageBean; 6 import cn.haifei.travel.domain.Route; 7 import cn.haifei.travel.service.RouteService; 8 9 import java.util.List; 10 11 public class RouteServiceImpl implements RouteService { 12 13 private RouteDao dao = new RouteDaoImpl(); 14 15 @Override 16 public PageBean<Route> pageQuery(int cid, int currentPage, int pageSize) { 17 //封装PageBean 18 PageBean<Route> pb = new PageBean<>(); 19 20 pb.setCurrentPage(currentPage); 21 pb.setPageSize(pageSize); 22 23 int totalCount = dao.findTotalCount(cid); 24 pb.setTotalCount(totalCount); 25 26 int start = (currentPage - 1) * pageSize; 27 List<Route> list = dao.findByPage(cid, start, pageSize); 28 pb.setList(list); 29 30 int totalPage = (totalCount % pageSize == 0) ? (totalCount / pageSize) : (totalCount / pageSize) + 1; 31 pb.setTotalPage(totalPage); 32 33 return pb; 34 } 35 }
1 package cn.haifei.travel.dao; 2 3 import cn.haifei.travel.domain.Route; 4 5 import java.util.List; 6 7 public interface RouteDao { 8 9 /** 10 * 根据cid查询总记录数 11 * @param cid 12 * @return 13 */ 14 public int findTotalCount(int cid); 15 16 17 /** 18 * 根据cid start pageSize查询当前页的数据集合 19 * @param cid 20 * @param start 21 * @param pageSize 22 * @return 23 */ 24 public List<Route> findByPage(int cid, int start, int pageSize); 25 26 }
1 package cn.haifei.travel.dao.impl; 2 3 import cn.haifei.travel.dao.RouteDao; 4 import cn.haifei.travel.domain.Route; 5 import cn.haifei.travel.util.JDBCUtils; 6 import org.springframework.jdbc.core.BeanPropertyRowMapper; 7 import org.springframework.jdbc.core.JdbcTemplate; 8 9 import java.util.List; 10 11 public class RouteDaoImpl implements RouteDao { 12 13 JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource()); 14 15 @Override 16 public int findTotalCount(int cid) { 17 String sql = "select count(*) from tab_route where cid = ?"; 18 return template.queryForObject(sql, Integer.class, cid); //实际返回Integer,预期返回int,自动拆箱了 19 } 20 21 @Override 22 public List<Route> findByPage(int cid, int start, int pageSize) { 23 String sql = "select * from tab_route where cid = ? limit ?,?"; 24 return template.query(sql, new BeanPropertyRowMapper<>(Route.class), cid, start, pageSize); 25 } 26 }
1 <!DOCTYPE html> 2 <html lang="en"> 3 4 <head> 5 <meta charset="UTF-8"> 6 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 7 <meta http-equiv="X-UA-Compatible" content="ie=edge"> 8 <title>黑马旅游-搜索</title> 9 <link rel="stylesheet" type="text/css" href="css/common.css"> 10 <link rel="stylesheet" href="css/search.css"> 11 <script src="js/jquery-3.3.1.js"></script> 12 <script> 13 $(function () { 14 var search = location.search; //BOM-location-search:设置或返回从?开始的URL 15 // alert(search); //?cid=5 16 17 var cid = search.split("=")[1]; //切割字符串,获取cid 18 // alert(cid); 19 20 //当页码加载完成后,调用load方法,发送ajax请求加载数据 21 load(cid); 22 }); 23 24 function load(cid, currentPage) { 25 //发送ajax请求,请求route/pageQuery,传递cid 26 $.get( 27 "route/pageQuery", 28 {cid:cid, currentPage:currentPage}, 29 function (pb) { //解析pagebean数据,展示到页面上 30 // ========================================================================== 31 //1 分页工具条展示 32 33 //1.1 展示总页码和总记录数 34 $("#totalPage").html(pb.totalPage); 35 $("#totalCount").html(pb.totalCount); 36 37 //1.2 展示分页页码 38 var lis = ""; 39 var beforeNum = pb.currentPage - 1; //上一页页码 40 if (beforeNum <= 0){ 41 beforeNum = 1; 42 } 43 44 var firstPage = '<li onclick="javascripy:load('+ cid +','+ 1 +')"><a href="javascript:void(0)">首页</a></li>'; 45 var beforePage = '<li onclick="javascripy:load('+ cid +','+ beforeNum +')"><a href="javascript:void(0)">上一页</a></li>'; 46 lis += firstPage; 47 lis += beforePage; 48 49 /*for (var i=1; i<=pb.totalPage; i++){ 50 var li; 51 if (currentPage == i){ //当前页码添加样式 52 li = '<li class="curPage" onclick="javascripy:load('+ cid +','+ i +')"><a href="javascript:void(0)">'+ i +'</a></li>'; 53 }else { 54 li = '<li onclick="javascripy:load('+ cid +','+ i +')"><a href="javascript:void(0)">'+ i +'</a></li>'; //递归 55 } 56 lis += li; 57 }*/ 58 //------------------------------------------------------------------------- 59 /* 60 1.一共展示10个页码,能够达到前5后4的效果 61 2.如果前边不够5个,后边补齐10个 62 3.如果后边不足4个,前边补齐10个 63 */ 64 var begin; // 开始位置 65 var end ; // 结束位置 66 67 //1 显示10个页码 68 if (pb.totalPage < 10){ 69 //总页码不够10页 70 begin = 1; 71 end = pb.totalPage; 72 }else { 73 //总页码超过10页 74 begin = pb.currentPage - 5; 75 end = pb.currentPage + 4; 76 77 //2 如果前边不够5个,后边补齐10个 78 if (begin < 1){ 79 begin = 1; 80 end = begin + 9; 81 } 82 //3 如果后边不足4个,前边补齐10个 83 if (end > pb.totalPage){ 84 end = pb.totalPage; 85 begin = end - 9; 86 } 87 } 88 89 for (var i=begin; i<=end; i++){ 90 var li; 91 if (currentPage == i){ //当前页码添加样式 92 li = '<li class="curPage" onclick="javascripy:load('+ cid +','+ i +')"><a href="javascript:void(0)">'+ i +'</a></li>'; 93 }else { 94 li = '<li onclick="javascripy:load('+ cid +','+ i +')"><a href="javascript:void(0)">'+ i +'</a></li>'; //递归 95 } 96 lis += li; 97 } 98 //------------------------------------------------------------------------- 99 100 var nextNum = pb.currentPage + 1; //下一页页码 101 if (nextNum > pb.totalPage){ 102 nextNum = pb.totalPage; 103 } 104 105 var nextPage = '<li onclick="javascripy:load('+ cid +','+ nextNum +')"><a href="javascript:void(0)">下一页</a></li>'; 106 var lastPage = '<li onclick="javascripy:load('+ cid +','+ pb.totalPage +')"><a href="javascript:void(0)">尾页</a></li>'; 107 lis += nextPage; 108 lis += lastPage; 109 110 $("#pageNum").html(lis); 111 112 //2 列表数据展示 113 var route_lis = ""; 114 for (var i=0; i<pb.list.length; i++){ 115 var route = pb.list[i]; //获取json格式的route对象,{rid:1,rname:"xxx",...} 116 var li = '<li>\n' + 117 ' <div class="img"><img src="'+ route.rimage +'" style="width: 299px"></div>\n' + 118 ' <div class="text1">\n' + 119 ' <p>'+ route.rname +'</p>\n' + 120 ' <br/>\n' + 121 ' <p>'+ route.routeIntroduce +'</p>\n' + 122 ' </div>\n' + 123 ' <div class="price">\n' + 124 ' <p class="price_num">\n' + 125 ' <span>¥</span>\n' + 126 ' <span>'+ route.price +'</span>\n' + 127 ' <span>起</span>\n' + 128 ' </p>\n' + 129 ' <p><a href="route_detail.html">查看详情</a></p>\n' + 130 ' </div>\n' + 131 ' </li>'; 132 route_lis += li; 133 } 134 $("#route").html(route_lis); 135 window.scrollTo(0,0); //换页后定位到页面顶部 136 // ========================================================================== 137 }, 138 "json" 139 ); 140 } 141 </script> 142 </head> 143 <body> 144 <!--引入头部--> 145 <div id="header"></div> 146 <div class="page_one"> 147 <div class="contant"> 148 <div class="crumbs"> 149 <img src="images/search.png" alt=""> 150 <p>黑马旅行><span>搜索结果</span></p> 151 </div> 152 <div class="xinxi clearfix"> 153 <div class="left"> 154 <div class="header"> 155 <span>商品信息</span> 156 <span class="jg">价格</span> 157 </div> 158 <ul id="route"> 159 160 </ul> 161 <div class="page_num_inf"> 162 <i></i> 共 163 <span id="totalPage"></span>页<span id="totalCount"></span>条 164 </div> 165 <div class="pageNum"> 166 <ul id="pageNum"> 167 168 </ul> 169 </div> 170 </div> 171 <div class="right"> 172 <div class="top"> 173 <div class="hot">HOT</div> 174 <span>热门推荐</span> 175 </div> 176 <ul> 177 <li> 178 <div class="left"><img src="images/04-search_09.jpg" alt=""></div> 179 <div class="right"> 180 <p>清远新银盏温泉度假村酒店/自由行套...</p> 181 <p>网付价<span>¥<span>899</span>起</span> 182 </p> 183 </div> 184 </li> 185 <li> 186 <div class="left"><img src="images/04-search_09.jpg" alt=""></div> 187 <div class="right"> 188 <p>清远新银盏温泉度假村酒店/自由行套...</p> 189 <p>网付价<span>¥<span>899</span>起</span> 190 </p> 191 </div> 192 </li> 193 <li> 194 <div class="left"><img src="images/04-search_09.jpg" alt=""></div> 195 <div class="right"> 196 <p>清远新银盏温泉度假村酒店/自由行套...</p> 197 <p>网付价<span>¥<span>899</span>起</span> 198 </p> 199 </div> 200 </li> 201 <li> 202 <div class="left"><img src="images/04-search_09.jpg" alt=""></div> 203 <div class="right"> 204 <p>清远新银盏温泉度假村酒店/自由行套...</p> 205 <p>网付价<span>¥<span>899</span>起</span> 206 </p> 207 </div> 208 </li> 209 <li> 210 <div class="left"><img src="images/04-search_09.jpg" alt=""></div> 211 <div class="right"> 212 <p>清远新银盏温泉度假村酒店/自由行套...</p> 213 <p>网付价<span>¥<span>899</span>起</span> 214 </p> 215 </div> 216 </li> 217 </ul> 218 </div> 219 </div> 220 </div> 221 </div> 222 223 <!--引入头部--> 224 <div id="footer"></div> 225 <!--导入布局js,共享header和footer--> 226 <script type="text/javascript" src="js/include.js"></script> 227 </body> 228 229 </html>