您的位置:首页 > 游戏 > 游戏 > SpringBoot整合Redis

SpringBoot整合Redis

2025/8/10 7:21:18 来源:https://blog.csdn.net/m0_65152767/article/details/139863970  浏览:    关键词:SpringBoot整合Redis

文章目录

  • 1、创建springboot工程
  • 2、添加依赖
  • 3、创建配置文件
  • 4、创建启动类
  • 5、创建测试类,编写测试方法
  • 6、序列化定制

1、创建springboot工程

在这里插入图片描述

2、添加依赖

<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.0.5</version><relativePath/> <!-- lookup parent from repository --></parent><!-- Generated by https://start.springboot.io --><!-- 优质的 spring/boot/data/security/cloud 框架中文文档尽在 => https://springdoc.cn --><groupId>com.atguigu</groupId><artifactId>springboot-redis</artifactId><version>0.0.1-SNAPSHOT</version><name>springboot-redis</name><description>springboot-redis</description><properties><java.version>17</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</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></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

3、创建配置文件

spring:data:redis:host: 192.168.74.148port: 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 {@Autowiredprivate RedisTemplate redisTemplate;@Testvoid 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 {@Beanpublic 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> 

在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com