Springboot整合Redis

Springboot整合Redis

redis下载与使用

Springboot整合Redis_第1张图片
Springboot整合Redis_第2张图片

SpringBoot整合Redis

Springboot整合Redis_第3张图片
Springboot整合Redis_第4张图片
这里注意密码,看你的配置文件中如果配置了密码,要加上密码的。
比如我的:

spring:
  data:
    redis:
      host: localhost
      port: 6379
      password: 123456

Springboot整合Redis_第5张图片
Springboot整合Redis_第6张图片

@SpringBootTest
class Springboot16RedisApplicationTests {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    void set() {
        ValueOperations ops = redisTemplate.opsForValue();
        ops.set("age",41);
    }

    @Test
    void get() {
        ValueOperations ops = redisTemplate.opsForValue();
        Object age = ops.get("age");
        System.out.println(age);
    }


    @Test
    void hset() {
        HashOperations ops = redisTemplate.opsForHash();
        ops.put("info","a","aa");
    }

    @Test
    void hget() {
        HashOperations ops = redisTemplate.opsForHash();
        Object o = ops.get("info", "a");
        System.out.println(o);
    }
}

Springboot整合Redis_第7张图片
Springboot整合Redis_第8张图片
在这里插入图片描述

@SpringBootTest
public class SpringRedisTemplateTest {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Test
    void get(){
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        String name = ops.get("name");
        System.out.println(name);
    }
}

springboot操作redis客户端实现技术切换(jedis)

  1. 导入jedis坐标
<dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>
  1. 配置profile
spring:
  data:
    redis:
      host: localhost
      port: 6379
      password: 123456
      client-type: jedis

Springboot整合Redis_第9张图片
Springboot整合Redis_第10张图片

你可能感兴趣的:(spring,boot,redis,后端)