springboot 中 redis注解,在修改时,级联报空值错误。用json做序列化,不用jdk的序列化的配置bean,注解返回值

主要原因是,json做序列化的时候,级联报错。

解决办法,

用jedis,删除这个key,key需要自己拼接,

        zhouyiContentService.modifyZhouyiContent(zhouyiContent);
        ShardedJedis jedis = shardedJedisPool.getResource();
//        String jsonString = JSON.toJSONString(zhouyiContent);
        
        String key= "ZhouyiContent::"+zhouyiContent.getZhouyiAuthor().getId()+":"+zhouyiContent.getZhouyiYao().getId();
        System.out.println("key is: "+key);
        try {
            jedis.del(key);
        } finally {
            // TODO: handle finally clause
            jedis.close();
        }
        
        
        zhouyiContentService.getZhouyiContentByAuthorAndYaoId(zhouyiContent.getZhouyiAuthor().getId(),zhouyiContent.getZhouyiYao().getId() );

另外:

补充在序列化时,不用jdk的序列化,而用json的。

@Configuration
public class RedisConfig {

    @Bean
     public RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties) {
        CacheProperties.Redis redisProperties = cacheProperties.getRedis();
        RedisCacheConfiguration config = RedisCacheConfiguration
                .defaultCacheConfig();

        config = config.serializeValuesWith(RedisSerializationContext.SerializationPair
                .fromSerializer(new GenericJackson2JsonRedisSerializer()));

        if (redisProperties.getTimeToLive() != null) {
            config = config.entryTtl(redisProperties.getTimeToLive());
        }
        if (redisProperties.getKeyPrefix() != null) {
            config = config.prefixKeysWith(redisProperties.getKeyPrefix());
        }
        if (!redisProperties.isCacheNullValues()) {
            config = config.disableCachingNullValues();
        }
        if (!redisProperties.isUseKeyPrefix()) {
            config = config.disableKeyPrefix();
        }
        return config;
        }
}

在springboot中,用注解,使用返回值 #result,代表返回的对象。传入的对象,用对象名字即可,如#record.id

    @Autowired
    private UserMapper userMapper;
        
    @CacheEvict(cacheNames = "user" ,key = "#id")
    public int deleteByPrimaryKey(Integer id) {
        return this.userMapper.deleteByPrimaryKey(id);
    }

     @CachePut(cacheNames = "user",key="#result.id")
    public  User insert(User record) {
         this.userMapper.insert(record);
         return record;
    }

    @CachePut(cacheNames = "user",key="#result.id")
    public  User insertSelective(User record) {
      this.userMapper.insertSelective(record);    
      return record;
    }

    @Cacheable(cacheNames = "user", key="#id")
   public  User selectByPrimaryKey(Integer id) {
        return this.userMapper.selectByPrimaryKey(id);
    }

   @CachePut(cacheNames = "user",key="#result.id")
    public User updateByPrimaryKeySelective(User record) {
        this.userMapper.updateByPrimaryKeySelective(record);
        return record;
    }

    @CachePut(cacheNames = "user",key="#record.id")
    public User updateByPrimaryKey(User record) {
         this.userMapper.updateByPrimaryKey(record);
         return record;
    }
    
//    public List<User> queryAll(){
//        return this.userMapper.selectByPrimaryKey(id)
//    }
 //   @Autowired
  //  private Redis redis ;
    
    @Cacheable(cacheNames = "userList")
    public List<User> queryAll(){
    //    Jedis jedis= new 
        List<User> queryAll = this.userMapper.queryAll();
        return     queryAll;
    }
原文地址:https://www.cnblogs.com/sdgtxuyong/p/14711683.html