需要的依赖
application.properties的配置
spring.redis.host=127.0.0.1
spring.redis.database=0
spring.redis.port=6379
spring.redis.password=handhand
Controller
package org.akk.redis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
StringRedisTemplate stringRedisTemplate;
@GetMapping("/set")
public void set(){
ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue();
opsForValue.set("name","akk");
}
@GetMapping("/get")
public void get(){
ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue();
System.out.println(opsForValue.get("name"));
}
}
运行后显示密码
登录
http://localhost:8080/set 自动跳转登陆界面
默认用户名user,使用以上密码
运行结果
使用http://localhost:8080/get
将会打印出信息