Springboot项目中对Redis的使用

1.Redis依赖安装

在pom.xml文件中添加Springboot的Redis依赖;

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

spring-boot-starter-data-redis是Springboot的Redis启动器,它会自动配置好Redis所需的基本组件。

2.Redis配置连接信息

在application.yml 或 application.properties文件中配置Redis的连接信息;
(1)application.yml配置文件格式

#   Redis配置
    redis:
      host: localhost(或192.168.2.XX)  #Redis服务器地址(电脑ip)
      port: 6379  #Redis服务器端口
      database: 0 #Redis服务器密码(默认为0)
      password:   #Redis服务器密码(如果有的话)
      timeout: 10s  #连接超时时间
      lettuce:
        pool:
          max-active: 8  #连接池最大连接数(使用负值表示没有限制)
          max-wait: -1ms  #连接池最大阻塞等待时间(使用负值表示没有限制)
          max-idle: 8  #连接池中的最大空闲连接
          min-idle: 0  #连接池中最小空闲连接

(2)application.properties配置文件格式

spring.redis.host=localhost
spring.redis.port=6379
spring.redis.database=0
spring.redis.password=yourpassword
spring.redis.jedis.pool.max-active=8
spring.redis.jedis.pool.max-wait=-1ms
spring.redis.jedis.pool.max-idle=8
spring.redis.jedis.pool.min-idle=0

3.Redis配置文件

RedisConfig.java文件

package com.example.mybatis_dev.util;

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.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        return redisTemplate;
    }
}

4.测试配置

在使用Redis类中直接导入Redis来验证Redis配置是否正确;

package com.example.mybatis_dev.controller;

import com.example.mybatis_dev.entity.User;
import com.example.mybatis_dev.service.UserService;
import com.example.mybatis_dev.util.R;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * ClassName:UserController
 * Package:IntelliJ IDEA
 *
 * @Create 2024/1/13 23:05
 */
@RestController
@RequestMapping("/user")
public class UserController {
    private final Logger logger= LoggerFactory.getLogger(UserController.class);

    @Autowired
    UserService userService;
//    导入Redis
    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    /**
     * 查询所有用户
     * @return
     */
    @PostMapping("/selectAllUser")
    public R selectAllUser(){
//        List<User> users = userService.selectAllUser();
        List<User> userList=userService.selectAllUser();
        if(userList.size()==0){
            return R.fail("没有数据");
        }
        else{
        //使用Redis中的方法
            redisTemplate.opsForValue().set("1",userList);
            System.out.println(redisTemplate.opsForValue().get("1"));
            return R.ok(userList);
        }
    }
}
  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值