springboot2.X整合redis

趁着今天休息就学习了一下redis,刚开始配置的时候遇到一些坑,现在写下来记一下,首先springboot使用的版本是2.X(我使用的是2.1.6)我的是在本地配置的,环境是windows环境,首先要先安装redis

  1. 首先在pom.xml中添加相应依赖

 

<!-- 2.* 版本 redis依赖改名了,直接使用下面的依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

2. 配置application.properties文件

# Redis数据库分片索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host= 192.168.196.1
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=

 3.新建类Rediscontroller

package com.example.ribbonclient.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RedisController {

    @Autowired
    RedisTemplate redisTemplate;

    @RequestMapping("/set")
    public String setHello(String key,String value){
        redisTemplate.opsForValue().set(key,value);
        return "redis success";
    }
    @RequestMapping("/get")
    public String getHello(String key){
        String value = (String)redisTemplate.opsForValue().get(key);
        return "redis value=" + value;
    }
}

4.访问http://localhost:1011/set?key=name&value=zwr

5.得到刚才设置的值http://localhost:1011/get?key=name

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值