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

maven/springboot

pom文件写依赖

json依赖

<dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>fastjson</artifactId>
   <version>1.2.13</version>
</dependency>
<dependency>
   <groupId>org.codehaus.jackson</groupId>
   <artifactId>jackson-core-asl</artifactId>
   <version>1.9.13</version>
</dependency>
<dependency>
   <groupId>org.codehaus.jackson</groupId>
   <artifactId>jackson-mapper-asl</artifactId>
   <version>1.9.13</version>
</dependency>

redis依赖

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

controller层

从数据库查出放入缓存中

@RequestMapping("showAll")
    public String showAll(HttpServletRequest request){
         Jedis jedis = new Jedis("虚拟机端口号", 6379);
            //先从缓存中查询
            String str = jedis.get("selectShopping");
            System.out.println(str);
            //如果缓存中没有,则查询数据库
            List<Goods> 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<String> types(){
        List<String > list = new ArrayList<String >();
        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;
    }

页面

<script>
        $(function(){
           $.post("/goods/types",function (data){
               $.each(data,function (index,obj) {
                   $("select").append("<option value="+obj+">"+obj+"</option>")
               })
           });
        });
    </script>

<div  class="form-group">
        <label >类别</label>
        <select class="form-control" name="gtype">
            <option >请选择</option>

        </select>
    </div>

给下拉菜单添加ajax事件

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

(#s为select标签ID)



  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值