springboot整合redis
1.整合redis
1.1首先安裝redis,并啟動(dòng)redis(這里忽略介紹,具體安裝和啟動(dòng)方法自行百度)
1.2 創(chuàng)建Spring boot項(xiàng)目,引入可在創(chuàng)建項(xiàng)目時(shí)直接引入如圖:
也可依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
1.3 在properties 配置redis連接信息
spring.redis.host=localhost
spring.redis.database=0
spring.redis.port=6379
#你的密碼
spring.redis.password=*****
1.4 web層測(cè)試
@RestController
public class HelloController {
@Autowired
StringRedisTemplate stringRedisTemplate;
@GetMapping("/set")
public void set(){
ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
ops.set("name","王志海");
}
@GetMapping("/get")
public void get(){
ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
String s = ops.get("name");
System.out.println(s);
}
}
注意:引用redis的StringRedisTemplate
在瀏覽器進(jìn)行測(cè)試存入redis和測(cè)試獲取redis的信息
這是一段簡(jiǎn)單的整合,實(shí)際開(kāi)發(fā)中,redis以集群的形式出現(xiàn),過(guò)段時(shí)間整合redis集群的實(shí)現(xiàn)的方式