• java 用redisTemplate 的 Operations存取list集合


    一 、存取为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集合

  • 相关阅读:
    boost开发指南
    boost bind function用法说明
    boost库 bind/function的使用
    OpenCV——PS滤镜, 碎片特效
    pthread_join()
    基于用例点来度量软件规模并管理进度 之中的一个
    tomcat的JK和JK2
    rackup工具
    ORACLE EXP命令
    很好的理解遗传算法的样例
  • 原文地址:https://www.cnblogs.com/zhuyeshen/p/11428356.html
Copyright © 2020-2023  润新知