SpringBoot之集成Redis NoSql数据库
2023-03-14 10:23:39 时间
本篇文章只是简单的介绍一下SpringBoot集成Redis的使用(不包括Redis集群的使用),算是一篇入门文章吧。下面我们进入正题。
前期准备
我们现在pom.xml中引入redis的配置:
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency>简单的配置几个参数,参数的配置可以根据你的需求来进行配置,这里只是简单的介绍一下,所以我们只用最简单的配置就行了。
redis.serverName = 127.0.0.1:6379 #超时时间 redis.timeout = 8
获取配置信息
这里我们定义了一个类用来获取Redis的各项配置信息:
package com.zkn.learnspringboot.redis; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; /** * Created by zkn on 2016/8/14. */ @ConfigurationProperties(prefix = "redis") @Component public class RedisArguments { /** * redis的服务地址 */ private String serverName; /** * 超时时间 */ private Integer timeout; public String getServerName() { return serverName; } public void setServerName(String serverName) { this.serverName = serverName; } public Integer getTimeout() { return timeout; } public void setTimeout(Integer timeout) { this.timeout = timeout; } }
配置Redis实例
我们定义一个类用来获取Redis的实例:
package com.zkn.learnspringboot.redis; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; /** * Created by zkn on 2016/8/14. */ @Component public class RedisExampleBean { @Autowired private RedisArguments redisArguments; @Bean private JedisPool getJedisPoll(){ JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); String[] strServer = redisArguments.getServerName().split(":"); return new JedisPool(jedisPoolConfig,strServer[0],Integer.parseInt(strServer[1])); } }
Redis的基本工具类
接下来我们写一个类用来获取Reids的实例和释放Redis的连接。
package com.zkn.learnspringboot.redis; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; /** * Created by zkn on 2016/8/14. */ @Component public class RedisBaseUtil { @Autowired private JedisPool jedisPool; public Jedis getJedis(){ Jedis jedis = jedisPool.getResource(); if(jedis == null) return null; return jedis; } public void releaseJedis(Jedis jedis){ if(jedis == null) return; jedis.close(); } }OK,接下来我们写一个简单的字符串的操作类:
package com.zkn.learnspringboot.redis; import org.springframework.stereotype.Component; import redis.clients.jedis.Jedis; /** * Created by zkn on 2016/8/15. */ @Component public class RedisStringUtil extends RedisBaseUtil { public void putString(String key,String value){ Jedis jedis = getJedis(); try{ if (jedis != null){ jedis.set(key,value); jedis.expire(key,300); } }catch (Exception e){ e.printStackTrace(); }finally { releaseJedis(jedis); } } public String getString(String key){ Jedis jedis = getJedis(); try{ if (jedis != null){ return jedis.get(key); } }catch (Exception e){ e.printStackTrace(); }finally { releaseJedis(jedis); } return null; } }
访问应用
接下来我们来写一个Controller类来测试一下我们刚才的成果吧。
package com.zkn.learnspringboot.controller; import com.zkn.learnspringboot.redis.RedisStringUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; /** * Created by zkn on 2016/8/15. */ @Controller @ResponseBody public class RedisTestController { @Autowired private RedisStringUtil redisStringUtil; @RequestMapping("/putStringkey.do") public String putString(@RequestParam String key){ redisStringUtil.putString(key,key); return "保存成功"; } @RequestMapping("/getStringkey.do") public String getString(@RequestParam String key){ return redisStringUtil.getString(key); } }首先我们来进行一个保存的操作:
http://localhost:8080/putStringkey.do?key=xiaoerwangsan
OK接下来我们来测试一下看看能不能取到刚才我们保存的数据:
http://localhost:8080/getStringkey.do?key=xiaoerwangsan![](http://img.blog.csdn.net/20160821160807970?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
可以发现我们刚才放入的数据已经保存成功了。另外我们也可以通过命令开查看一下是否保存到Redis的库里。
本文只是简单的介绍一下SpringBoot和Redis结合的使用,仅供参考。
相关文章
- 基于海量日志和时序数据的质量建设最佳实践
- 利用ECS搭建公交提前报站系统的心得体会
- hibernate自定义sql关联查询结果组装为对象
- 流计算引擎数据一致性的本质
- 阿里云产品-2021 2月刊
- 爬虫系列:爬虫介绍
- 网络基础知识
- 【学习资料】第18期快速入门PostgreSQL应用开发与管理 – 8 PostgreSQL 管理
- mysql数据库操作
- 网页证书
- 使用ecs服务器进行mysql操作
- 阿里云服务器真好
- 数据库服务器
- 空间数据库设计
- 搜索引擎更新动态是每个SEOer需关注的,如何了解新动态呢?
- 阿里云数据库有哪几个热门数据库产品?要注意事项及优惠?
- HBASE入库方式比较
- 大数据可视化技术面临的挑战及应对措施
- 大数据可视化技术面临的挑战及应对措施
- 一次MySQL线上调优分享