您的位置:首页 > 游戏 > 手游 > 怎么做公众号小程序_社交电商app开发_新闻头条最新消息今日头条_中国刚刚发生8件大事

怎么做公众号小程序_社交电商app开发_新闻头条最新消息今日头条_中国刚刚发生8件大事

2024/12/22 11:23:58 来源:https://blog.csdn.net/m0_58838332/article/details/142452670  浏览:    关键词:怎么做公众号小程序_社交电商app开发_新闻头条最新消息今日头条_中国刚刚发生8件大事
怎么做公众号小程序_社交电商app开发_新闻头条最新消息今日头条_中国刚刚发生8件大事

(在跑 Redis 测试之前,需要先安装 Redis,并开启 Redis 服务)

Spring Boot 项目添加依赖

NoSQL -> Spring Data Redis

pom.xml 文件如下

    <dependencies><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-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build>

Spring boot 所有的配置类,都有一个自动配置类

自动配置类都会绑定一个 properties 配置文件

application.properties:

#配置redis
spring.data.redis.host=127.0.0.1
spring.data.redis.port=6397

测试类:

package com.demo.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 RedisApplicationTests {@Autowiredprivate RedisTemplate redisTemplate;@Testvoid contextLoads() {redisTemplate.opsForValue().set("mykey","张三");System.out.println(redisTemplate.opsForValue().get("mykey"));}}

pojo 包下创建实体类

implements Serializable 所有 pojo 的对象都会序列化

package com.demo.redis.pojo;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;import java.io.Serializable;@Component
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {  //所有pojo的对象都会序列化private String name;private int age;
}

测试类:

package com.demo.redis;import com.demo.redis.pojo.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
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 RedisApplicationTests {@Autowiredprivate RedisTemplate redisTemplate;@Testvoid contextLoads() {redisTemplate.opsForValue().set("mykey","张三");System.out.println(redisTemplate.opsForValue().get("mykey"));}@Testpublic void test() throws JsonProcessingException {User user = new User("张三", 18);//String jsonUser = new ObjectMapper().writeValueAsString(user);//redisTemplate.opsForValue().set("user",jsonUser);redisTemplate.opsForValue().set("user",user);System.out.println(redisTemplate.opsForValue().get("user"));}}

config 包下配置类:

可以自定义 RedisTemplate

package com.demo.redis.config;import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
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.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;@Configuration
public class RedisConfig {//自定义一个RedisTemplate@Beanpublic RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){RedisTemplate<String,Object> template = new RedisTemplate<>();template.setConnectionFactory(factory);//Json序列化配置Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);ObjectMapper objectMapper = new ObjectMapper();objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);jackson2JsonRedisSerializer.setObjectMapper(objectMapper);//String的序列化StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();//key采用String的序列化方式template.setKeySerializer(stringRedisSerializer);//hash的key采用String的序列化方式template.setHashKeySerializer(stringRedisSerializer);//value序列化方法采用jacksontemplate.setValueSerializer(jackson2JsonRedisSerializer);//hash的value序列化方法采用jacksontemplate.setHashValueSerializer(jackson2JsonRedisSerializer);template.afterPropertiesSet();return template;}
}

测试类在属性上方添加 @Qualifier("redisTemplate") 指定 config 包下的配置类

版权声明:

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

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