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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值