旅游网CategoryRedisDaoImpl

本文详细介绍了如何在Java中使用Redis存储和查询Category对象。通过Jedis客户端,实现了Category对象的序列化和反序列化过程,包括将对象转换为JSON字符串存储到Redis,并从Redis读取JSON字符串再转换回Category对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class CategoryRedisDaoImpl implements CategoryDao {
//获得jedis
private Jedis jedis = JedisUtil.getJedis();

@Override
public List<Category> queryAll() {
    //获得jedis查询到的字符串集合
    List<String> categories = jedis.lrange("heima.travel.categories", 0, -1);
    //创建一个Category集合
    //通过ObjectMapper对象将字符串转变为Category对象
    List<Category> result = new ArrayList<>();
    //遍历字符串集合
    for (String category : categories) {
        ObjectMapper mapper = new ObjectMapper();
        Category c = null;
        try {
            c = mapper.readValue(category, Category.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        result.add(c);
    }
    return result;
    //将转换后的对象添加到Category集合中并返回
}

@Override
public int append(List<Category> categories) {

    for (Category category : categories) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            String s = mapper.writeValueAsString(category);
            jedis.rpush("heima.travel.categories", s);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }
    return categories.size();
}

@Override
public Category queryById(int cid) {
    return null;
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值