redis存储list

把List转换成JSON,存储到Redis,取出来的时候,再把JSON转换成List。

User user = new User();
    	user.setId(123);
    	user.setName("fighter");
    	 User user1 = new User();
     	user1.setId(1232);
     	user1.setName("ssss");
    	List list = new ArrayList();
    	list.add(user);
    	list.add(user1);
    	String json=JSONObject.toJSONString(list);
    	System.out.println(json);
    	redisService.set("user",json);
    	
    	String username = redisService.get("user");
        List newList=JSONObject.parseArray(username, User.class);
        System.out.println(newList.size());

 其中用到阿里的fastjson,maven的pom:

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.4</version>
</dependency>

相关推荐