1.Redis依赖安装
在pom.xml文件中添加Springboot的Redis依赖;
<!-- redis依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
spring-boot-starter-data-redis是Springboot的Redis启动器,它会自动配置好Redis所需的基本组件。
2.Redis配置连接信息
在application.yml 或 application.properties文件中配置Redis的连接信息;
(1)application.yml配置文件格式
# Redis配置
redis:
host: localhost(或192.168.2.XX) #Redis服务器地址(电脑ip)
port: 6379 #Redis服务器端口
database: 0 #Redis服务器密码(默认为0)
password: #Redis服务器密码(如果有的话)
timeout: 10s #连接超时时间
lettuce:
pool:
max-active: 8 #连接池最大连接数(使用负值表示没有限制)
max-wait: -1ms #连接池最大阻塞等待时间(使用负值表示没有限制)
max-idle: 8 #连接池中的最大空闲连接
min-idle: 0 #连接池中最小空闲连接
(2)application.properties配置文件格式
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.database=0
spring.redis.password=yourpassword
spring.redis.jedis.pool.max-active=8
spring.redis.jedis.pool.max-wait=-1ms
spring.redis.jedis.pool.max-idle=8
spring.redis.jedis.pool.min-idle=0
3.Redis配置文件
RedisConfig.java文件
package com.example.mybatis_dev.util;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
return redisTemplate;
}
}
4.测试配置
在使用Redis类中直接导入Redis来验证Redis配置是否正确;
package com.example.mybatis_dev.controller;
import com.example.mybatis_dev.entity.User;
import com.example.mybatis_dev.service.UserService;
import com.example.mybatis_dev.util.R;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* ClassName:UserController
* Package:IntelliJ IDEA
*
* @Create 2024/1/13 23:05
*/
@RestController
@RequestMapping("/user")
public class UserController {
private final Logger logger= LoggerFactory.getLogger(UserController.class);
@Autowired
UserService userService;
// 导入Redis
@Autowired
RedisTemplate<String,Object> redisTemplate;
/**
* 查询所有用户
* @return
*/
@PostMapping("/selectAllUser")
public R selectAllUser(){
// List<User> users = userService.selectAllUser();
List<User> userList=userService.selectAllUser();
if(userList.size()==0){
return R.fail("没有数据");
}
else{
//使用Redis中的方法
redisTemplate.opsForValue().set("1",userList);
System.out.println(redisTemplate.opsForValue().get("1"));
return R.ok(userList);
}
}
}