SpringBoot之集成Redis NoSql数据库

本篇文章只是简单的介绍一下SpringBoot集成Redis的使用(不包括Redis集群的使用),算是一篇入门文章吧。下面我们进入正题。

前期准备

我们现在pom.xml中引入redis的配置:
[html]  view plain  copy
  1. <dependency>  
  2.     <groupId>org.springframework.data</groupId>  
  3.     <artifactId>spring-data-redis</artifactId>  
  4. </dependency>  
  5. <dependency>  
  6.     <groupId>redis.clients</groupId>  
  7.     <artifactId>jedis</artifactId>  
  8. </dependency>  
简单的配置几个参数,参数的配置可以根据你的需求来进行配置,这里只是简单的介绍一下,所以我们只用最简单的配置就行了。
[html]  view plain  copy
  1. redis.serverName = 127.0.0.1:6379  
  2. #超时时间  
  3. redis.timeout = 8  

获取配置信息

这里我们定义了一个类用来获取Redis的各项配置信息:
[java]  view plain  copy
  1. package com.zkn.learnspringboot.redis;  
  2.   
  3. import org.springframework.beans.factory.annotation.Value;  
  4. import org.springframework.boot.context.properties.ConfigurationProperties;  
  5. import org.springframework.stereotype.Component;  
  6.   
  7. /** 
  8.  * Created by zkn on 2016/8/14. 
  9.  */  
  10. @ConfigurationProperties(prefix = "redis")  
  11. @Component  
  12. public class RedisArguments {  
  13.   
  14.     /** 
  15.      * redis的服务地址 
  16.      */  
  17.     private String serverName;  
  18.     /** 
  19.      * 超时时间 
  20.      */  
  21.     private Integer timeout;  
  22.   
  23.     public String getServerName() {  
  24.         return serverName;  
  25.     }  
  26.   
  27.     public void setServerName(String serverName) {  
  28.         this.serverName = serverName;  
  29.     }  
  30.   
  31.     public Integer getTimeout() {  
  32.         return timeout;  
  33.     }  
  34.   
  35.     public void setTimeout(Integer timeout) {  
  36.         this.timeout = timeout;  
  37.     }  
  38. }  

配置Redis实例

我们定义一个类用来获取Redis的实例:
[java]  view plain  copy
  1. package com.zkn.learnspringboot.redis;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.context.annotation.Bean;  
  5. import org.springframework.stereotype.Component;  
  6. import redis.clients.jedis.JedisPool;  
  7. import redis.clients.jedis.JedisPoolConfig;  
  8.   
  9. /** 
  10.  * Created by zkn on 2016/8/14. 
  11.  */  
  12. @Component  
  13. public class RedisExampleBean {  
  14.   
  15.     @Autowired  
  16.     private RedisArguments redisArguments;  
  17.     @Bean  
  18.     private JedisPool getJedisPoll(){  
  19.   
  20.         JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();  
  21.         String[] strServer = redisArguments.getServerName().split(":");  
  22.         return new JedisPool(jedisPoolConfig,strServer[0],Integer.parseInt(strServer[1]));  
  23.     }  
  24.   
  25. }  

Redis的基本工具类

接下来我们写一个类用来获取Reids的实例和释放Redis的连接。
[java]  view plain  copy
  1. package com.zkn.learnspringboot.redis;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.stereotype.Component;  
  5. import redis.clients.jedis.Jedis;  
  6. import redis.clients.jedis.JedisPool;  
  7.   
  8. /** 
  9.  * Created by zkn on 2016/8/14. 
  10.  */  
  11. @Component  
  12. public class RedisBaseUtil {  
  13.   
  14.     @Autowired  
  15.     private JedisPool jedisPool;  
  16.   
  17.     public Jedis getJedis(){  
  18.   
  19.         Jedis jedis = jedisPool.getResource();  
  20.         if(jedis == null)  
  21.             return null;  
  22.         return jedis;  
  23.     }  
  24.   
  25.     public void releaseJedis(Jedis jedis){  
  26.   
  27.         if(jedis == null)  
  28.             return;  
  29.         jedis.close();  
  30.     }  
  31.   
  32. }  
OK,接下来我们写一个简单的字符串的操作类:
[java]  view plain  copy
  1. package com.zkn.learnspringboot.redis;  
  2.   
  3. import org.springframework.stereotype.Component;  
  4. import redis.clients.jedis.Jedis;  
  5.   
  6. /** 
  7.  * Created by zkn on 2016/8/15. 
  8.  */  
  9. @Component  
  10. public class RedisStringUtil extends RedisBaseUtil {  
  11.   
  12.     public void putString(String key,String value){  
  13.         Jedis jedis = getJedis();  
  14.         try{  
  15.             if (jedis != null){  
  16.                 jedis.set(key,value);  
  17.                 jedis.expire(key,300);  
  18.             }  
  19.         }catch (Exception e){  
  20.             e.printStackTrace();  
  21.         }finally {  
  22.             releaseJedis(jedis);  
  23.         }  
  24.     }  
  25.   
  26.     public String getString(String key){  
  27.         Jedis jedis = getJedis();  
  28.         try{  
  29.             if (jedis != null){  
  30.                 return jedis.get(key);  
  31.             }  
  32.         }catch (Exception e){  
  33.             e.printStackTrace();  
  34.         }finally {  
  35.             releaseJedis(jedis);  
  36.         }  
  37.         return null;  
  38.     }  
  39. }  

访问应用

接下来我们来写一个Controller类来测试一下我们刚才的成果吧。
[java]  view plain  copy
  1. package com.zkn.learnspringboot.controller;  
  2.   
  3. import com.zkn.learnspringboot.redis.RedisStringUtil;  
  4. import org.springframework.beans.factory.annotation.Autowired;  
  5. import org.springframework.stereotype.Controller;  
  6. import org.springframework.web.bind.annotation.RequestMapping;  
  7. import org.springframework.web.bind.annotation.RequestParam;  
  8. import org.springframework.web.bind.annotation.ResponseBody;  
  9.   
  10. /** 
  11.  * Created by zkn on 2016/8/15. 
  12.  */  
  13. @Controller  
  14. @ResponseBody  
  15. public class RedisTestController {  
  16.   
  17.     @Autowired  
  18.     private RedisStringUtil redisStringUtil;  
  19.   
  20.     @RequestMapping("/putStringkey.do")  
  21.     public String putString(@RequestParam String key){  
  22.         redisStringUtil.putString(key,key);  
  23.         return "保存成功";  
  24.     }  
  25.   
  26.     @RequestMapping("/getStringkey.do")  
  27.     public String getString(@RequestParam String key){  
  28.   
  29.         return redisStringUtil.getString(key);  
  30.     }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值