ARTICLE DETAIL

资讯详情

深耕网站建设、视觉设计与SEO优化的一线实战洞察。

Spring boot 集成redis

Spring boot 集成redis

一、使用spring bootspring-boot-starter-data-redis

导入maven包

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

spring boot配置文件

spring.redis.host=127.0.0.1
spring.redis.database=0
spring.redis.username=
spring.redis.password=

然后直接使用RedisTemplate里面的api去操作redis即可

@Autowired
private RedisTemplate<Object,Object> redisTemplate;public void set() {redisTemplate.opsForValue().set("test", "test");
}

二,使用jedis方式

导入maven依赖

<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>3.6.0</version>
</dependency>

使用示例

//1.new 一个Jedis对象
Jedis jedis = new Jedis("192.168.11.220",6379);
//检测连接
System.out.println(jedis.ping());
String set = jedis.set("key", "value");
System.out.println(set);

运行结果

PONG
OK

更多请参照Jedis里面api

返回列表