• SpringBoot整合Redis


    1、创建springboot工程

    在这里插入图片描述

    2、添加依赖

    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    	<modelVersion>4.0.0modelVersion>
    	<parent>
    		<groupId>org.springframework.bootgroupId>
    		<artifactId>spring-boot-starter-parentartifactId>
    		<version>3.0.5version>
    		<relativePath/> 
    	parent>
    
      
      
    	<groupId>com.atguigugroupId>
    	<artifactId>springboot-redisartifactId>
    	<version>0.0.1-SNAPSHOTversion>
    	<name>springboot-redisname>
    	<description>springboot-redisdescription>
    	<properties>
    		<java.version>17java.version>
    	properties>
    	<dependencies>
    		<dependency>
    			<groupId>org.springframework.bootgroupId>
    			<artifactId>spring-boot-starterartifactId>
    		dependency>
    
    		<dependency>
    			<groupId>org.springframework.bootgroupId>
    			<artifactId>spring-boot-starter-webartifactId>
    		dependency>
    
    		<dependency>
    			<groupId>org.springframework.bootgroupId>
    			<artifactId>spring-boot-starter-testartifactId>
    		dependency>
    
    		<dependency>
    			<groupId>org.springframework.bootgroupId>
    			<artifactId>spring-boot-starter-data-redisartifactId>
    		dependency>
    
    	dependencies>
    
    	<build>
    		<plugins>
    			<plugin>
    				<groupId>org.springframework.bootgroupId>
    				<artifactId>spring-boot-maven-pluginartifactId>
    			plugin>
    		plugins>
    	build>
    
    project>
    
    

    3、创建配置文件

    spring:
      data:
        redis:
          host: 192.168.74.148
          port: 6379
    
    

    4、创建启动类

    package com.atguigu.springboot.redis;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    
    // Generated by https://start.springboot.io
    // 优质的 spring/boot/data/security/cloud 框架中文文档尽在 => https://springdoc.cn
    @SpringBootApplication
    public class SpringbootRedisApplication {
    
    	public static void main(String[] args) {
    		SpringApplication.run(SpringbootRedisApplication.class, args);
    	}
    
    }
    
    

    5、创建测试类,编写测试方法

    package com.atguigu.springboot.redis;
    
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.data.redis.core.RedisTemplate;
    
    @SpringBootTest
    class SpringbootRedisApplicationTests {
    	@Autowired
    	private RedisTemplate redisTemplate;
    
    	@Test
    	void contextLoads() {
    		redisTemplate.opsForValue().set("hello", "world");
    	}
    
    }
    
    
    127.0.0.1:6379> select 0
    OK
    127.0.0.1:6379> keys *
    1) "\xac\xed\x00\x05t\x00\x05hello"
    127.0.0.1:6379> get "\xac\xed\x00\x05t\x00\x05hello"
    "\xac\xed\x00\x05t\x00\x05world"
    127.0.0.1:6379> 
    

    在这里插入图片描述

    6、序列化定制

    package com.atguigu.springboot.redis;
    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;
    @Configuration
    public class AppRedisConfiguration {
    
        @Bean
        public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
            RedisTemplate<Object, Object> template = new RedisTemplate<>();
            template.setConnectionFactory(redisConnectionFactory);
            //把对象转为json字符串的序列化工具
            template.setDefaultSerializer(new GenericJackson2JsonRedisSerializer());
            return template;
        }
    }
    
    

    此时再执行一下测试类SpringbootRedisApplicationTests,乱码问题就得到解决

    127.0.0.1:6379> keys *
    1) "\xac\xed\x00\x05t\x00\x05hello"
    2) "\"hello\""
    127.0.0.1:6379> get "\"hello\""
    "\"world\""
    127.0.0.1:6379> 
    

    在这里插入图片描述

  • 相关阅读:
    list解析<stl初级> (跑路人笔记)
    Qt定时器
    Python 引用问题 - ImportError: attempted relative import with no known parent package
    自然语言处理(六):词的相似性和类比任务
    A搜索算法简介
    手把手教你用代码画架构图
    拥抱AI-ChatGPT:人类新纪元
    使用API有效率地管理Dynadot域名,自查账户信息
    计算机毕业设计Python+Django农产品推荐系统 农产品爬虫 农产品商城 农产品大数据 农产品数据分析可视化 PySpark Hadoop Hive
    128【Java学习笔记(一百二十八)】之Object类和包装类
  • 原文地址:https://blog.csdn.net/m0_65152767/article/details/139863970