SpringBoot之集成Redis NoSql数据库

释放双眼,带上耳机,听听看~!

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

前期准备

我们现在pom.xml中引入redis的配置:

[html] view plain
 copy

  1. <

dependency

  

  1.     

<
groupId

org.springframework.data
</
groupId

  

  1.     

<
artifactId

spring-data-redis
</
artifactId

  

  1. </

dependency

  

  1. <

dependency

  

  1.     

<
groupId

redis.clients
</
groupId

  

  1.     

<
artifactId

jedis
</
artifactId

  

  1. </

dependency

  
简单的配置几个参数,参数的配置可以根据你的需求来进行配置,这里只是简单的介绍一下,所以我们只用最简单的配置就行了。

[html] view plain
 copy

  1. redis.serverName

 = 
127
.0.0.1:6379  

  1. #超时时间  
  2. redis.timeout

 = 
8
  

获取配置信息

这里我们定义了一个类用来获取Redis的各项配置信息:

[java] view plain
 copy

  1. package

 com.zkn.learnspringboot.redis;  

  1.   
  2. import

 org.springframework.beans.factory.annotation.Value;  

  1. import

 org.springframework.boot.context.properties.ConfigurationProperties;  

  1. import

 org.springframework.stereotype.Component;  

  1.   
  2. /** 
  3.  * Created by zkn on 2016/8/14. 
  4.  */

  

  1. @ConfigurationProperties

(prefix = 
"redis"
)  

  1. @Component

  

  1. public

 
class
 RedisArguments {  

  1.   
  2.     

/** 

  1.      * redis的服务地址 
  2.      */

  

  1.     

private
 String serverName;  

  1.     

/** 

  1.      * 超时时间 
  2.      */

  

  1.     

private
 Integer timeout;  

  1.   
  2.     

public
 String getServerName() {  

  1.         

return
 serverName;  

  1.     }  
  2.   
  3.     

public
 
void
 setServerName(String serverName) {  

  1.         

this
.serverName = serverName;  

  1.     }  
  2.   
  3.     

public
 Integer getTimeout() {  

  1.         

return
 timeout;  

  1.     }  
  2.   
  3.     

public
 
void
 setTimeout(Integer timeout) {  

  1.         

this
.timeout = timeout;  

  1.     }  
  2. }  

配置Redis实例

我们定义一个类用来获取Redis的实例:

[java] view plain
 copy

  1. package

 com.zkn.learnspringboot.redis;  

  1.   
  2. import

 org.springframework.beans.factory.annotation.Autowired;  

  1. import

 org.springframework.context.annotation.Bean;  

  1. import

 org.springframework.stereotype.Component;  

  1. import

 redis.clients.jedis.JedisPool;  

  1. import

 redis.clients.jedis.JedisPoolConfig;  

  1.   
  2. /** 
  3.  * Created by zkn on 2016/8/14. 
  4.  */

  

  1. @Component

  

  1. public

 
class
 RedisExampleBean {  

  1.   
  2.     

@Autowired
  

  1.     

private
 RedisArguments redisArguments;  

  1.     

@Bean
  

  1.     

private
 JedisPool getJedisPoll(){  

  1.   
  2.         JedisPoolConfig jedisPoolConfig = 

new
 JedisPoolConfig();  

  1.         String[] strServer = redisArguments.getServerName().split(

":"
);  

  1.         

return
 
new
 JedisPool(jedisPoolConfig,strServer[
0
],Integer.parseInt(strServer[
1
]));  

  1.     }  
  2.   
  3. }  

Redis的基本工具类

接下来我们写一个类用来获取Reids的实例和释放Redis的连接。

[java] view plain
 copy

  1. package

 com.zkn.learnspringboot.redis;  

  1.   
  2. import

 org.springframework.beans.factory.annotation.Autowired;  

  1. import

 org.springframework.stereotype.Component;  

  1. import

 redis.clients.jedis.Jedis;  

  1. import

 redis.clients.jedis.JedisPool;  

  1.   
  2. /** 
  3.  * Created by zkn on 2016/8/14. 
  4.  */

  

  1. @Component

  

  1. public

 
class
 RedisBaseUtil {  

  1.   
  2.     

@Autowired
  

  1.     

private
 JedisPool jedisPool;  

  1.   
  2.     

public
 Jedis getJedis(){  

  1.   
  2.         Jedis jedis = jedisPool.getResource();  
  3.         

if
(jedis == 
null
)  

  1.             

return
 
null
;  

  1.         

return
 jedis;  

  1.     }  
  2.   
  3.     

public
 
void
 releaseJedis(Jedis jedis){  

  1.   
  2.         

if
(jedis == 
null
)  

  1.             

return
;  

  1.         jedis.close();  
  2.     }  
  3.   
  4. }  

OK,接下来我们写一个简单的字符串的操作类:

[java] view plain
 copy

  1. package

 com.zkn.learnspringboot.redis;  

  1.   
  2. import

 org.springframework.stereotype.Component;  

  1. import

 redis.clients.jedis.Jedis;  

  1.   
  2. /** 
  3.  * Created by zkn on 2016/8/15. 
  4.  */

  

  1. @Component

  

  1. public

 
class
 RedisStringUtil 
extends
 RedisBaseUtil {  

  1.   
  2.     

public
 
void
 putString(String key,String value){  

  1.         Jedis jedis = getJedis();  
  2.         

try
{  

  1.             

if
 (jedis != 
null
){  

  1.                 jedis.set(key,value);  
  2.                 jedis.expire(key,

300
);  

  1.             }  
  2.         }

catch
 (Exception e){  

  1.             e.printStackTrace();  
  2.         }

finally
 {  

  1.             releaseJedis(jedis);  
  2.         }  
  3.     }  
  4.   
  5.     

public
 String getString(String key){  

  1.         Jedis jedis = getJedis();  
  2.         

try
{  

  1.             

if
 (jedis != 
null
){  

  1.                 

return
 jedis.get(key);  

  1.             }  
  2.         }

catch
 (Exception e){  

  1.             e.printStackTrace();  
  2.         }

finally
 {  

  1.             releaseJedis(jedis);  
  2.         }  
  3.         

return
 
null
;  

  1.     }  
  2. }  

访问应用

接下来我们来写一个Controller类来测试一下我们刚才的成果吧。

[java] view plain
 copy

  1. package

 com.zkn.learnspringboot.controller;  

  1.   
  2. import

 com.zkn.learnspringboot.redis.RedisStringUtil;  

  1. import

 org.springframework.beans.factory.annotation.Autowired;  

  1. import

 org.springframework.stereotype.Controller;  

  1. import

 org.springframework.web.bind.annotation.RequestMapping;  

  1. import

 org.springframework.web.bind.annotation.RequestParam;  

  1. import

 org.springframework.web.bind.annotation.ResponseBody;  

  1.   
  2. /** 
  3.  * Created by zkn on 2016/8/15. 
  4.  */

  

  1. @Controller

  

  1. @ResponseBody

  

  1. public

 
class
 RedisTestController {  

  1.   
  2.     

@Autowired
  

  1.     

private
 RedisStringUtil redisStringUtil;  

  1.   
  2.     

@RequestMapping
(
"/putStringkey.do"
)  

  1.     

public
 String putString(
@RequestParam
 String key){  

  1.         redisStringUtil.putString(key,key);  
  2.         

return
 
"保存成功"
;  

  1.     }  
  2.   
  3.     

@RequestMapping
(
"/getStringkey.do"
)  

  1.     

public
 String getString(
@RequestParam
 String key){  

  1.   
  2.         

return
 redisStringUtil.getString(key);  

  1.     }  

给TA打赏
共{{data.count}}人
人已打赏
安全运维

OpenSSH-8.7p1离线升级修复安全漏洞

2021-10-23 10:13:25

安全运维

设计模式的设计原则

2021-12-12 17:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索