java 用redisTemplate 的 Operations存取list集合
Posted zhuyeshen
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 用redisTemplate 的 Operations存取list集合相关的知识,希望对你有一定的参考价值。
一 、存取为list类型
-
@RestController
-
@RequestMapping("/test")
-
@Slf4j
-
public class TestController
-
-
@Autowired
-
private RedisTemplate redisTemplate;
-
-
@ApiOperation("redis-savelist")
-
@PostMapping("/redis/save/list")
-
public void redisSaveList()
-
List<Person> list = getPersonList();
-
//清空
-
while (redisTemplate.opsForList().size("oowwoo") > 0)
-
redisTemplate.opsForList().leftPop("oowwoo");
-
-
//存储
-
redisTemplate.opsForList().rightPushAll("oowwoo", list);
-
-
//取出
-
List<Person> oowwoo = redisTemplate.opsForList().range("oowwoo", 0, -1);
-
log.info(">>>>>>>>>>>>>>>list = ", oowwoo.toString());
-
Iterator<Person> it = oowwoo.iterator();
-
while(it.hasNext())
-
Person p = it.next();
-
log.info("person = ", p.toString());
-
-
-
-
private List<Person> getPersonList()
-
Person p1 = new Person();
-
p1.setId(1L);
-
p1.setName("张一");
-
p1.setAge(11);
-
-
Person p2 = new Person();
-
p2.setId(2L);
-
p2.setName("张二");
-
p2.setAge(22);
-
-
Person p3 = new Person();
-
p3.setId(3L);
-
p3.setName("张三");
-
p3.setAge(33);
-
-
List<Person> list = new ArrayList<>();
-
list.add(p1);
-
list.add(p2);
-
list.add(p3);
-
return list;
-
-
二 、将list转为json对象存取
-
import com.alibaba.fastjson.JSON;
-
import com.alibaba.fastjson.JSONObject;
-
-
-
-
private StringRedisTemplate stringRedisTemplate;
-
-
//存
-
List<Long> businessIdList = eeFreecarriageShopService.selectBusinessIdInPromotion();
-
stringRedisTemplate.opsForValue().set(RedisConstants.FREECARRIAGE_BUSINESSIDLIST, JSON.toJSON(businessIdList).toString());
-
-
//取
-
String businessJsonArray = stringRedisTemplate.opsForValue().get(RedisConstants.FREECARRIAGE_BUSINESSIDLIST);
-
List<Long> businessIdList = JSONObject.parseArray(businessJsonArray, Long.class);
java 用redisTemplate 的 Operations存取list集合
以上是关于java 用redisTemplate 的 Operations存取list集合的主要内容,如果未能解决你的问题,请参考以下文章
java通过redisTemplate封装redis3.2.9工具类