首页>代码>Spring Boot学习(八)之使用NoSQL数据库(一):Redis 博客源码>/springbootstudy-demo8-redis/src/main/java/com/xiaojingg/web/HelloController.java
package com.xiaojingg.web;
import com.xiaojingg.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private RedisTemplate redisTemplate;
@RequestMapping("/hello")
public String index() {
// 保存字符串
stringRedisTemplate.opsForValue().set("aaa", "111");
String string = stringRedisTemplate.opsForValue().get("aaa");
System.out.println(string);
// 保存对象
User user = new User("超人", 20);
redisTemplate.opsForValue().set(user.getUsername(), user);
user = new User("蝙蝠侠", 30);
redisTemplate.opsForValue().set(user.getUsername(), user);
user = new User("蜘蛛侠", 40);
redisTemplate.opsForValue().set(user.getUsername(), user);
User user1 = (User)redisTemplate.opsForValue().get("超人");
System.out.println(user1.getAge().longValue());
System.out.println(((User)redisTemplate.opsForValue().get("蝙蝠侠")).getAge().longValue());
System.out.println(((User)redisTemplate.opsForValue().get("蜘蛛侠")).getAge().longValue());
return "Hello World";
}
}
最近下载更多
最近浏览更多
dane168 LV10
8月5日
LONGGE123 LV1
1月10日
微信网友_7134912998903808 LV15
2024年8月29日
唐唐丶 LV34
2023年11月14日
漫步的海星 LV4
2023年9月26日
微信网友_6482545254191104
2023年5月19日
暂无贡献等级
小赤0120 LV4
2022年12月3日
youwuzuichen LV11
2022年6月6日
小橘子1640 LV3
2022年5月31日
dingdehong LV10
2021年9月27日

