码迷,mamicode.com
首页 > 编程语言 > 详细

java 用redisTemplate 的 Operations存取list集合

时间:2019-08-29 11:06:29      阅读:1127      评论:0      收藏:0      [点我收藏+]

标签:sele   hal   ssid   obj   service   range   nbsp   ops   res   

一 、存取为list类型

  1. @RestController
  2. @RequestMapping("/test")
  3. @Slf4j
  4. public class TestController {
  5.  
  6. @Autowired
  7. private RedisTemplate redisTemplate;
  8.  
  9. @ApiOperation("redis-savelist")
  10. @PostMapping("/redis/save/list")
  11. public void redisSaveList() {
  12. List<Person> list = getPersonList();
  13. //清空
  14. while (redisTemplate.opsForList().size("oowwoo") > 0){
  15. redisTemplate.opsForList().leftPop("oowwoo");
  16. }
  17. //存储
  18. redisTemplate.opsForList().rightPushAll("oowwoo", list);
  19.  
  20. //取出
  21. List<Person> oowwoo = redisTemplate.opsForList().range("oowwoo", 0, -1);
  22. log.info(">>>>>>>>>>>>>>>list = {}", oowwoo.toString());
  23. Iterator<Person> it = oowwoo.iterator();
  24. while(it.hasNext()){
  25. Person p = it.next();
  26. log.info("person = {}", p.toString());
  27. }
  28. }
  29.  
  30. private List<Person> getPersonList() {
  31. Person p1 = new Person();
  32. p1.setId(1L);
  33. p1.setName("张一");
  34. p1.setAge(11);
  35.  
  36. Person p2 = new Person();
  37. p2.setId(2L);
  38. p2.setName("张二");
  39. p2.setAge(22);
  40.  
  41. Person p3 = new Person();
  42. p3.setId(3L);
  43. p3.setName("张三");
  44. p3.setAge(33);
  45.  
  46. List<Person> list = new ArrayList<>();
  47. list.add(p1);
  48. list.add(p2);
  49. list.add(p3);
  50. return list;
  51. }
  52. }

 

二 、将list转为json对象存取

  1. import com.alibaba.fastjson.JSON;
  2. import com.alibaba.fastjson.JSONObject;
  3.  
  4.  
  5. @Autowired
  6. private StringRedisTemplate stringRedisTemplate;
  7.  
  8. //存
  9. List<Long> businessIdList = eeFreecarriageShopService.selectBusinessIdInPromotion();
  10. stringRedisTemplate.opsForValue().set(RedisConstants.FREECARRIAGE_BUSINESSIDLIST, JSON.toJSON(businessIdList).toString());
  11.  
  12. //取
  13. String businessJsonArray = stringRedisTemplate.opsForValue().get(RedisConstants.FREECARRIAGE_BUSINESSIDLIST);
  14. List<Long> businessIdList = JSONObject.parseArray(businessJsonArray, Long.class);

 
java 用redisTemplate 的 Operations存取list集合

java 用redisTemplate 的 Operations存取list集合

标签:sele   hal   ssid   obj   service   range   nbsp   ops   res   

原文地址:https://www.cnblogs.com/zhuyeshen/p/11428356.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!