SpringBoot整合spring data Redis

SpringBoot整合spring data Redis

在这里插入图片描述
redis-sb-demo02:pom.xml中添加redis坐标

<!-- 配置使用redis启动器 -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

配置信息

########################################################
###REDIS (RedisProperties) redis基本配置;
########################################################
# database name
spring.redis.database=0
# server host1
spring.redis.host=127.0.0.1
# server password
#spring.redis.password=
#connection port
spring.redis.port=6379
# pool settings ...
spring.redis.jedis.pool.max-idle=8
spring.redis.jedis.pool.min-idle=0
spring.redis.jedis.pool.max-active=8
spring.redis.jedis.pool.max-wait=-1
# name of Redis server
#spring.redis.sentinel.master=
# comma-separated list of host:port pairs
#spring.redis.sentinel.nodes=

当SpringBoot整合SpringDataRedis之后,我们使用的对象的是RedisTemplate
在这里插入图片描述
第一步:pom

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 配置使用redis启动器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

第二步:创建UserController类
在这里插入图片描述

package com.youpingou.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    //只需要导入springboot-data-redis的启动器,就可以使用当前这个目标,所有的初始化都由Spring帮你完成
    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @GetMapping
    public ResponseEntity<Object> testReids(){
        //存值
        stringRedisTemplate.opsForValue().set("rt","SpringBoot整合SpringDataRedis案例");
        //取值
        String school = stringRedisTemplate.opsForValue().get("school");
        System.out.println(school);
        return ResponseEntity.ok(school);
    }


}

第三步:启动测试,ok

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中,可以通过配置文件或编程方式来设置Redis线程池。以下是通过编程方式实现整合Redis并设置Redis线程池的步骤: 1. 首先,在你的Spring Boot项目中,添加对Redis和Lettuce(Redis的Java客户端)的依赖。可以在项目的pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> </dependency> ``` 2. 在application.properties或application.yml配置文件中添加Redis相关配置,包括主机名、端口号、密码等。例如: ```yaml spring.redis.host=localhost spring.redis.port=6379 spring.redis.password= ``` 3. 创建Redis连接工厂类,可以使用LettuceConnectionFactory。在该类中,你可以设置与Redis连接相关的属性,包括连接池配置。下面是一个示例: ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisStandaloneConfiguration; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; @Configuration public class RedisConfig { @Value("${spring.redis.host}") private String redisHost; @Value("${spring.redis.port}") private int redisPort; @Value("${spring.redis.password}") private String redisPassword; @Bean public LettuceConnectionFactory redisConnectionFactory() { RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(redisHost, redisPort); config.setPassword(redisPassword); return new LettuceConnectionFactory(config); } @Bean public RedisTemplate<String, Object> redisTemplate() { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory()); return redisTemplate; } } ``` 在上述示例中,LettuceConnectionFactory被创建为一个Bean,并通过RedisStandaloneConfiguration设置了连接的主机名、端口号和密码。然后,将该连接工厂设置到RedisTemplate中。 4. 在需要使用Redis的地方,注入RedisTemplate,并调用其相关方法来操作Redis。例如: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; @Service public class MyService { private final RedisTemplate<String, Object> redisTemplate; @Autowired public MyService(RedisTemplate<String, Object> redisTemplate) { this.redisTemplate = redisTemplate; } public void saveData(String key, Object value) { redisTemplate.opsForValue().set(key, value); } public Object getData(String key) { return redisTemplate.opsForValue().get(key); } } ``` 上述示例中的MyService类注入了RedisTemplate,并通过redisTemplate实现了保存数据和获取数据的方法。 这样,你就可以使用Spring Boot整合Redis并设置Redis线程池了。注意,在实际应用中,你可能还需要做一些其他配置和异常处理。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值