009-SpringBoot整合Redis

该博客介绍了如何在SpringBoot项目中集成Redis作为缓存,通过配置Redis连接参数,创建RedisTemplate,编写Controller和服务层实现数据的存取操作。详细步骤包括引入依赖、配置Redis、创建POJO、配置RedisTemplate、编写Controller和Service,以及测试用例。
摘要由CSDN通过智能技术生成

1、案例思路

完善根据学生 id 查询学生的功能,先从 redis 缓存中查找,如果找不到,再从数据库中查找,然后放到 redis 缓存中

2、创建module

013-springboot-redis

在这里插入图片描述

3、代码编写

3.1 引入redis依赖

 <!--springboot集成redis的起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>


<!--lombok的依赖-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
            <scope>provided</scope>
        </dependency>

3.2 核心配置文件中配置redis

#配置内嵌tomcat端口号
server.port=8013
server.servlet.context-path=/

#配置redis
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=192.168.126.146
spring.redis.username=root
#spring.redis.password=root
# Redis服务器连接端口
spring.redis.port=6379
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=5000


3.3 编写pojo

package com.zzy.springboot.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import java.io.Serializable;

@Data
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
public class Student implements Serializable {
    private Integer id;
    private String name;
    private Integer age;
}

3.4 Redis的配置类

package com.zzy.springboot.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<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(connectionFactory);

        // 使用Jackson2JsonRedisSerialize替换默认序列化
        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和value的序列化规则
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.afterPropertiesSet();

        return redisTemplate;
    }
}

3.5 编写controller

package com.zzy.springboot.web;

import com.zzy.springboot.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class StudentController {

    //注入service
    @Autowired
    private StudentService studentService;

    //想redis中存储数据
    @RequestMapping(value = "/put")
    public @ResponseBody Object put(String key,String value){
        studentService.put(key,value);
        return "值已成功传入redis";
    }

    //获取redis中的数据
    @RequestMapping(value = "/get")
    public @ResponseBody Object get(String key){
        String value = studentService.get(key);
        return "数据c的值为"+value;
    }

}

3.6 编写service 以及impl

package com.zzy.springboot.service;

public interface StudentService {

    void put(String key, String value);

    String get(String c);
}


package com.zzy.springboot.service.impl;

import com.zzy.springboot.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class StudentServiceImpl implements StudentService {
    //注入redis模板对象
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;


    @Override
    public void put(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
    }

    @Override
    public String get(String key) {
        return (String) redisTemplate.opsForValue().get(key);
    }
}

3.7 运行启动类

在这里插入图片描述

3.8 访问测试

  • 存储值
    http://localhost:8013/put?key=a&value=tom

在这里插入图片描述

  • 获取redis中的数据
    http://localhost:8013/get?key=a

在这里插入图片描述

3.9 可视化查看redis数据

  • 进入redis

在这里插入图片描述

  • 启动redis

在这里插入图片描述

  • 进入redis客户端

在这里插入图片描述

  • 获取所有的键

在这里插入图片描述

  • 获取键a对应的value值

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值