pom.xml依赖
org.springframework.boot spring-boot-starter-data-redis
全局属性application.properties
server.port=8080 #热部署 spring.devtools.restart.enabled=true spring.devtools.restart.additional-paths=src/main/java #redis spring.redis.host=127.0.0.1 #ip spring.redis.port=6379 #端口 spring.redis.database=1 #使用的数据库 spring.redis.password=整合 方案一(手动转换类型)
RedisController
这里用的参数是实体类对象,根据开发者需求自行选择传参类型
@RestController @RequestMapping("/redis") public class RedisController { //redis模板对象 @Autowired StringRedisTemplate template; ObjectMapper om=new ObjectMapper(); //存数据 //对象--->json @RequestMapping("/put") public String put() throws JsonProcessingException { // template.opsForValue().set("spring","整合redis"); User user =new User(88,"肖治磊",new Date()); String json=om.writevalueAsString(user);//将obj转json template.opsForValue().set("testobj",json); return "put ok"; } //取数据 //json--->对象 @RequestMapping("/get") public Object get() throws JsonProcessingException { String json=template.opsForValue().get("testobj"); User obj=om.readValue(json,User.class); //json转obj System.out.println("val="+obj); return obj; } }方案二(自动转换类型)
首先创建个redis配置类
RedisConfig
@Configuration @EnableCaching //开启注解 public class RedisConfig extends CachingConfigurerSupport { //返回redisTemplate 的对象 ---->spring容器 @Bean public RedisTemplateredisTemplate(RedisConnectionFactory factory){ RedisTemplate template=new RedisTemplate(); //配置连接工厂 template.setConnectionFactory(factory); //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式) Jackson2JsonRedisSerializer jackson=new Jackson2JsonRedisSerializer(Object.class);//序列号 ObjectMapper om=new ObjectMapper(); // (可见性)指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); // 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常 om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson.setObjectMapper(om); // 值采用json序列化 template.setValueSerializer(jackson); //使用StringRedisSerializer来序列化和反序列化redis的key值 template.setKeySerializer(new StringRedisSerializer()); // 设置hash key 和value序列化模式 template.setHashKeySerializer(new StringRedisSerializer()); template.setHashValueSerializer(jackson); template.afterPropertiesSet(); return template; } }
最后进行调用测试
RedisController2
@RestController @RequestMapping("/redis2") public class RedisController2 { //redis模板对象 @Autowired RedisTemplate redisTemplate; ObjectMapper om=new ObjectMapper(); //String存对象 @RequestMapping("/put") public String put(){ User user =new User(66,"男明星",new Date()); redisTemplate.opsForValue().set("user",user); //定时(1分钟消失) redisTemplate.expire("user",60, TimeUnit.SECONDS); return "put ok"; } //String取数据 @RequestMapping("/get") public Object get(){ User user=(User)redisTemplate.opsForValue().get("user"); System.out.println("String---------------------->"); System.out.println("val="+user); return user; } //Hash存对象 @RequestMapping("/hput") public String hput(){ User user =new User(10,"hash明星",new Date()); redisTemplate.opsForHash().put("huser","user",user); //定时(1分钟消失) redisTemplate.expire("huser",60, TimeUnit.SECONDS); return "Hashput ok"; } //Hash取数据 @RequestMapping("/hget") public Object hget(){ User user=(User)redisTemplate.opsForHash().get("huser","user"); System.out.println("Hash---------------------->"); System.out.println("val="+user); return user; } //List存对象 @RequestMapping("/lput") public String lput(){ User user =new User(10,"list明星",new Date()); redisTemplate.opsForList().leftPush("Listuser",user); //定时(1分钟消失) redisTemplate.expire("Listuser",60, TimeUnit.SECONDS); return "Listput ok"; } //List取数据 @RequestMapping("/lget") public Object lget(){ User user=(User)redisTemplate.opsForList().index("Listuser",0); System.out.println("List---------------------->"); System.out.println("val="+user); return user; } //Set存对象 @RequestMapping("/sput") public String sput(){ User user =new User(10,"set明星",new Date()); redisTemplate.opsForSet().add("Setuser",user); //定时(1分钟消失) redisTemplate.expire("Setuser",60, TimeUnit.SECONDS); return "Setput ok"; } //Set取数据 @RequestMapping("/sget") public Object sget(){ Set sets=redisTemplate.opsForSet().members("Setuser"); System.out.println("Set---------------------->"); System.out.println("val="+sets); return sets; } //sorted Set存对象 @RequestMapping("/sortput") public String sortput(){ User user =new User(6,"sort明星",new Date()); redisTemplate.opsForZSet().add("Sortuser",user,1); redisTemplate.opsForZSet().add("Sortuser","排名老二",2); //定时(1分钟消失) redisTemplate.expire("Sortuser",60, TimeUnit.SECONDS); return "sortput ok"; } //sorted Set取数据 @RequestMapping("/sortget") public Object sortget(){ Set sets=redisTemplate.opsForZSet().range("Sortuser",0,2); System.out.println("Sorted set---------------------->"); System.out.println("val="+sets); return sets; } //删除键 @RequestMapping("/del/{key}") public String del(@PathVariable("key")String key){ Boolean n=redisTemplate.delete(key); if (n){ return "删除成功"; }else { return "删除失败"; } } }总结
- String opsForValue()
- Hash opsForHash()
- set opsForSet()
- list opsForList()
- sorted set opsForZSet()
项目实战完整篇:https://www.jianshu.com/p/5596c3a4978d
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)