SpringBoot中配置redis数据库及常见错误解析——亲测通过

一前言:
在本章开始之前,确保自己的Redis是可以成功启动的,本篇使用的是Windows版本的Redis。
二SpringBoot中配置Redis

  • 1配置POM依赖
  • 2配置配置信息
  • 3封装常用操作
  • 4进行测试

1配置POM依赖:

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

2配置配置信息

#redis服务器地址
spring.redis.host=127.0.0.1
#redis服务器连接端口
spring.redis.port=6379
#redis服务器连接密码(默认为空)
spring.redis.password=
#redis数据库索引(默认为0(0-15)
spring.redis.database=0
#redis连接池最大连接数(使用负数则代表没有连接数没有限制)
spring.redis.jedis.pool.max-active=11
#redis连接池最大阻塞等待时间
spring.redis.jedis.pool.max-wait=-1
#redis连接池最大空闲连接
spring.redis.jedis.pool.max-idle=11
#redis连接池最小空闲连接
spring.redis.jedis.pool.min-idle=0
#连接超时时间(以毫秒为单位)
spring.redis.timeout=0

3封装常用操作

package com.example.xxx.DBTools;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
/*
封装Redis的常用操作
 */
@Component
public class RedisTools {

    @Autowired
    private RedisTemplate redisTemplate;

    /*
    数据插入
     */
    public boolean set(String key,String value){
        boolean result;
        try{
            redisTemplate.opsForValue().set(key,value);
            result=true;
        }
        catch (Exception e){
            result=false;
            e.printStackTrace();
        }
        return  result;
    }
    /*
    获取数据
     */
    public String get(String key){
        String result;
        try{
        result=redisTemplate.opsForValue().get(key).toString();
        }catch (Exception e){
            result="获取失败";
            e.printStackTrace();
        }
        return result;
    }
    /*
     删除数据
     */
    public boolean delete(String key){
        boolean result;
        try{
        redisTemplate.delete(key);
        result=true;
        }catch (Exception e){
            result=false;
            e.printStackTrace();
        }
        return result;
    }
}

4进行测试

package com.example.xxx.RedisTools;

import com.example.xxx.DBTools.RedisTools;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class RedisTest {
    @Autowired
    RedisTools redisTools;

    /*
    测试插入方法
     */
    @Test
    public void TestSet(){
        redisTools.set("k2","k2");
    }
}

执行之前:
当前Redis数据库中是没有任何数据的:
在这里插入图片描述
执行之后:
多了K1这条记录
在这里插入图片描述

注意点:在进行测试的时候,测试类的路径必须要与该封装工具类的包名一致,否则就会出现报错:
Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=…) with your test

连接超时时间设置过短会导致:
java.lang.IllegalStateException: Failed to load ApplicationContext

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot配置Redis可以通过以下步骤完成: 1. 在`pom.xml`文件添加Redis依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 2. 在`application.properties`(或`application.yml`)文件配置Redis连接信息: ```properties spring.redis.host=127.0.0.1 spring.redis.port=6379 ``` 你可以将上述配置根据你的实际情况进行修改,比如更改Redis主机地址和端口号等。 3. 创建一个Redis配置类,例如`RedisConfig.java`,并使用`@Configuration`注解标记。在该类,可以配置Redis连接池、序列化方式等。以下是一个示例: ```java @Configuration public class RedisConfig { @Value("${spring.redis.host}") private String host; @Value("${spring.redis.port}") private int port; @Bean public JedisConnectionFactory jedisConnectionFactory() { RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(host, port); return new JedisConnectionFactory(config); } @Bean public RedisTemplate<String, Object> redisTemplate() { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(jedisConnectionFactory()); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); return template; } } ``` 在上述示例,我们使用了Jedis作为Redis客户端,并且配置连接工厂和RedisTemplate。 现在,你可以在Spring Boot应用程序使用`@Autowired`注解将`RedisTemplate`注入到需要使用Redis的类,然后就可以使用Redis相关的操作了。例如: ```java @Autowired private RedisTemplate<String, Object> redisTemplate; public void saveToRedis(String key, Object value) { redisTemplate.opsForValue().set(key, value); } public Object getFromRedis(String key) { return redisTemplate.opsForValue().get(key); } ``` 这样配置后,你就可以在Spring Boot使用Redis了。记得启动你的Redis服务器,以确保能够成功连接和使用Redis数据库
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值