• SpringBoot2.7.4整合Redis


    目录

    一、添加maven依赖

    二、添加配置项

    三、新增配置类

    四、编辑实体类

    五、编写接口

    六、编写业务层

    1.编写service层

    2.编写service实现层 

    七、测试接口


    一、添加maven依赖

    
       org.springframework.boot
       spring-boot-starter-data-redis
    
    
       org.projectlombok
       lombok
       1.16.10
    

    、添加配置项

    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服务端

    、新增配置类

    1. package com.saas.springboot.study.config;
    2. import com.fasterxml.jackson.annotation.JsonAutoDetect;
    3. import com.fasterxml.jackson.annotation.PropertyAccessor;
    4. import com.fasterxml.jackson.databind.ObjectMapper;
    5. import org.springframework.context.annotation.Bean;
    6. import org.springframework.context.annotation.Configuration;
    7. import org.springframework.data.redis.connection.RedisConnectionFactory;
    8. import org.springframework.data.redis.core.RedisTemplate;
    9. import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
    10. import org.springframework.data.redis.serializer.StringRedisSerializer;
    11. @Configuration
    12. public class RedisConfig {
    13. @Bean
    14. public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
    15. RedisTemplate redisTemplate = new RedisTemplate();
    16. redisTemplate.setConnectionFactory(redisConnectionFactory);
    17. Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
    18. ObjectMapper objectMapper = new ObjectMapper();
    19. objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    20. objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    21. jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
    22. //key使用String序列化
    23. redisTemplate.setKeySerializer(new StringRedisSerializer());
    24. redisTemplate.setHashKeySerializer(new StringRedisSerializer());
    25. //value使用jackson序列化
    26. redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
    27. redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
    28. redisTemplate.afterPropertiesSet();
    29. return redisTemplate;
    30. }
    31. }

    、编辑实体类

    1. package com.saas.springboot.study.entity.vo;
    2. import lombok.AllArgsConstructor;
    3. import lombok.Data;
    4. import lombok.NoArgsConstructor;
    5. /**
    6. * 用户表
    7. */
    8. @Data
    9. @AllArgsConstructor
    10. @NoArgsConstructor
    11. public class UserVO {
    12. /**
    13. * 主键id
    14. */
    15. Integer id;
    16. /**
    17. * 用户名
    18. */
    19. String username;
    20. /**
    21. * 密码
    22. */
    23. String password;
    24. }

    、编写接口

    1. package com.saas.springboot.study.controller;
    2. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    3. import com.saas.springboot.study.entity.dto.UserSaveDTO;
    4. import com.saas.springboot.study.entity.po.User;
    5. import com.saas.springboot.study.entity.vo.UserVO;
    6. import com.saas.springboot.study.service.UserService;
    7. import org.springframework.web.bind.annotation.*;
    8. import javax.annotation.Resource;
    9. import java.util.List;
    10. @RestController
    11. public class UserController {
    12. @Resource
    13. private UserService userService;
    14. //保存用户信息到redis
    15. @PostMapping("/saveUserInfoToRedis")
    16. public void saveUserInfoToRedis(@RequestBody UserSaveDTO userSaveDTO){
    17. userService.saveUserInfoToRedis(userSaveDTO);
    18. }
    19. //从redis获取用户信息
    20. @PostMapping("/getUserInfoFromRedis")
    21. public UserVO getUserInfoFromRedis(){
    22. return userService.getUserInfoFromRedis();
    23. }
    24. }

    、编写业务层

    1.编写service层

    1. package com.saas.springboot.study.service;
    2. import com.saas.springboot.study.entity.dto.UserSaveDTO;
    3. import com.saas.springboot.study.entity.vo.UserVO;
    4. public interface UserService {
    5. void saveUserInfoToRedis(UserSaveDTO userSaveDTO);
    6. UserVO getUserInfoFromRedis();
    7. }

    2.编写service实现层 

    1. package com.saas.springboot.study.service.impl;
    2. import com.saas.springboot.study.entity.dto.UserSaveDTO;
    3. import com.saas.springboot.study.entity.vo.UserVO;
    4. import com.saas.springboot.study.service.UserService;
    5. import org.springframework.beans.BeanUtils;
    6. import org.springframework.data.redis.core.RedisTemplate;
    7. import org.springframework.stereotype.Service;
    8. import javax.annotation.Resource;
    9. @Service
    10. public class UserServiceImpl implements UserService {
    11. @Resource
    12. private RedisTemplate redisTemplate;
    13. @Override
    14. public void saveUserInfoToRedis(UserSaveDTO userSaveDTO) {
    15. UserVO user = new UserVO();
    16. BeanUtils.copyProperties(userSaveDTO,user);
    17. redisTemplate.opsForValue().set("user",user);
    18. }
    19. @Override
    20. public UserVO getUserInfoFromRedis() {
    21. return (UserVO)redisTemplate.opsForValue().get("user");
    22. }
    23. }

    、测试接口

    1.访问http://localhost:8888/saveUserInfoToRedis 保存redis

    2.访问http://localhost:8888/getUserInfoFromRedis 获取redis

  • 相关阅读:
    Git 版本控制工具
    自洽可分的哈密顿系统的辛算法
    [附源码]计算机毕业设计JAVA小区物业管理系统论文
    vscode package.json文件开头的{总是提升警告
    Linux Debian / Ubuntu安装v2rayA教程
    SpringCore完整学习教程5,入门级别
    洛谷-P1007-魔法少女
    【Java盲点攻克】「数值浮点数精度系列」Double与Float的坑与解决办法以及BigDecimal的取而代之!
    常用的实体类转换方式 - BeanUtil | MapStruct
    阶段总结与展望——我的简历
  • 原文地址:https://blog.csdn.net/qq_35160479/article/details/128041437