慕的地8271018
redis序列化数据有多重方式:JacksonJsonRedisSerializerJdkSerializationRedisSerializerOxmSerializer这里前两种测试1.StringSerializer.javapublic enum StringSerializer implements RedisSerializer<String> { INSTANCE; public byte[] serialize(String s) throws SerializationException { return (null != s ? s.getBytes() : new byte[0]); } public String deserialize(byte[] bytes) throws SerializationException { if (bytes.length > 0) { return new String(bytes); } else { return null; } } }12345678910111213142.测试类public RedisConnectionFactory redisConnectionFactory() { JedisConnectionFactory cf = new JedisConnectionFactory(); cf.setHostName("172.16.28.214"); cf.setPort(6379); //cf.setPassword("123456"); cf.afterPropertiesSet(); return cf; }@Test public void test() { try { RedisConnectionFactory connectionFactory = redisConnectionFactory(); RedisTemplate<String, Serializable> redis = new RedisTemplate<String, Serializable>(); redis.setConnectionFactory(connectionFactory); redis.setKeySerializer(StringSerializer.INSTANCE); // redis.setValueSerializer(new JdkSerializationRedisSerializer()); redis.setValueSerializer(new JacksonJsonRedisSerializer<TestUser>(TestUser.class)); redis.afterPropertiesSet(); ValueOperations<String, Serializable> ops = redis.opsForValue(); TestUser user1 = new TestUser(); user1.setName("lisi"); user1.setId(20L); // ops.set("lisi", user1); TestUser user = (TestUser) ops.get("lisi"); System.out.println(user.getName()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }