SpringBoot2.7.4整合Redis

目录

一、添加maven依赖

二、添加配置项

三、新增配置类

四、编辑实体类

五、编写接口

六、编写业务层

1.编写service层

2.编写service实现层 

七、测试接口


一、添加maven依赖


   org.springframework.boot
   spring-boot-starter-data-redis


   org.projectlombok
   lombok
   1.16.10

SpringBoot2.7.4整合Redis_第1张图片

、添加配置项

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

SpringBoot2.7.4整合Redis_第2张图片

、新增配置类

SpringBoot2.7.4整合Redis_第3张图片

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;
    }
}

、编辑实体类

SpringBoot2.7.4整合Redis_第4张图片

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;
}

、编写接口

SpringBoot2.7.4整合Redis_第5张图片

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层

SpringBoot2.7.4整合Redis_第6张图片

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实现层 

SpringBoot2.7.4整合Redis_第7张图片

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

SpringBoot2.7.4整合Redis_第8张图片

2.访问http://localhost:8888/getUserInfoFromRedis 获取redisSpringBoot2.7.4整合Redis_第9张图片

你可能感兴趣的:(技术栈,redis,数据库,java)