当前位置:网站首页>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);
}
边栏推荐
- Another miserable day by kotlin grammar
- VScode选中多个单词
- R语言ggplot2可视化:使用ggplot2可视化散点图、使用scale_color_viridis_d函数指定数据点的配色方案
- R language ggplot2 visualization: use ggplot2 to visualize the scatter diagram and use scale_ The size function configures the measurement adjustment range of the size of the data point
- Qt嵌入子Qt程序窗口到当前程序
- [revisiting the classic C language] ~x,%c,%d,%x, etc. in C language, the role of the address character in C language, and the consortium in C language
- Dameng data rushes to the scientific innovation board, or becomes the "first share of domestic database" in the A-share market
- Discussion on the essence of "FPGA mining" from open source projects
- 他是上海两大产业的第一功臣,却在遗憾中默默离世
- R语言ggplot2可视化:使用ggplot2可视化散点图、使用scale_size函数配置数据点的大小的(size)度量调整的范围
猜你喜欢

【模式识别大作业】

Review the writing software with characteristics

Alibaba cloud lifeifei: China's cloud database has taken the lead in many mainstream technological innovations abroad

There are so many kinds of coupons. First distinguish them clearly and then collect the wool!

Set up your own website (13)

一个悄然崛起的国产软件,低调又强大!

这些电影中的科幻构想,已经用AI实现了

科普達人丨漫畫圖解什麼是eRDMA?

Who still remembers "classmate Zhang"?

聊聊怎么做硬件兼容性检测,快速迁移到openEuler?
随机推荐
又被 Kotlin 语法糖坑惨的一天
Object mapping - mapping Mapster
他是上海两大产业的第一功臣,却在遗憾中默默离世
R language ggplot2 visualization: gganimate package is based on Transition_ Time function to create dynamic scatter animation (GIF)
MySQL 表的内连和外连
Redis - ziplist compressed list
Who still remembers "classmate Zhang"?
R语言ggplot2可视化:使用ggplot2可视化散点图、使用scale_x_log10函数配置X轴的数值范围为对数坐标
缓存雪崩和缓存穿透解决方案
R语言ggplot2可视化:使用ggplot2可视化散点图、使用scale_color_viridis_d函数指定数据点的配色方案
R语言ggplot2可视化:使用ggplot2可视化散点图、aes函数中的colour参数指定不同分组的数据点使用不同的颜色显示
The operation and maintenance security gateway (Fortress machine) of Qiming star group once again won the first place!
"War" caused by a bottle of water
R语言ggplot2可视化:使用ggplot2可视化散点图、使用scale_size函数配置数据点的大小的(size)度量调整的范围
Multiparty cardinality testing for threshold private set-2021: Interpretation
[cf] 803 div2 B. Rising Sand
Global Capital Market 101:国内高净值人群最好的投资标的之一BREIT
Customize an annotation to get a link to the database
Stm32f407zgt6 uses SDIO mode to drive SD card
wallys/IPQ8074a/2x(4×4 or 8×8) 11AX MU-MIMO DUAL CONCURRENT EMBEDDEDBOARD