l'intégration de springboot Redis atteindre sérialisation

 < Dépendance > 
            < groupId > org.springframework.boot </ groupId > 
            < artifactId > ressort-boot-démarreurs-données Redis </ artifactId > 
        </ dépendance >

classe de configuration :( sérialisation de mettre en œuvre Redis)

emballer com.example.mypay.config; 

importation com.alibaba.fastjson.JSON;
importation com.alibaba.fastjson.serializer.SerializerFeature;
importation org.springframework.data.redis.serializer.RedisSerializer;
importation org.springframework.data.redis.serializer.SerializationException; 

importation java.nio.charset.Charset; 

publique  classe FastJsonRedisSerializer <T> outils RedisSerializer <T> { 

    publique  statique  finale charset = DEFAULT_CHARSET Charset.forName ( "UTF-8" ); 

    privé de classe <T> clazz; 

    publique FastJsonRedisSerializer (classe <T>clazz) {
         super ();
        ce .clazz = clazz; 
    } 

    @Override 
    publique  octet [] sérialiser (T t) jette SerializationException {
         si (t == NULL ) {
             retour  nouvel  octet [0 ]; 
        } 
        Retour JSON.toJSONString (t, SerializerFeature.WriteClassName) .getBytes (default_charset); 
    } 

    @Override 
    publique T deserialize ( octet [] octets) lance SerializationException {
         si (octets ==null || bytes.length <= 0 ) {
             retour  null ; 
        } 
        String str = nouvelle chaîne (octets, default_charset);
        retour (T) JSON.parseObject (str, clazz); 
    } 

}

Classe de configuration:

emballer com.example.mypay.config; 


importation com.alibaba.fastjson.JSON;
importation com.alibaba.fastjson.parser.ParserConfig;
importation com.alibaba.fastjson.support.config.FastJsonConfig; 

importation org.springframework.cache.annotation.CachingConfigurerSupport;
importation org.springframework.context.annotation.Bean;
importation org.springframework.context.annotation.Configuration;
importation org.springframework.data.redis.connection.RedisConnectionFactory;
importation org.springframework.data.redis.core.RedisTemplate;
importation org.springframework.data.redis.serializer.StringRedisSerializer; 

@Configuration
publique  classe RedisConfig { 
    RedisConfig () { 
        // 打开autotype功能,需要强转的类一次添加其后
        ParserConfig.getGlobalInstance () 
                .addAccept ( "com.example.mypay.entity" ); 
    } 

    @Bean 
    publique RedisTemplate <Object, Object> redisTemplate (RedisConnectionFactory redisConnectionFactory) { 
        RedisTemplate <Object, Object> template = nouveau RedisTemplate <> ();
        // matrice .setConnectionFactory (redisConnectionFactory); 
        FastJsonRedisSerializer <Object> fastJsonRedisSerializer = nouveau FastJsonRedisSerializer <> (Object.classe ); 
        template.setValueSerializer (fastJsonRedisSerializer); 
        template.setKeySerializer ( nouveau StringRedisSerializer ()); 
        template.setHashValueSerializer (fastJsonRedisSerializer); 
        template.setHashKeySerializer ( nouveau StringRedisSerializer ()); 
        template.setConnectionFactory (redisConnectionFactory); 
            retourner modèle; 

    } 

  
}

utilisation

 

  Les objets peuvent être directement prises

Redis modèles de données

 

Je suppose que tu aimes

Origine www.cnblogs.com/dkws/p/12455645.html
conseillé
Classement