SpringBoot&Redis整合

x33g5p2x  于2021-03-14 发布在 Spring  
字(3.1k)|赞(0)|评价(0)|浏览(363)

SpringBoot整合Redis,主要用到如下两个类:

RedisConnectionFactory:建立与Redis服务器的连接,提供Redis服务器一些必要的参数配置;
RedisTemplate:封装了RedisConnectionFactory作为自身的属性,为创建操作Redis的工具类提供服务;

1. 创建一个SpringBoot项目,目录结构如下

2. 添加如下配置
pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-redis</artifactId>
    <version>1.4.7.RELEASE</version>
</dependency>

3. Redis的参数配置
resources/application.properties


# 就是给RedisConnectionFactory的属性进行赋值

#Redis数据库索引(默认为0)
spring.redis.database=0
#Redis服务器地址
spring.redis.host=127.0.0.1
#Redis服务器连接端口
spring.redis.port=6379
#Redis服务器连接密码(默认为空),如果你的Redis没有设置用户名和密码,请将其注释掉
#spring.redis.user=root 
#spring.redis.password=123456
#连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
#连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
#连接池中的最大空闲连接
spring.redis.pool.max-idle=8
#连接池中的最小空闲连接
spring.redis.pool.min-idle=0
#连接超时时间(毫秒)
spring.redis.timeout=3000

4. 编写配置类
com/example/redis/config/RedisConfig.java

package com.example.redis.config;

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;

@Configuration // 标记该类为一个IoC容器
public class RedisConfig {
    /**
     * 将RedisTemplate注入到IoC容器中
     * @Bean注解将方法的返回值注入到IoC容器中,返回值作为<bean class="返回值"/>
     *  方法名作为<bean id="方法名"/>
     * @param factory
     * @return
     */
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);
        return redisTemplate;
    }
}

5. 编写工具类
com/example/redis/util/RedisUtil.java

package com.example.redis.util;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component // 标注该类为Bean,供其他类使用
public class RedisUtil {
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 增
     * @param key
     * @param object
     * @return
     */
    public boolean set(String key, Object object) {
        ValueOperations<String, Object> vo = redisTemplate.opsForValue();
        vo.set(key, object);
        return true;
    }

    /**
     * 取
     * @param key
     * @return
     */
    public Object get(String key) {
        ValueOperations<String, Object> vo = redisTemplate.opsForValue();
        return vo.get(key);
    }
}

6. 将Controller层作为测试类
com/example/redis/controller/TestController.java

package com.example.redis.controller;

import com.example.redis.util.RedisUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
public class TestController {

    @Resource
    private RedisUtil redisUtil;

    @RequestMapping(value = "/redis")
    @ResponseBody // 该注解可以将方法的返回值传送到页面上
    public String getReisValue() {
        redisUtil.set("key", "66666666666");
        return redisUtil.get("key").toString();
    }
}

7. 进行测试
先开启Redis服务器,再运行启动类启动SpringBoot项目,然后访问:http://localhost:8080/redis 页面显示如下:

成功输出,整合成功!

相关文章

微信公众号

最新文章

更多