分类数据展示功能
分析
代码实现
后台代码实现
BaseServlet类:
将对象序列化为json方法进行抽取
/** * 直接将传入的对象序列化为json,并且写回客户端 * @param obj */ public void writeValue(Object obj,HttpServletResponse response) throws IOException { ObjectMapper mapper = new ObjectMapper(); response.setContentType("application/json;charset=utf-8"); mapper.writeValue(response.getOutputStream(),obj); } /** * 将传入的对象序列化为json,返回 * @param obj */ public String writeValueAsString(Object obj,HttpServletResponse response) throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); return mapper.writeValueAsString(obj); }
CategoryServlet类:
@WebServlet("/category/*") public class CategoryServlet extends BaseServlet { private CategoryService service = new CategoryServiceImpl(); /** * 查询所有 * @param request * @param response * @throws ServletException * @throws IOException */ public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1、调用service查询所有 List<Category> cs = service.findAll(); //2、序列化json返回 /*ObjectMapper mapper = new ObjectMapper(); response.setContentType("application/json;charset=utf-8"); mapper.writeValue(response.getOutputStream(),cs);*/ writeValue(cs,response); } }
CategoryService接口:
public interface CategoryService { public List<Category> findAll(); }
CategoryServiceImpl实现类:
public class CategoryServiceImpl implements CategoryService { private CategoryDao categoryDao = new CategoryDaoImpl(); @Override public List<Category> findAll() { return categoryDao.findAll(); } }
CategoryDao接口:
public interface CategoryDao { /** * 查询所有 * @return */ public List<Category> findAll(); }
CategoryDaoImpl实现类:
public class CategoryDaoImpl implements CategoryDao { private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource()); @Override public List<Category> findAll() { String sql = "select * from tab_category"; return template.query(sql,new BeanPropertyRowMapper<>(Category.class)); } }
前台代码
header.html被请求后,请求/category/findAll获取数据
div class="navitem"> <ul id="category" class="nav"> <!--<li class="nav-active"><a href="index.html">首页</a></li> <li><a href="route_list.html">门票</a></li> <li><a href="route_list.html">酒店</a></li> <li><a href="route_list.html">香港车票</a></li> <li><a href="route_list.html">出境游</a></li> <li><a href="route_list.html">国内游</a></li> <li><a href="route_list.html">港澳游</a></li> <li><a href="route_list.html">抱团定制</a></li> <li><a href="route_list.html">全球自由行</a></li> <li><a href="favoriterank.html">收藏排行榜</a></li>--> </ul> </div>
<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">'+data[i].cname+'</a></li>'; lis+=li; } lis+='<li><a href="favoriterank.html">收藏排行榜</a></li>'; //将lis字符串,设置到ul的html内容中 $("#category").html(lis); }); }); </script>
对分类数据进行缓存优化
分析发现,分类的数据在每一次页面加载后都会重新请求数据库来加载,对数据库的压力
比较大,而且分类的数据不会经常产生变化,所有可以使用redis来缓存这个数据
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); 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 (String name : categorys) { Category category = new Category(); category.setCname(name); cs.add(category); } } return cs; } }