springboot支持redis 11

redis是最常见的高速缓存,本文将讲述springboot如何整合redis。

1、环境约束

  • win10 64位操作系统
  • idea2018.1.5
  • maven-3.0.5
  • jdk-8u162-windows-x64
  • redis3.2

2、前提约束

3、操作

  • 在pom.xml的增加以下依赖:
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
  • 修改application.properties为application.yml,在application.yml中添加以下内容:
spring:
  redis:
    host: localhost
    port: 6379
  • 在src/main/java中新增测试类net.wanho.springboot.redis.redisdemo.RedisController.java
package net.wanho.springboot.redis.redisdemo;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
public class RedisController {
    @Resource
    StringRedisTemplate stringRedisTemplate;

    @RequestMapping("/writeredis")
    @ResponseBody
    public String write(@RequestParam("key") String key, @RequestParam("value")  String value)
    {
        //设置值
        stringRedisTemplate.opsForValue().set(key,value);
        return "ok";
    }

    @RequestMapping("/readredis")
    @ResponseBody
    public String read(@RequestParam("key") String key)
    {
        //获取值
        return stringRedisTemplate.opsForValue().get(key);
    }
}

4、测试

  • 运行主启动类
  • 在浏览器中输入以下url在redis中设置值,回车
http://localhost:8080/writeredis?key=key&value=xiaoli
  • 在浏览器中再输入以下url从redis中获取值,回车
http://localhost:8080/readredis?key=key

5、结果分析

通过以上操作,我们便能看到浏览器打印出了“xiaoli”这个字段;倘若这时候去redis中查询,同样能看到key值为“key”,value值为“xiaoli”的键值对已经被写入到了redis。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值