将查询的数据写到redis缓存中

maven/springboot

pom文件写依赖

json依赖

<dependency>
   <groupId>com.alibabagroupId>
   <artifactId>fastjsonartifactId>
   <version>1.2.13version>
dependency>
<dependency>
   <groupId>org.codehaus.jacksongroupId>
   <artifactId>jackson-core-aslartifactId>
   <version>1.9.13version>
dependency>
<dependency>
   <groupId>org.codehaus.jacksongroupId>
   <artifactId>jackson-mapper-aslartifactId>
   <version>1.9.13version>
dependency>

redis依赖

<dependency>
   <groupId>org.springframework.bootgroupId>
   <artifactId>spring-boot-starter-data-redisartifactId>
dependency>

controller层

从数据库查出放入缓存中

@RequestMapping("showAll")
    public String showAll(HttpServletRequest request){
         Jedis jedis = new Jedis("虚拟机端口号", 6379);
            //先从缓存中查询
            String str = jedis.get("selectShopping");
            System.out.println(str);
            //如果缓存中没有,则查询数据库
            List selectAll = null;
            if(str==null){
                selectAll = service.SelectAll();
                for (Goods goods : selectAll) {
                    System.out.println(goods);
                }
                String jsonString = JSON.toJSONString(selectAll);
                jedis.set("selectShopping", jsonString);
            }

else {
    people = JSON.parseArray(str,Person.class);
}

            request.setAttribute("all", selectAll);
            return "showAll";
    }

先将数据放入缓存中,然后从缓存中查出来,在页面以下拉菜单的形式展示出来

@Resource
    private Jedis jedis = new Jedis("192.168.134.20",6379);

   @RequestMapping("toadd")
    public String toadd(){
        jedis.set("type","电脑");
        jedis.set("type1","手机");
        jedis.set("type2","水果");

        return "add";
    }
    @RequestMapping("types")
    @ResponseBody
    public List types(){
        List list = new ArrayList();
        String type = jedis.get("type");
        String type1 =jedis.get("type1");
        String type2 =jedis.get("type2");
        list.add(new String(type));
        list.add(new String(type1));
        list.add(new String(type2));
        return list;
    }

页面



       
       
   


给下拉菜单添加ajax事件

$("#s").change(function(){
                 var s = $(this).val();
                 window.location.href="${pageContext.request.contextPath }/stu/show?cid="+s+"";
             });

(#s为select标签ID)



你可能感兴趣的:(将查询的数据写到redis缓存中)