栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot整合spring data Redis(掌握)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot整合spring data Redis(掌握)

SpringBoot整合spring data Redis(掌握)

  • redis-sb-demo02:pom.xml中添加redis坐标

    
    	org.springframework.boot
    	spring-boot-starter-data-redis
    
    
  • 配置信息

    ########################################################
    ###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


        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.boot
            spring-boot-starter-data-redis
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
    

第二步:创建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 testReids(){
        //存值
        stringRedisTemplate.opsForValue().set("rt","SpringBoot整合SpringDataRedis案例");
        //取值
        String school = stringRedisTemplate.opsForValue().get("school");
        System.out.println(school);
        return ResponseEntity.ok(school);
    }


}

 

第三步:启动测试,ok

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号