@Autowired private RedisTemplate<String,String> redisTemplate; //如果不指定类型,必须要实例化
存储的结构
redisTemplate.opsForValue();//操作字符串 redisTemplate.opsForHash();//操作hash redisTemplate.opsForList();//操作list redisTemplate.opsForSet();//操作set redisTemplate.opsForZSet();//操作有序set
保存数据
redisTemplate.opsForValue().set(key, value);
删除数据
redisTemplate.delete(key);
序列化对象
1.创建对象类
import java.io.Serializable; /** * user * @author hemingzhu * @version 2018/5/31 * */ public class User implements Serializable { private static final long serialVersionUID = -1L; private String username; private Integer age; public User(String username, Integer age) { this.username = username; this.age = age; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; }
2.序列化配置
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.*; import com.example.redis.entity.User; /** * user * @author hemingzhu * @version 2018/5/31 * */ @Configuration public class RedisConfig { @Bean JedisConnectionFactory jedisConnectionFactory() { return new JedisConnectionFactory(); } @Bean public RedisTemplate<String, User> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, User> template = new RedisTemplate<String, User>(); template.setConnectionFactory(jedisConnectionFactory()); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new RedisObjectSerializer()); return template; } }
对对象的序列化
import org.springframework.core.convert.converter.Converter; import org.springframework.core.serializer.support.DeserializingConverter; import org.springframework.core.serializer.support.SerializingConverter; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.SerializationException; /** * user * @author hemingzhu * @version 2018/5/31 * */ public class RedisObjectSerializer implements RedisSerializer<Object> { private Converter<Object, byte[]> serializer = new SerializingConverter(); private Converter<byte[], Object> deserializer = new DeserializingConverter(); static final byte[] EMPTY_ARRAY = new byte[0]; public Object deserialize(byte[] bytes) { if (isEmpty(bytes)) { return null; } try { return deserializer.convert(bytes); } catch (Exception ex) { throw new SerializationException("Cannot deserialize", ex); } } public byte[] serialize(Object object) { if (object == null) { return EMPTY_ARRAY; } try { return serializer.convert(object); } catch (Exception ex) { return EMPTY_ARRAY; } } private boolean isEmpty(byte[] data) { return (data == null || data.length == 0); } }
完成上面连个配置
import java.io.Serializable; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Service; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.example.redis.entity.User; import com.example.redis.service.RedisService; /** * user * @author hemingzhu * @version 2018/5/31 * */ @Service public class RedisServiceImpl implements RedisService{ @Autowired private StringRedisTemplate stringRedisTemplate; @Autowired private RedisTemplate<String, String> redisTemplate; @Autowired private RedisTemplate<String, User> redisTemplates; // @Override // public void saveString() { // Map<String,String> map=new HashMap<String,String>(); // map.put("key1","value1"); // map.put("key2","value2"); // map.put("key3","value3"); // map.put("key4","value4"); // map.put("key5","value5"); // // stringRedisTemplate.opsForHash().putAll("map", map); // stringRedisTemplate.opsForValue().set("a", "b"); // stringRedisTemplate.opsForSet().add("w", "c"); // stringRedisTemplate.opsForSet().add("w", "er"); // //Assert.assertEquals("b", stringRedisTemplate.opsForValue().get("a")); // redisTemplate.opsForValue().set("aa", "bb"); // redisTemplate.opsForList().leftPush("cc", "cc"); // redisTemplate.opsForList().leftPush("cc", "bb"); // redisTemplate.opsForList().leftPush("cc", "dd"); // redisTemplate.opsForList().leftPush("dd", "dd"); // // } @Override public void saveUser() { User user = new User("蜘蛛侠", 20); redisTemplates.opsForValue().set(user.getUsername(), user); user = new User("蝙蝠侠", 30); redisTemplates.opsForValue().set(user.getUsername(), user); user = new User("超人", 40); redisTemplates.opsForValue().set(user.getUsername(), user); Object a = redisTemplates.opsForValue().get("超人"); System.out.println(a.toString()); System.out.println(((User)a).getAge()); System.out.println(redisTemplate.opsForValue().get("what")); } }
Controller
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import com.example.redis.service.RedisService; /** * user * @author hemingzhu * @version 2018/5/31 * */ @RestController public class RedisController { @Autowired private RedisService redis; // @GetMapping("/redis/string/save") // public void saveString() { // redis.saveString(); // } @GetMapping("/redis/user/save") public void saveUser() { redis.saveUser(); } }