Day11JavaWeb【Redis】综合案例使用redis缓存商品分类***

思路分析

在这里插入图片描述
》》1:查询redis,没有json数据,就调用CategoryDao,去获取集合List<Category>
先返回给CategorySrvice,再转成json存进redis
》》2:第二次起,直接获取redis中的json,将json转成List<Category>

(1) 编写CategoryServiceTest

test\java\com\wzx\pack01\TestCategoryService.java

//第一步:测试开发CategoryService
public class TestCategoryService {
    
    
    @Test
    public void test01() throws Exception {
    
    

        //0: 创建业务类对象
        CategoryService categoryService = new CategoryService();

        //1: 查询分类集合
        List<Category> list = categoryService.queryAll();

        for(Category category:list){
    
    
            System.out.println(category);
        }


    }
}

(2)编写javaBean与CategoryDao

src\com\wzx\bean\Category.java

//第二步骤 :
public class Category {
    
    
    private int cid;
    private String cname;

src\com\wzx\dao\CategoryDao.java

public class CategoryDao {
    
    
    public List<Category> findAll() {
    
    
        //1:创建集合
        List<Category> list = new ArrayList<>();
        //使用循环模拟数据,以后使用mybatis来查数据
        for (int i = 0; i < 10; i++) {
    
    
            list.add(new Category(i,"分类名"+i));
        }
        return list;
    }
}

(3)编写CategoryService

src\com\wzx\service\CategoryService.java

//第三步骤:实现queryAll方法
//3.1 复制jedis jar过来
//3.2 绑定
//3.3 复制properties文件,与工具类
public class CategoryService {
    
    
    CategoryDao dao = new CategoryDao();
    ObjectMapper objectMapper = new ObjectMapper();
    public List<Category> queryAll() throws IOException {
    
    
        //queryAll的逻辑
        //1:先访问redis,有数据就返回 JedisUtils  Map<Srting,String>
        //3.4获取连接
        Jedis redis = JedisUtils.getRedis();
        //缓存
        String json = redis.get("list");
        if(json == null){
    
    
            System.out.println("第一次查询 redis没有数据,查的数据库的数据,慢");
            //2: 没有数据就查询数据,进行缓存,方便下次使用 CategoryDao
            List<Category> list = dao.findAll();
            //缓存  将集合转换成json,缓存到redis
            //3.5 添加json jar到lib目录并且绑定
            //3.6 创建ObjectMapper对象,转list成json
            String jsonValue =  objectMapper.writeValueAsString(list);

            System.out.println(jsonValue);

            redis.set("list",jsonValue);
            return list;
        }else{
    
    //json ! =null
            System.out.println("第二次 redis有数据,直接返回,快");
            List<Category> list = objectMapper.readValue(json,new TypeReference<List<Category>>(){
    
    });//将json转成对象   参1 json数据  参2
            return list;
        }

    }
}

(4)将json数据转成对象

test\java\com\wzx\pack01\TestObjectMapper.java

public class TestObjectMapper {
    
    
    @Test
    public void test01() throws IOException {
    
    
        String json="[{\"cid\":0,\"cname\":\"分类名0\"},{\"cid\":1,\"cname\":\"分类名1\"},{\"cid\":2,\"cname\":\"分类名2\"},{\"cid\":3,\"cname\":\"分类名3\"},{\"cid\":4,\"cname\":\"分类名4\"},{\"cid\":5,\"cname\":\"分类名5\"},{\"cid\":6,\"cname\":\"分类名6\"},{\"cid\":7,\"cname\":\"分类名7\"},{\"cid\":8,\"cname\":\"分类名8\"},{\"cid\":9,\"cname\":\"分类名9\"}]";

        //
        ObjectMapper objectMapper = new ObjectMapper();

        List<Category> list = objectMapper.readValue(json,new TypeReference<List<Category>>(){
    
    });//将json转成对象   参1 json数据  参2 new TypeReference<>(){}
        System.out.println(list);
    }
}

  • 参2 new TypeReference<返回值类型>(){}

(5)过滤器解决全站中文编码乱码

src\com\wzx\filter\CharSetFilter.java

@WebFilter("/*")
public class CharSetFilter implements Filter {
    
    
    public void destroy() {
    
    
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
    
    
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        chain.doFilter(req, resp);
    }

    public void init(FilterConfig config) throws ServletException {
    
    

    }

}

(6)解决Servlet显示分类列表

src\com\wzx\servlet\CategoryListServlet.java

@WebServlet("/list")
public class CategoryListServlet extends HttpServlet {
    
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
        //0: 创建业务类对象
        CategoryService categoryService = new CategoryService();

        //1: 查询分类集合
        List<Category> list = categoryService.queryAll();

        //2: 请求转发
        request.setAttribute("list",list);
        request.getRequestDispatcher("list.jsp").forward(request,response);

    }
}

web\list.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 <html>
<head>
    <title>Title</title>
</head>
<body>
        <c:forEach items="${list}" var="item">
           <div > ${
    
    item.cname} </div>
        </c:forEach>
</body>
</html>

(7)其他

Lib

在这里插入图片描述

jedis.properties

maxTotal=30
maxIdle=10
url=localhost
port=6379

JedisUtils

src\com\wzx\util\JedisUtils.java

public class JedisUtils {
    
    
    private static JedisPool pool = null;
    //1:创建一个连接池
    static{
    
    
        //1.1 解析properties文件
        ResourceBundle bundle = ResourceBundle.getBundle("jedis");
        //获取参数
        String maxTotal = bundle.getString("maxTotal");
        String maxIdle = bundle.getString("maxIdle");
        String url = bundle.getString("url");
        String port = bundle.getString("port");

        //1.2创建连接池
        //1:创建连接池的配置对象
        JedisPoolConfig config = new JedisPoolConfig();
        //1.1 设置最大连接数
        config.setMaxTotal(Integer.parseInt(maxTotal));
        //1.2 设置空闲连接数
        config.setMaxIdle(Integer.parseInt(maxIdle));

        //2:创建连接池
         pool = new JedisPool(config, url, Integer.parseInt(port));
    }

    //2:对外提供一个获取连接的方法
    public static Jedis getJedis(){
    
    
        return  pool.getResource();
    }

    //3:提供释放资源的方法
    public static void close(Jedis jedis){
    
    
        if(jedis != null) {
    
    
            jedis.close();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/u013621398/article/details/108711842