Spring Boot Redis

Spring Boot Redis

1、配置pom.xml,增加redis依赖。

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

2、application.properties 添加redis连接配置。

spring.redis.host=127.0.0.1
spring.redis.password=redis1234
spring.redis.port=6379

3、配置文件,redis.windows.conf、redis.windows-service.conf。
找到# requirepass foobared,修改为requirepass redis1234。

4、启动redis。redis-server.exe redis.windows.conf

5、测试。http://localhost:8080/redis/9001

package com.idodo.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class RedisController {

  @Autowired
  private StringRedisTemplate redisClient;

  @RequestMapping("/redis/{user_code}")
  public @ResponseBody String getUser(@PathVariable String user_code) {
    redisClient.opsForValue().set("user_code", user_code);
    String str = redisClient.opsForValue().get("user_code");
    
    System.out.println(str);
    
    return str;
  }
}

 

发表回复

您的电子邮箱地址不会被公开。