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

相关推荐
敲代码的嘎仔19 分钟前
从零搭建微服务:Spring Cloud Alibaba 全家桶踩坑实录(Nacos + OpenFeign + Gateway + Sentinel)
java·spring boot·spring·spring cloud·微服务·gateway·sentinel
程序员爱钓鱼29 分钟前
Rust 所有权 Ownership 详解:理解内存安全的核心机制
前端·后端·rust
JaguarJack37 分钟前
PHP 引用计数机制深度解析
后端·php·服务端
一条泥憨鱼1 小时前
苍穹外卖【day7| 对购物车进行操作】
java·后端·mysql·苍穹外卖
陈随易9 小时前
FFmpeg 9.0 发布,代号 Lei,音视频处理再升级
前端·后端·程序员
IT界的老黄牛11 小时前
Spring Boot 的 `/actuator/health` 不是免费的:健康检查打爆连接池的反面教材
spring boot·hikaricp·actuator·健康检查·线上排障·架构反模式
凌虚12 小时前
面向 MySQL 用户的 PostgreSQL 快速上手指南
数据库·后端·架构
都叫我大帅哥13 小时前
Java JJWT 完全指南:从入门到生产级实践
java
IT_陈寒14 小时前
Vite静态资源引用这个坑我踩得有点疼
前端·人工智能·后端
前端开发张小七14 小时前
Java 学习笔记 · 第一课:基础语法与核心概念
java·后端