其他分享
首页 > 其他分享> > 《黑马旅游网》综合案例(六)分类数据展示功能

《黑马旅游网》综合案例(六)分类数据展示功能

作者:互联网

分类数据展示功能

分析:

 

 

 代码实现:后台代码:

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>(Category.class));
    }
}

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

CategoryServlet:

@WebServlet("/category/*")
public class CategoryServlet extends BaseServlet {
    private CategoryService service = new CategoryServiceImpl();
    public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //调用service查询所有
        List<Category> cs = service.findAll();
        writeValue(cs,response);
    }
}

在父类中添加两个方法:(方便一些操作)

BaseServlet:

    /**
     * 直接将传入的对象序列化为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
     * @return
     */
    public String writeValueAsString(Object obj) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(obj);
    }

代码实现:前台代码:

header.html:

 

 

//查询分类数据
        $.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;
            }
            //拼接收藏排行榜的li
            lis+='<li><a href="favoriterank.html">收藏排行榜</a></li>';
            //将lis字符串设置到ul的html中
            $("#category").html(lis);
        });
        
    });

 

 缓存优化:

分析:

CategoryServiceImpl:

public class CategoryServiceImpl implements CategoryService {
    private CategoryDao categoryDao = new CategoryDaoImpl();
    @Override
    public List<Category> findAll() {
        //获取Jedis客户端
        Jedis jedis = JedisUtil.getJedis();
        //可使用sortedset排序查询
        Set<String> category = jedis.zrange("category", 0, -1);
        List<Category> cs = null;
        //判断查询的集合是否为空
        if (category==null || category.size()==0){
            //如果为空,需要从数据库查询,在将数据存入redis
            //从数据库查询
            cs = categoryDao.findAll();
            //System.out.println("数据库查询....");
            //将集合数据存储到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查询...");
            //如果不为空,将set数据存入list
            cs = new ArrayList<Category>();
            for (String name : category) {
                Category c = new Category();
                c.setCname(name);
                cs.add(c);
            }
        }
        return cs;
    }
}

 

 

 

 

搜索

复制

标签:category,findAll,List,黑马,案例,旅游网,cs,new,public
来源: https://www.cnblogs.com/pengtianyang/p/16623408.html