SpringBoot---------整合Redis

目录

第一步:引入依赖

第二步:配置Redis信息

[第三步:选择Spring Data Redis进行操作Redis数据库](#第三步:选择Spring Data Redis进行操作Redis数据库)

①操作String类型数据(用的少)

②操作Object类型数据(重要!!!)

③操作Hash,Set,List类型数据


第一步:引入依赖

XML 复制代码
        <!--SpringDataRedis-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

        <!--common-pool-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>

        <!--Jackson依赖-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>

第二步:配置Redis信息

XML 复制代码
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://192.168.136.132/mydb
    username: root
    password: 1234
    type: com.alibaba.druid.pool.DruidDataSource
    
    
  redis:
    database: 0
    host: 192.168.136.132
    port: 6379
    password: pz030812...
    lettuce:
      pool:
        max-active: 8
        max-idle: 8
        min-idle: 0
        max-wait: 100ms

第三步:选择Spring Data Redis进行操作Redis数据库

①操作String类型数据(用的少)

java 复制代码
@Autowired
private StringRedisTemplate stringRedisTemplate;

@GetMapping("/redismessge")
    public JsonResult Redismsg(){
        //操作String数据

        //增,改
        stringRedisTemplate.opsForValue().set("jxxy","计科一班");
        //查
        String jxxy = stringRedisTemplate.opsForValue().get("jxxy");
        System.out.println("jxxy = " + jxxy);
        //删
        stringRedisTemplate.opsForValue().getAndDelete("jxxy");
        return new JsonResult();
    }

②操作Object类型数据(重要!!!)

java 复制代码
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RedisTemplate redisTemplate;

    //序列化工具
    private static final ObjectMapper mapper= new ObjectMapper();

    @PostMapping("/redismessge_1")
    public JsonResult Redismsg_1(@RequestBody Student student) throws JsonProcessingException {

        //使用自动序列化
        redisTemplate.opsForValue().set("jxxy_01",student);

        Object jxxy_01 = redisTemplate.opsForValue().get("jxxy_01");
        System.out.println("jxxy_01 = " + jxxy_01);


        //手动序列化
        String json = mapper.writeValueAsString(student);
        stringRedisTemplate.opsForValue().set("jxxy_02",json);

        json = stringRedisTemplate.opsForValue().get("jxxy_02");
        Student jxxy_02 = mapper.readValue(json, Student.class);
        System.out.println("jxxy_02 = " + jxxy_02);


        return new JsonResult();
    }

postman发送请求:

idea输出数据:

注:自动序列化和手动序列化的区别:如果单在java客户端看的话,二者并无明显区别,但是从Redis数据库中看的话就会发现在自动序列化的方式在redis数据库中每个数据前面都会增加一串地址,而手动的则是清晰的数据;而且使用Redistemplate时,对象类要加 implements Serializable


先看自动序列化redis的存储情况:

再看手动序列化redis的存储情况:

③操作Hash,Set,List类型数据

这几类数据用的比较少,可以去Redis基础讲解中查看,这里就不做代码演示

学习路线:

基础框架SSM-----------spring篇

SpringBoot---------Lombook

SpringBoot---------@Value,@ConfigurationProperyies以及多环境开发配置

SpringBoot---------整合Junit

SpringBoot---------整合Mybatis

SpringBoot---------整合Mybatisplus

SpringBoot---------整合Redis

相关推荐
卷无止境8 分钟前
智能体开发环境ADE浅析,编程工具的下一次范式跃迁
后端·python
IT_Octopus2 小时前
IntelliJ 本地日志路径自定义:`-DLOG_PATH=./logs` 为什么总“不听话“
java·log4j·intellij-idea
码事漫谈3 小时前
DeepSeek 这波操作很凶
后端
长征coder3 小时前
【无标题】
java·性能优化
新时代牛马4 小时前
PCI与PCIe 硬件原理、配置空间/BAR 与 Linux 驱动完整篇:从 LTSSM、TLP 到 ECAM 与probe
java·linux·服务器
Bs_MoneyMagnet4 小时前
基于springboot+vue的医疗健康便民服务平台的设计与实现 源码+文档
java·vue.js·spring boot·后端·spring
sunshine22 girl5 小时前
Java学习一 环境配置2 Idea的入门使用和maven配置,项目启动脚本配置
java·maven
彧azz5 小时前
Redis 全面指南:从核心概念到高可用架构实战
数据库·redis·架构
有点。5 小时前
*C++哈夫曼树与哈夫曼编码
java·c++·servlet
子兮曰5 小时前
Bun v1.4.1 深度解析:从 Zig 到 Rust,一场 11 天、64 个 AI 代理的语言迁徙
前端·后端·bun