一、SpringBoot整合Redis
1.1导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
1.2在application.properties文件添加配置
#配置redis数据库索引(默认0号库)
spring.redis.database=0
#配置redis服务器ip地址
spring.redis.host=192.168.228.135
#配置redis服务器的端口号
spring.redis.port=6379
1.3创建controller
package com.qf.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/redis")
public class RedisController {
@Autowired
private RedisTemplate redisTemplate ;
@Autowired
private StringRedisTemplate stringRedisTemplate;
@RequestMapping("/testString")
public String test(){
//redisTemplate.opsForValue();//操作字符串
//redisTemplate.opsForList();//操作List
//redisTemplate.opsForSet();//操作Set
//redisTemplate.opsForZSet();//操作ZSet
//redisTemplate.opsForHash();//操作Map
redisTemplate.opsForValue().set("username","jack");
String username = (String)redisTemplate.opsForValue().get("username");
System.out.println(username);
stringRedisTemplate.opsForValue().set("password","123");
String password = stringRedisTemplate.opsForValue().get("password");
System.out.println(password);
return "success";
}
}
启动工程,进行测试