SpringBoot2整合Redis

SpringBoot2整合Redis

每个公司每个项目可能有不同的引入方式,但是最基本的都大差不差,所以这里只展示最基本的。

引入redis的maven配置

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

application.properties中配置redis信息

#redis服务地址
spring.redis.host=192.168.200.129
#redis端口
spring.redis.port=6379
#redis密码,没有可以为空或者屏蔽
#spring.redis.password=
##redis超时时间
spring.redis.timeout=60000
#redis默认数据库
spring.redis.database=0

使用RedisTemplate工具类操作redis

springboot中使用RedisTemplate来操作redis,需要在我们的bean中注入这个对象,代码如下:

@Autowired
private RedisTemplate<String, String> redisTemplate;

// 用下面5个对象来操作对应的类型
this.redisTemplate.opsForValue(); 	//提供了操作string类型的所有方法
this.redisTemplate.opsForList(); 	// 提供了操作list类型的所有方法
this.redisTemplate.opsForSet(); 	//提供了操作set的所有方法
this.redisTemplate.opsForHash(); 	//提供了操作hash表的所有方法
this.redisTemplate.opsForZSet(); 	//提供了操作zset的所有方法

RedisTemplate示例代码

具体示例如下(基本数据控制示例):

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.RestController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/redis")
public class RedisController {
    
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    
    @RequestMapping("/stringTest")
    public String stringTest() {
        this.redisTemplate.delete("name");
        this.redisTemplate.opsForValue().set("name", "zhangsan");
        String name = this.redisTemplate.opsForValue().get("name");
        return name;
    }
    
    @RequestMapping("/listTest")
    public List<String> listTest() {
        this.redisTemplate.delete("names");
        this.redisTemplate.opsForList().rightPushAll("names", "zhangsan", "lisi", "wangwu", "zhaoliu");
        List<String> courses = this.redisTemplate.opsForList().range("names", 0, -1);
        return courses;
    }
    
    @RequestMapping("setTest")
    public Set<String> setTest() {
        this.redisTemplate.delete("chengji");
        this.redisTemplate.opsForSet().add("chengji", "java", "spring", "springboot");
        Set<String> courses = this.redisTemplate.opsForSet().members("chengji");
        return courses;
    }
    
    @RequestMapping("hashTest")
    public Map<Object, Object> hashTest() {
        this.redisTemplate.delete("user");
        Map<String, String> map = new HashMap<>();
        map.put("name", "zhangsan");
        map.put("age", "30");
        this.redisTemplate.opsForHash().putAll("user", map);
        Map<Object, Object> userMap = this.redisTemplate.opsForHash().entries("user");
        return userMap;
    }
    
    @RequestMapping("zsetTest")
    public Set<String> zsetTest() {
        this.redisTemplate.delete("chengji");
        this.redisTemplate.opsForZSet().add("chengji", "java", 100d);
        this.redisTemplate.opsForZSet().add("chengji", "c", 95d);
        this.redisTemplate.opsForZSet().add("chengji", "php", 70);
        Set<String> languages = this.redisTemplate.opsForZSet().range("chengji", 0, -1);
        return languages;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WorkLee

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值