springboot整合redis并在项目中使用

推荐文章:
redis学习
springboot+mybatis+vue实战——旅游网站(一) 完成登录注册、验证码、省份增删改查功能


一、下载安装并启动redis

看这篇博客的第二点:https://blog.csdn.net/m0_45234510/article/details/106504401

注意:启动后不要关闭,启动项目时需要保持redis一直打开

二、springboot项目整合redis

1、pom.xml引入redis依赖

<!--redis-->
<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2、在application.properties中加入配置

spring.redis.host=localhost
spring.redis.port=6379
spring.redis.database=0

3、新建RedisCache.java

在这里插入图片描述

  • RedisCache.java
package com.travel.travels.cache;

import com.travel.travels.utils.ApplicationContextUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.cache.Cache;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Slf4j
public class RedisCache implements Cache {

    private String id;

    public RedisCache(String id){
        log.info("当前的缓存id: [{}]",id);
        this.id = id;
    }

    @Override
    public String getId() {
        return this.id;
    }

    @Override //放入redis缓存
    public void putObject(Object key, Object value) {
        log.info("放入缓存的key:[{}] 放入缓存的valus:[{}]",key,value);
        getRedisTemplate().opsForHash().put(id,key.toString(),value);
    }

    @Override//从redis缓存获取
    public Object getObject(Object key) {
        log.info("取出的缓存的key:[{}] ",key.toString());
        return getRedisTemplate().opsForHash().get(id,key.toString());
    }

    @Override//删除指定缓存
    public Object removeObject(Object o) {
        return null;
    }

    @Override  //清除缓存
    public void clear() {
        log.info("清除所有缓存信息...");
        getRedisTemplate().delete(id);
    }

    @Override
    public int getSize() {
        return getRedisTemplate().opsForHash().size(id).intValue();
    }

    //封装获取redistemplate的方法
    public RedisTemplate getRedisTemplate(){
        RedisTemplate redisTemplate = (RedisTemplate) ApplicationContextUtils.getBean("redisTemplate");
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        return redisTemplate;
    }
}

4、新建工具类ApplicationContextUtils.java

在这里插入图片描述

  • ApplicationContextUtils.java
package com.travel.travels.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component // //注意:需要添加这个注解
public class ApplicationContextUtils implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public static Object getBean(String name){
        return applicationContext.getBean(name);
    }
}

5、在实体类中实现Serializable接口

implements Serializable

在这里插入图片描述

6、在对应的Mapper.xml中绑定redis

   <cache type="com.travel.travels.cache.RedisCache"/>

在这里插入图片描述

7、启动项目

注意:需要提前打开redis!
在这里插入图片描述
在这里插入图片描述在这里插入图片描述


推荐文章:
redis学习
springboot+mybatis+vue实战——旅游网站(一) 完成登录注册、验证码、省份增删改查功能

  • 2
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
您好,关于您的问题,可以按照以下步骤来实现springboot整合Redis在idea的操作: 1. 在pom.xml文件添加Redis依赖 ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 2. 在application.properties添加Redis配置 ``` spring.redis.host=127.0.0.1 spring.redis.port=6379 ``` 3. 创建一个Redis配置类 ``` @Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate( RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); // 使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值 Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); serializer.setObjectMapper(objectMapper); // 设置value的序列化规则和 key的序列化规则 redisTemplate.setValueSerializer(serializer); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.afterPropertiesSet(); return redisTemplate; } } ``` 4. 在需要使用Redis的地方注入RedisTemplate,然后即可使用Redis的API进行操作。 ``` @Service public class UserServiceImpl implements UserService { @Autowired private RedisTemplate<String, Object> redisTemplate; @Override public User getUserById(Long id) { String key = "user_" + id; User user = (User) redisTemplate.opsForValue().get(key); if (user == null) { user = userRepository.selectById(id); if (user != null) { redisTemplate.opsForValue().set(key, user, 1, TimeUnit.MINUTES); // 缓存一分钟 } } return user; } } ``` 希望以上步骤可以帮到您,如果还有其他问题,请随时提出。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值