• Redis(二) Java操作Redis


    一. Linux连接上Redis

    在这里插入图片描述

    二.用Redis客户端测试

    即Redis Desktop Manager 进行连接,连接步骤如下

    在这里插入图片描述
    验证处填写你的密码即可。

    三.建立一个SpringBoot工程

    3.1 导入依赖

    		<dependency>           
    			<groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    3.2 实体类

    Book

    package com.huang.demo.demo.model;
    
    import java.util.Date;
    
    public class Book {
        private Integer id;
        private String name;
        private String author;
    
        private Date publishDate;
    
        @Override
        public String toString() {
            return "Book{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", author='" + author + '\'' +
                    ", publishDate=" + publishDate +
                    '}';
        }
    
        public Book(Integer id, String name, String author, Date publishDate) {
            this.id = id;
            this.name = name;
            this.author = author;
            this.publishDate = publishDate;
        }
    
        public Date getPublishDate() {
            return publishDate;
        }
    
        public void setPublishDate(Date publishDate) {
            this.publishDate = publishDate;
        }
    
        public Book() {
        }
    
        public Book(Integer id, String name, String author) {
            this.id = id;
            this.name = name;
            this.author = author;
        }
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getAuthor() {
            return author;
        }
    
        public void setAuthor(String author) {
            this.author = author;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70

    3.3 重点部分

    3.3.1

        @Test
        void test02() {
            ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
            ops.set("k3", "v3");
            String k3 = ops.get("k3");
            System.out.println("k3 = " + k3);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述

    3.3.2

        @Test
        void test03() {
            ValueOperations ops = redisTemplate.opsForValue();
            ops.set("b1", new Book(1, "三国演义3", "罗贯中"));
            Book b1 = (Book) ops.get("b1");
            System.out.println("b1 = " + b1);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    linux上
    在这里插入图片描述

    3.3.3

    @Test
        void test01() {
            //序列化
            redisTemplate.setKeySerializer(RedisSerializer.string());
            ValueOperations op = redisTemplate.opsForValue();
            op.set("k2", "v2");
            Object k2 = op.get("k2");
            System.out.println("k2 = " + k2);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在这里插入图片描述

    3.3.4

       @Test
        void contextLoad2() {
            ValueOperations ops = redisTemplate.opsForValue();
            ops.set("k1", "v1");
            Object k1 = ops.get("k1");
            System.out.println("k1 = " + k1);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述

    存对象

    3.3.5

     @Test
        void test03() {
            ValueOperations ops = redisTemplate.opsForValue();
            ops.set("b1", new Book(1, "三国演义3", "罗贯中"));
            Book b1 = (Book) ops.get("b1");
            System.out.println("b1 = " + b1);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述

    3.3.6 存 json对象

        @Test
        void test05() {
            redisTemplate.setKeySerializer(RedisSerializer.string());
            redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
            ValueOperations ops = redisTemplate.opsForValue();
            //存的时候,系统会自动将 book 对象转为 JSON 字符串存到 Redis 中
            ops.set("b2", new Book(99, "红楼梦5", "曹雪芹"));
            //从 Redis 中读取的时候,读到的也是 JSON 字符串,会自动将 JSON 转为一个 Book 对象
            Book b2 = (Book) ops.get("b2");
            System.out.println("b2 = " + b2);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述

    3.3.7 存带时间类型的json对象

      @Test
        void contextLoads() {
            redisTemplate.setKeySerializer(RedisSerializer.string());
            ObjectMapper om = new ObjectMapper();
            om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
            om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
            redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer(om));
    
            ValueOperations ops = redisTemplate.opsForValue();
            ops.set("b2",new Book(9,"红楼梦","曹雪芹",new Date()));
            Book b2 = (Book) ops.get("b2");
            System.out.println("b2 = " + b2);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在这里插入图片描述

    四.封装一个带时间参数的json

    封装原因:由于要多次大量的使用到json对象,(带不带时间类型多加几行代码就行),但是由上面的几个案例可以得出结论是,前面的几行都是重复的,故此我们把多余重复的代码抽出来封装起来

    4.1 先来看看底层redisautoconfiuration

    按两下shift 输入搜索即可

    在这里插入图片描述

    代码主题,把中间这一段抽取处来
    在这里插入图片描述

    在这里插入图片描述
    这一段抽取出来,放入我们的工具类RedisConfig中
    在这里插入图片描述

    执行封装的步骤:
    在这里插入图片描述

    4.2 封装完成后

    Redisconfig

    package com.huang.demo.demo.config;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
    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.GenericJackson2JsonRedisSerializer;
    import org.springframework.data.redis.serializer.RedisSerializer;
    
    import java.text.SimpleDateFormat;
    
    @Configuration
    public class RedisConfig {
        @Bean
        public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
            RedisTemplate<Object, Object> template = new RedisTemplate();
            //为时间类型加的代码段,转化为SimpleDateFormat格式
            ObjectMapper om = new ObjectMapper();
            om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
            om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
            template.setConnectionFactory(redisConnectionFactory);
            template.setKeySerializer(RedisSerializer.string());
            template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
            return template;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    Test

    //   封装之后
    
        @Test
        void  test1(){
              ValueOperations ops = redisTemplate.opsForValue();
            //存的时候,系统会自动将 book 对象转为 JSON 字符串存到 Redis 中
            ops.set("b12", new Book(99, "红楼梦", "曹雪芹",new Date()));
            //从 Redis 中读取的时候,读到的也是 JSON 字符串,会自动将 JSON 转为一个 Book 对象
            Book b2 = (Book) ops.get("b12");
            System.out.println("b12 = " + b2);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述

  • 相关阅读:
    基于PHP的毕业设计管理系统设计与实现
    材料科学基础学习指导-吕宇鹏-名词和术语解释-第1章:晶体结构
    【3. 操作系统—物理内存管理】
    基于邻接矩阵的克鲁斯卡尔算法和普利姆算法
    Mybatis核心配置文件中的常用标签
    LeetCode--HOT100题(48)
    Spring Webflux 后端处理前端请求的 4 种方式
    #力扣:2894. 分类求和并作差@FDDLC
    ARM cortex-A7核UART实验 收发数据
    深度学习_1_基本语法
  • 原文地址:https://blog.csdn.net/weixin_43189971/article/details/126165101