当前位置:网站首页>Redis annotation
Redis annotation
2022-07-27 18:55:00 【Get lazy】
@Autowired
private StringRedisTemplate redisTemplate; @Autowired
private RedisTemplate redisTemplate;The upper and lower ones are the same, but using the following one will cause garbled code. You have to use
@Configuration
public class RedisConfig {
@Bean(name = "redisTemplate")
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
// Customized Serialization tools customized for values
Jackson2JsonRedisSerializer jacksonSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jacksonSerializer.setObjectMapper(om);
// To key Set the serialization tool
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// Set up customized serialization tools
// Using annotations @Bean return RedisTemplate When , Simultaneous configuration hashKey And hashValue How to serialize .
// key use String How to serialize
template.setKeySerializer(stringRedisSerializer);
// value The serialization method adopts jackson
template.setValueSerializer(jacksonSerializer);
// hash Of key Also used String How to serialize
template.setHashKeySerializer(stringRedisSerializer);
// hash Of value The serialization method adopts jackson
template.setHashValueSerializer(jacksonSerializer);
template.afterPropertiesSet();
// Customization ends
return template;
}
}ListOperations、ValueOperations、SetOperations、ZSetOperations、HashOperations How to use the interface
@Resource(name="redisTemplate")
private ValueOperations valueOperations;And so on .
Why use Resource Annotation is because ValueOperations yes StringRedisTemplate Subclasses of
This is a redisTemplate elective
ValueOperations<String, String> ValueOperations1 = redisTemplate.opsForValue();This is the choice redisTemplate.opforValue(). Things will be found with the following direct use ValueOperations It's the same


So choose ValueOperations Yes, I skipped redisTemplate.opforValue(). But the results are the same
边栏推荐
- Uploading and downloading of files
- Here are all the MySQL interview questions you can't expect (the latest version of 2022)
- I'm stupid. When completable future is used with openfegin, it even reports an error
- 我人都傻了,CompletableFuture和OpenFegin一起使用竟然报错
- [yuntu said] 249 mobile application security service - app's physical examination center, comprehensive testing, safe on the road!
- 个人中心--订单业务流程
- Wechat applet obtains mobile number
- 订单超时取消 及 按类别查询商品
- What should I do if MySQL master-slave replication data is inconsistent?
- 多功能无线遥控艾灸仪芯片-DLTAP703SD
猜你喜欢
随机推荐
浴室带除雾化妆镜触摸芯片-DLT8T10S
阿里p8总结的10条 SQL 优化方案(非常实用)
如何实现Word、PDF、TXT文件的全文内容检索?
MySQL create event execution task
Run the uniapp to the mobile phone (real machine debugging)
Overview of Baidu map technology, and application development of basic API and webapi
Generate PDM file from Navicat export table
MySQL 03 高级查询(一)
电动加热护颈枕芯片-DLTAP703SC
飞机大战敌机出场
Jianmu continuous integration platform v2.5.2 release
TS study notes class
Have you ever stumbled on MySQL's order by
was not registered for synchronization because synchronization is not active[已解决]
Wechat payment and payment callback
内网的公司邮箱服务器怎么发外部邮件
Talking about JVM (frequent interview)
瑞吉外卖sql表
SQL server stored procedures multi angle introduction suggestions collection
express get/post/delete...请求







