一、添加maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
二、添加配置项
redis: database: 1 # 指定所在的库 host: 127.0.0.1 # Redis服务器地址 写你的ip port: 6379 # Redis服务器连接端口 password: 123456 # Redis服务器连接密码 lettuce: pool: max-active: 200 # 连接池最大连接数(使用负值表示没有限制) 类似于mysql的连接池 max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制) 表示连接池的链接拿完了 现在去申请需要等待的时间 max-idle: 10 # 连接池中的最大空闲连接 min-idle: 0 # 连接池中的最小空闲连接 timeout: 6000 # 连接超时时间(毫秒) 去链接redis服务端
三、新增配置类
package com.saas.springboot.study.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
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.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(redisConnectionFactory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
//key使用String序列化
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
//value使用jackson序列化
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
四、编辑实体类
package com.saas.springboot.study.entity.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 用户表
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserVO {
/**
* 主键id
*/
Integer id;
/**
* 用户名
*/
String username;
/**
* 密码
*/
String password;
}
五、编写接口
package com.saas.springboot.study.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.saas.springboot.study.entity.dto.UserSaveDTO;
import com.saas.springboot.study.entity.po.User;
import com.saas.springboot.study.entity.vo.UserVO;
import com.saas.springboot.study.service.UserService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
@RestController
public class UserController {
@Resource
private UserService userService;
//保存用户信息到redis
@PostMapping("/saveUserInfoToRedis")
public void saveUserInfoToRedis(@RequestBody UserSaveDTO userSaveDTO){
userService.saveUserInfoToRedis(userSaveDTO);
}
//从redis获取用户信息
@PostMapping("/getUserInfoFromRedis")
public UserVO getUserInfoFromRedis(){
return userService.getUserInfoFromRedis();
}
}
六、编写业务层
1.编写service层
package com.saas.springboot.study.service;
import com.saas.springboot.study.entity.dto.UserSaveDTO;
import com.saas.springboot.study.entity.vo.UserVO;
public interface UserService {
void saveUserInfoToRedis(UserSaveDTO userSaveDTO);
UserVO getUserInfoFromRedis();
}
2.编写service实现层
package com.saas.springboot.study.service.impl;
import com.saas.springboot.study.entity.dto.UserSaveDTO;
import com.saas.springboot.study.entity.vo.UserVO;
import com.saas.springboot.study.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class UserServiceImpl implements UserService {
@Resource
private RedisTemplate redisTemplate;
@Override
public void saveUserInfoToRedis(UserSaveDTO userSaveDTO) {
UserVO user = new UserVO();
BeanUtils.copyProperties(userSaveDTO,user);
redisTemplate.opsForValue().set("user",user);
}
@Override
public UserVO getUserInfoFromRedis() {
return (UserVO)redisTemplate.opsForValue().get("user");
}
}
七、测试接口
1.访问http://localhost:8888/saveUserInfoToRedis 保存redis
2.访问http://localhost:8888/getUserInfoFromRedis 获取redis
本文转载自: https://blog.csdn.net/qq_35160479/article/details/128041437
版权归原作者 这人很懒没留下什么 所有, 如有侵权,请联系我们删除。
版权归原作者 这人很懒没留下什么 所有, 如有侵权,请联系我们删除。