博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java springcloud版b2b2c社交电商spring cloud分布式微服务(九)springboot整合Redis
阅读量:7244 次
发布时间:2019-06-29

本文共 1553 字,大约阅读时间需要 5 分钟。

java b2b2c电子商务社交平台源码请加企鹅求求:一零三八七七四六二六

引入依赖:

在pom文件中添加redis依赖:

org.springframework.boot
spring-boot-starter-data-redis
复制代码

配置数据源

spring.redis.host=localhostspring.redis.port=6379#spring.redis.password=spring.redis.database=1spring.redis.pool.max-active=8spring.redis.pool.max-wait=-1spring.redis.pool.max-idle=500spring.redis.pool.min-idle=0spring.redis.timeout=0复制代码

如果你的redis有密码,配置下即可。经过上述两步的操作,你可以访问redis数据了。

数据访问层dao

通过redisTemplate来访问redis.

@Repositorypublic class RedisDao {    @Autowired    private StringRedisTemplate template;    public  void setKey(String key,String value){        ValueOperations
ops = template.opsForValue(); ops.set(key,value,1, TimeUnit.MINUTES);//1分钟过期 } public String getValue(String key){ ValueOperations
ops = this.template.opsForValue(); return ops.get(key); }}复制代码

单元测试

@RunWith(SpringRunner.class)@SpringBootTestpublic class SpringbootRedisApplicationTests {    public static Logger logger= LoggerFactory.getLogger(SpringbootRedisApplicationTests.class);    @Test    public void contextLoads() {    }    @Autowired    RedisDao redisDao;    @Test    public void testRedis(){        redisDao.setKey("name","forezp");        redisDao.setKey("age","11");        logger.info(redisDao.getValue("name"));        logger.info(redisDao.getValue("age"));    }}复制代码

启动单元测试,你发现控制台打印了:

forezp11复制代码

单元测试通过;

Spring cloud b2b2c电子商务社交平台源码请加企鹅求求:一零三八七七四六二六

转载于:https://juejin.im/post/5cdd124a5188256964773e24

你可能感兴趣的文章
浅谈MySQL中优化sql语句查询常用的30种方法
查看>>
写在程序猿的困惑(特别Java程序猿)入行一年,感觉我不知道接下来该怎么办才能不断进步的,寻求翼...
查看>>
《C++程序设计教程——给予Visual Studio 2008》读书笔记3章
查看>>
使用phpize增加php模块
查看>>
一些常用软件的网络端口协议分类介绍
查看>>
poj 3253 Fence Repair(优先队列+哈夫曼树)
查看>>
微信公众平台教程和SDK收集
查看>>
C++ classes and uniform initialization
查看>>
转:Eclipse常见问题,快捷键收集
查看>>
[转]extjs组件添加事件监听的三种方式
查看>>
教你安装漂亮的Arc GTK主题
查看>>
PowerDesigner使用心得
查看>>
交叉报表列头排序时遇到的oracle问题—oracle ORA-12704:字符集不匹配、varchar2转化为nvarchar2字符缺失、case when else后的字符类型要一致...
查看>>
思数云hadoop目录
查看>>
图的创建和遍历(BFS/DFS)
查看>>
使用Robomongo 连接MongoDB 3.x 报 Authorization failed 解决办法(转)
查看>>
__stdcall,__cdecl,__fastcall的区别
查看>>
(转载)hibernate缓存
查看>>
UIcollectionView的使用(首页的搭建3)
查看>>
asp.net页面关闭的时候如何触发事件?
查看>>