springboot整合redis

1、引入maven配置
< dependency >
< groupId > org.springframework.boot </ groupId >
< artifactId > spring-boot-starter-data-redis </ artifactId >
</ dependency >
2、配置application.properties
#默认访问索引为0的库
spring.redis.database = 0
#redis服务器地址
spring.redis.host = 192.168.52.128
#端口
spring.redis.port = 6379
# 目标为保持在池中的最小空闲连接数。这个设置只有在正面的情况下才有效果
spring.redis.jedis.pool.min-idle = 0
#池在给定时间可以分配的最大连接数。使用负值无限制。
spring.redis.jedis.pool.max-active = 8
#池中“空闲”连接的最大数量。使用负值表示无限数量的空闲连接。
spring.redis.jedis.pool.max-idle = 8
#连接分配在池被耗尽时抛出异常之前应该阻塞的最长时间量(以毫秒为单位)。使用负值可以无限期地阻止。
spring.redis.jedis.pool.max-wait = -1
#以毫秒为单位的连接超时。
spring.redis.timeout = 1000
3、创建RedisComponent类,注入StringRedisTemplate
package cn.itfeiyue.springbootredis.config;

import org.springframework.beans.factory.annotation. Autowired ;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype. Component ;

/**
* @Description: java 类作用描述
* @Author: ZHANGPENGFEI
* @CreateDate: 2018/5/4 15:45
* @Version: 1.0
*/
@Component
public class RedisComponent {
@Autowired
private StringRedisTemplate stringRedisTemplate ;

    //保存
public void set(String key, String value){
        //获取ValueOperations对象 (这里对应Redis中数据类型String类型,还有HashOperations对应Hash类型<参见博客:“https://blog.csdn.net/qq_34409255/article/details/80198458”>)
ValueOperations<String,String> ops = this . stringRedisTemplate .opsForValue();
        //判断当前库中是否存在该key
boolean bExistent = this . stringRedisTemplate .hasKey(key);
if (bExistent) {
System. out .println( "this key is bExistent!" );
} else {
ops.set(key, value);
}
}

    //根据key获取value
public String get(String key){
return this . stringRedisTemplate .opsForValue().get(key);
}

    //根据key删除对应的信息
public void del(String key){
this . stringRedisTemplate .delete(key);
}
}
4、创建Controller进行测试
package cn.itfeiyue.springbootredis.controller;

import cn.itfeiyue.springbootredis.config.RedisComponent;
import org.springframework.beans.factory.annotation. Autowired ;
import org.springframework.web.bind.annotation. PathVariable ;
import org.springframework.web.bind.annotation. RequestMapping ;
import org.springframework.web.bind.annotation. RestController ;

/**
* @Description: java 类作用描述
* @Author: ZHANGPENGFEI
* @CreateDate: 2018/5/4 15:46
* @Version: 1.0
*/
@RestController
@RequestMapping (value= "/web" )
public class WebController {

@Autowired
private RedisComponent redisComponet ;

@RequestMapping (value= "/set/{key}/{value}" )
public String set( @PathVariable String key, @PathVariable String value){
redisComponet .set(key, value);
return "set key succ!" ;
}

@RequestMapping (value= "/get/{key}" )
public String get( @PathVariable String key){
return redisComponet .get(key);
}

@RequestMapping (value= "/del/{key}" )
public void del( @PathVariable String key){
redisComponet .del(key);
}
}
5、效果图


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值