资讯 小学 初中 高中 语言 会计职称 学历提升 法考 计算机考试 医护考试 建工考试 教育百科
栏目分类:
子分类:
返回
空麓网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
空麓网 > 计算机考试 > 软件开发 > 后端开发 > Java

springboot整合redis

Java 更新时间: 发布时间: 计算机考试归档 最新发布

springboot整合redis

springboot整合redis 一、依赖
 
            org.springframework.boot
            spring-boot-starter-data-redis
        

 
            redis.clients
            jedis
            3.2.0
        
 
        com.alibaba
        fastjson
        1.2.70
    
二、配置
server:
  port: 9090
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/springboot-vue?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8
    username: root
    password: password
    oracleucp:
      initial-pool-size: 5
  redis:
    # redis数据库索引(默认为0),我们使用索引为3的数据库,避免和其他数据库冲突
    database: 3
    # redis服务器地址(默认为loaclhost)
    host: 192.168.43.55
    # redis端口(默认为6379)
    port: 6379
    # redis访问密码(默认为空)
    password: 123456
    # redis连接超时时间(单位毫秒)
    timeout: 0
    # redis连接池配置
    pool:
      # 最大可用连接数(默认为8,负数表示无限)
      max-active: 8
      # 最大空闲连接数(默认为8,负数表示无限)
      max-idle: 8
      # 最小空闲连接数(默认为0,该值只有为正数才有用)
      min-idle: 0
      # 从连接池中获取连接最大等待时间(默认为-1,单位为毫秒,负数表示无限)
      max-wait: -1
三、配置类
  • 教程有,实测不需要
package com.redis.test.config;

import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
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;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@EnableCaching
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate template = new RedisTemplate<>();

        // 实例化序列化器 FastJsonRedisSerializer
        FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);

        // value 的序列化采用 FastJsonRedisSerializer [包括 String 类型和 Hash 类型]
        template.setValueSerializer(fastJsonRedisSerializer);
        template.setHashValueSerializer(fastJsonRedisSerializer);

        // 全局开启 AutoType,这里方便开发,使用全局的方式,即允许所有包的序列化和反序列化,官方不推荐使用
        ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
        // 建议使用这种方式,小范围指定白名单,即指定小范围包的序列化和反序列化
        // ParserConfig.getGlobalInstance().addAccept("club.wadreamer.demo.entity");

        // key 的序列化采用StringRedisSerializer
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());

        template.setConnectionFactory(redisConnectionFactory);

        return template;
    }
}


 
四、测试(重点) 
package com.redis.test.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RedisTestController {
     //测试后,注销掉配置类也能运行
     //注意,此时为**String**RedisTemplate
       @Autowired
       private StringRedisTemplate redisTemplate;
      //@Resource
     // private RedisTemplate redisTemplate;

    @GetMapping("/redisTest")
    public  String redisTest(){
        redisTemplate.opsForValue().set("name","J.J");
        String name = (String) redisTemplate.opsForValue().get("name");
        return name;
    }
}
  • 页面成功显示

    blog.csdnimg.cn/6b77c6cba04545f1bcdddb86aa43225d.png#pic_center)
转载请注明:文章转载自 http://www.konglu.com/
免责声明:

我们致力于保护作者版权,注重分享,被刊用文章【springboot整合redis】因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理,本文部分文字与图片资源来自于网络,转载此文是出于传递更多信息之目的,若有来源标注错误或侵犯了您的合法权益,请立即通知我们,情况属实,我们会第一时间予以删除,并同时向您表示歉意,谢谢!

我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2023 成都空麓科技有限公司

ICP备案号:蜀ICP备2023000828号-2