当前位置:网站首页>redis在项目中的使用
redis在项目中的使用
2022-06-30 11:41:00 【qingqing不秃头】
1.引入依赖
org.springframework.boot
spring-boot-starter-data-redis
2.在yml文件中进行配置
3.redis相关配置类
package com.redis.confic;
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 {
@Bean
@SuppressWarnings("all")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(factory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// key采用String的序列化方式
template.setKeySerializer(stringRedisSerializer);
// hash的key也采用String的序列化方式
template.setHashKeySerializer(stringRedisSerializer);
// value序列化方式采用jackson
template.setValueSerializer(jackson2JsonRedisSerializer);
// hash的value序列化方式采用jackson
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
4.进行测试
@Test
public void demo2() {
User user = new User("zrq",18);
Set<String> keys = redisTemplate.keys("*");
redisTemplate.delete(keys);
redisTemplate.opsForValue().set("user", JSON.toJSONString(user));
String user1 = redisTemplate.opsForValue().get("user");
System.out.println(user1);
User user2 = JSON.parseObject(String.valueOf(user1), User.class);
System.out.println(user2);
}
边栏推荐
- The sci-fi ideas in these movies have been realized by AI
- R language ggplot2 visualization: gganimate package is based on Transition_ Time function to create dynamic scatter animation (GIF)
- Re understand oauth2.0 protocol for joint login
- R语言ggplot2可视化:gganimate包基于transition_time函数创建动态散点图动画(gif)、使用labs函数为动画图添加动态时间标题(抽取frame_time信息)
- 数据库 自动增长
- Customize an annotation to get a link to the database
- R language ggplot2 visualization: use ggplot2 to visualize the scatter diagram and use scale_ color_ viridis_ D function specifies the color scheme of data points
- 使用深度学习进行生物网络分析
- There are so many kinds of coupons. First distinguish them clearly and then collect the wool!
- R语言ggplot2可视化:使用ggplot2可视化散点图、使用scale_size函数配置数据点的大小的(size)度量调整的范围
猜你喜欢

60 个神级 VS Code 插件!!

Limited time appointment | Apache pulsar Chinese developer and user group meeting in June

Boost研究:Boost Log

Win10 R package installation error: not installed in arch=i386

网络营销之四大误解

YOLOv5导出onnx遇到的坑

"War" caused by a bottle of water

限时预约|6 月 Apache Pulsar 中文开发者与用户组会议

Another miserable day by kotlin grammar

NoSQL——Redis的配置与优化
随机推荐
用于生成学习任务的量子神经网络2022最新综述
R language ggplot2 visual Facet: gganimate package is based on Transition_ The time function creates a dynamic scatter graph animation (GIF) and uses the labs function to add a dynamic time title to t
缓存雪崩和缓存穿透解决方案
Typescript readonlyarray (read only array type) details
一个悄然崛起的国产软件,低调又强大!
Uncover the whole link communication process of customer service im
MySQL 表的内连和外连
R语言ggplot2可视化:使用ggplot2可视化散点图、aes函数中的size参数指定数据点的大小(point size)
Quel est le rôle du rétroéclairage LED?
Multiparty Cardinality Testing for Threshold Private Set-2021:解读
Typescript readonlyarray (read only array type) details
Stm32f407zgt6 uses SDIO mode to drive SD card
c# 怎样能写个sql的解析器
Alibaba cloud database represented by polardb ranks first in the world
Redis - ziplist compressed list
Oracle netsuite helps TCM bio understand data changes and make business development more flexible
Limited time appointment | Apache pulsar Chinese developer and user group meeting in June
led背光板的作用是什么呢?
How to analyze native crash through GDB
论文解读(AGC)《Attributed Graph Clustering via Adaptive Graph Convolution》