当前位置:网站首页>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);
}
边栏推荐
- Evaluation of IP location query interface Ⅲ
- 服务器常用的一些硬件信息(不断更新)
- R language ggplot2 visualization: use ggplot2 to visualize the scatter diagram, and_ Set show in the point parameter_ The legend parameter is false, and the legend information is not displayed
- wallys/600VX – 2×2 MIMO 802.11ac Mini PCIe Wi-Fi Module, Dual Band, 2,4GHz / 5GHz QCA 9880
- Cache avalanche and cache penetration solutions
- wallys/600VX – 2 × 2 MIMO 802.11ac Mini PCIe Wi-Fi Module, Dual Band, 2,4GHz / 5GHz QCA 9880
- 再不上市,旷视科技就熬不住了
- Alibaba cloud database represented by polardb ranks first in the world
- shell第一个命令结果传入第二个命令删除
- Yolov5 export the pit encountered by onnx
猜你喜欢

数据库连接池 druid

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

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

koa - 洋葱模型浅析

治数如治水,数据治理和数据创新难在哪?

Set up your own website (13)

线下门店为什么要做新零售?

In depth analysis of Apache bookkeeper series: Part 4 - back pressure

"War" caused by a bottle of water

Oracle netsuite helps TCM bio understand data changes and make business development more flexible
随机推荐
[cf] 803 div2 A. XOR Mixup
再不上市,旷视科技就熬不住了
数据库连接池 druid
Evaluation of IP location query interface Ⅲ
又被 Kotlin 语法糖坑惨的一天
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
CVPR 2022 | greatly reduce the manual annotation required for zero sample learning. Mapu and Beiyou proposed category semantic embedding rich in visual information
60 divine vs Code plug-ins!!
Typescript readonlyarray (read only array type) details
服务器常用的一些硬件信息(不断更新)
YOLOv5导出onnx遇到的坑
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
Review the writing software with characteristics
【模式识别大作业】
[pattern recognition]
HMS Core音频编辑服务3D音频技术,助力打造沉浸式听觉盛宴
用宝塔建第2个网站时网站总是报错:No input file specified.
治数如治水,数据治理和数据创新难在哪?
Multiparty cardinality testing for threshold private set-2021: Interpretation
A quietly rising domestic software, low-key and powerful!