当前位置:网站首页>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);
}
边栏推荐
- 如何使用插件化机制优雅的封装你的请求hook
- Automatic database growth
- Alibaba cloud lifeifei: China's cloud database has taken the lead in many mainstream technological innovations abroad
- 安装onnx很慢,使用清华镜像
- R language ggplot2 visualization: use ggplot2 to visualize the scatter diagram, and_ Set the alpha parameter in the point parameter to specify the transparency level of data points (points transparent
- Boost研究:Boost Log
- R语言ggplot2可视化:使用ggplot2可视化散点图、使用scale_color_viridis_d函数指定数据点的配色方案
- R语言ggplot2可视化:gganimate包基于transition_time函数创建动态散点图动画(gif)
- WebView, Scrollview sliding conflict correction
- OpenMLDB Meetup No.4 会议纪要
猜你喜欢

Evaluation of IP location query interface Ⅲ

A theoretical defect of relative position coding transformer and Its Countermeasures

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

Our company has used this set of general solutions for 7 years, and has opened up dozens of systems, a stable batch!

Dameng data rushes to the scientific innovation board, or becomes the "first share of domestic database" in the A-share market

The sci-fi ideas in these movies have been realized by AI

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

STM32F407ZGT6使用SDIO方式驱动SD卡

60 divine vs Code plug-ins!!

AutoCAD - len command
随机推荐
Typescript readonlyarray (read only array type) details
R language ggplot2 visualization: use ggplot2 to visualize the scatter diagram and use scale_ x_ The log10 function configures the value range of the X axis to be logarithmic coordinates
Summer vacation study record
R语言ggplot2可视化:gganimate包基于transition_time函数创建动态散点图动画(gif)
线下门店为什么要做新零售?
HMS core audio editing service 3D audio technology helps create an immersive auditory feast
用宝塔建第2个网站时网站总是报错:No input file specified.
wallys/3×3 MIMO 802.11ac Mini PCIe Wi-Fi Module, QCA9880, 2,4GHz / 5GHzDesigned for Enterprise
限时预约|6 月 Apache Pulsar 中文开发者与用户组会议
Re understand oauth2.0 protocol for joint login
使用深度学习进行生物网络分析
网络营销之四大误解
It is said that with this, the boss opened the test overnight
A theoretical defect of relative position coding transformer and Its Countermeasures
koa - 洋葱模型浅析
Summer vacation study record
Dameng data rushes to the scientific innovation board, or becomes the "first share of domestic database" in the A-share market
Object mapping - mapping Mapster
Shell first command result is transferred to the second command delete
聊聊怎么做硬件兼容性检测,快速迁移到openEuler?