zoukankan      html  css  js  c++  java
  • SpringBoot使用Redis数据库

      (1)pom.xml文件引入jar包,如下:

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

      (2)application.properties文件中配置redis连接信息,如下:

    # Redis数据库索引(默认为0)
    spring.redis.database=0
    # Redis服务器地址
    spring.redis.host=172.31.19.222
    # Redis服务器连接端口
    spring.redis.port=6379
    # Redis服务器连接密码(默认为空)
    spring.redis.password=
    # 连接池最大连接数(使用负值表示没有限制)
    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=0

      (3)测试redis缓存

    package springboot.web;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class HelloController {
        
        @Autowired
        private StringRedisTemplate stringRedisTemplate;
    
        @RequestMapping("/redisHandler")
        public String redisHandler(){
            stringRedisTemplate.opsForValue().set("k5", "Springboot redis");
            return stringRedisTemplate.opsForValue().get("k5");
        }
    }

      (4)启动项目,调用reidsHandler方法,查询redis服务器信息,如下:

       

  • 相关阅读:
    Keras学习笔记——Hello Keras
    记一次线上事故的JVM内存学习
    postgresql中的search_path
    CentOS7安装setuptools
    CentOS7安装EPEL的两种方式
    Ncures库的介绍与安装
    CentOs6.5 安装Zlib
    Centos 安装zlib
    Windows如何压缩tar.gz格式
    nginx运行文件出错env: /etc/init.d/nginx: No such file or directory
  • 原文地址:https://www.cnblogs.com/gdpuzxs/p/7224944.html
Copyright © 2011-2022 走看看