Spring Boot 学习笔记--整合Redis

1.新建Spring Boot项目

添加spring-boot-starter-data-redis依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

本文会根据StringRedisTemplate、RedisTemplate这两个模板来介绍,其中StringRedisTemplate继承自RedisTemplate,只能操作键值都是String类型的数据。在实际开发中建议使用RedisTemplate<K,V>。

查询源码可知RedisTemplate默认使用JdkSerializationRedisSerializer序列化,而StringRedisTemplate则使用StringRedisSerializer。

2.配置redis

在application.properties中配置如下:

 1 ########################################################
 2 ###Redis (RedisConfiguration)
 3 ########################################################
 4 spring.redis.database=1
 5 spring.redis.host=127.0.0.1
 6 spring.redis.port=6379
 7 spring.redis.password=
 8 spring.redis.pool.max-idle=8
 9 spring.redis.pool.min-idle=0
10 spring.redis.pool.max-active=8
11 spring.redis.pool.max-wait=-1
12 spring.redis.timeout=5000

3.编写代码

  1.创建model

 1 package com.haq.entity;
 2 
 3 import java.io.Serializable;
 4 
 5 /**
 6  * Created by on 2017/2/24.
 7  */
 8 public class User implements Serializable {
 9 
10     private static final long serialVersionUId = 1L;
11 
12     private String id;
13     private String name;
14 
15     public User(){
16         super();
17     }
18 
19     public String getId() {
20         return id;
21     }
22 
23     public void setId(String id) {
24         this.id = id;
25     }
26 
27     public String getName() {
28         return name;
29     }
30 
31     public void setName(String name) {
32         this.name = name;
33     }
34 
35     @Override
36     public String toString() {
37         return "User{" +
38                 "id='" + id + '\'' +
39                 ", name='" + name + '\'' +
40                 '}';
41     }
42 }

 

  2.创建RedisService
 1 package com.haq.common.redis;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.data.redis.core.RedisTemplate;
 5 import org.springframework.data.redis.core.StringRedisTemplate;
 6 import org.springframework.data.redis.core.ValueOperations;
 7 import org.springframework.stereotype.Service;
 8 
 9 import javax.annotation.Resource;
10 
11 /**
12  * Created by on 2017/3/1.
13  */
14 @Service
15 public class RedisService {
16 
17     @Autowired
18     StringRedisTemplate stringRedisTemplate;
19 
20 
21     @Resource(name = "stringRedisTemplate")
22     ValueOperations<String, String> valOpsStr;
23 
24     @Autowired
25     RedisTemplate<Object, Object> redisTemplate;
26 
27     @Resource(name = "redisTemplate")
28     ValueOperations<Object, Object> valOpsObj;
29 
30     /**
31      * 根据指定key获取String
32      * @param key
33      * @return
34      */
35     public String getStr(String key){
36         return valOpsStr.get(key);
37     }
38 
39     /**
40      * 设置Str缓存
41      * @param key
42      * @param val
43      */
44     public void setStr(String key, String val){
45         valOpsStr.set(key,val);
46     }
47 
48     /**
49      * 删除指定key
50      * @param key
51      */
52     public void del(String key){
53         stringRedisTemplate.delete(key);
54     }
55 
56     /**
57      * 根据指定o获取Object
58      * @param o
59      * @return
60      */
61     public Object getObj(Object o){
62         return valOpsObj.get(o);
63     }
64 
65     /**
66      * 设置obj缓存
67      * @param o1
68      * @param o2
69      */
70     public void setObj(Object o1, Object o2){
71         valOpsObj.set(o1, o2);
72     }
73 
74     /**
75      * 删除Obj缓存
76      * @param o
77      */
78     public void delObj(Object o){
79         redisTemplate.delete(o);
80     }
81 
82 }
  3.创建RedisController
  1 package com.haq.web;
  2 
  3 import com.haq.common.redis.RedisService;
  4 import com.haq.entity.User;
  5 import org.springframework.beans.factory.annotation.Autowired;
  6 import org.springframework.web.bind.annotation.RequestMapping;
  7 import org.springframework.web.bind.annotation.RestController;
  8 
  9 /**
 10  * Created by on 2017/3/1.
 11  */
 12 @RestController
 13 @RequestMapping("/redis")
 14 public class RedisController {
 15 
 16     @Autowired
 17     RedisService redisService;
 18 
 19     /**
 20      * 设置Str缓存
 21      * @param key
 22      * @param val
 23      * @return
 24      */
 25     @RequestMapping(value = "setStr")
 26     public String setStr(String key, String val){
 27         try {
 28             redisService.setStr(key, val);
 29             return "success";
 30         } catch (Exception e){
 31             e.printStackTrace();
 32             return "error";
 33         }
 34     }
 35 
 36     /**
 37      * 根据key查询Str缓存
 38      * @param key
 39      * @return
 40      */
 41     @RequestMapping(value = "getStr")
 42     public String getStr(String key){
 43         return redisService.getStr(key);
 44     }
 45 
 46 
 47     /**
 48      * 根据key产出Str缓存
 49      * @param key
 50      * @return
 51      */
 52     @RequestMapping(value = "delStr")
 53     public String delStr(String key){
 54         try {
 55             redisService.del(key);
 56             return "success";
 57         } catch (Exception e){
 58             return "error";
 59         }
 60     }
 61 
 62 
 63     /**
 64      * 设置obj缓存
 65      * @param key
 66      * @param user
 67      * @return
 68      */
 69     @RequestMapping(value = "setObj")
 70     public String setObj(String key, User user){
 71         try {
 72             redisService.setObj(key, user);
 73             return "success";
 74         } catch (Exception e){
 75             e.printStackTrace();
 76             return "error";
 77         }
 78     }
 79 
 80     /**
 81      * 获取obj缓存
 82      * @param key
 83      * @return
 84      */
 85     @RequestMapping(value = "getObj")
 86     public Object getObj(String key){
 87         return redisService.getObj(key);
 88     }
 89 
 90 
 91     /**
 92      * 删除obj缓存
 93      * @param key
 94      * @return
 95      */
 96     @RequestMapping(value = "delObj")
 97     public Object delObj(String key){
 98         try {
 99             redisService.delObj(key);
100             return "success";
101         } catch (Exception e){
102             e.printStackTrace();
103             return "error";
104         }
105     }
106 
107 }

4.运行测试

1.StringRedisTemplate

http://127.0.0.1:8080/redis/setStr?key=aa&val=name

http://127.0.0.1:8080/redis/getStr?key=aa

http://127.0.0.1:8080/redis/delStr?key=aa

2.RedisTemplate

http://127.0.0.1:8080/redis/setObj?key=aa&id=1&name=test

http://127.0.0.1:8080/redis/getObj?key=aa

http://127.0.0.1:8080/redis/delObj?key=aa

以上地址都能通过测试,在这里就不一一截图了

 

后记

可以直接使用redistemplate来保存string类型,不过使用redis client看起来就不太直观,因为JdkSerializationRedisSerializer是使用二进制形式存储数据。

最后希望大家有问题能多多指教,O(∩_∩)O谢谢

 

posted @ 2017-03-01 19:12  采采灬卷耳  阅读(28109)  评论(1编辑  收藏  举报