赞
踩
pom.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
-
- <groupId>com.liuyc.redis</groupId>
- <artifactId>Redis-Springboot-2022</artifactId>
- <version>1.0-SNAPSHOT</version>
-
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-dependencies</artifactId>
- <version>2.7.3</version>
- </parent>
-
- <packaging>jar</packaging>
-
- <dependencies>
- <!-- 引用spring-web开发包 -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- <version>2.2.2.RELEASE</version>
- </dependency>
-
- <!-- 配置使用 redis 启动器 -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-redis</artifactId>
- <version>2.7.5</version>
- </dependency>
- </dependencies>
-
- </project>
application.properties
- spring.redis.host=119.23.217.151
- #Redis服务器连接端口
- spring.redis.port=6379
- #Redis服务器连接密码(默认为空)
- spring.redis.password=xxxxx
-
RedisConfig
- package com.liuyc.redis.config;
-
- import com.fasterxml.jackson.annotation.JsonAutoDetect;
- import com.fasterxml.jackson.annotation.JsonTypeInfo;
- import com.fasterxml.jackson.annotation.PropertyAccessor;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
- 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.core.StringRedisTemplate;
- import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
-
- @Configuration
- public class RedisConfig {
-
- /**
- * 默认是JDK的序列化策略,这里配置redisTemplate采用的是Jackson2JsonRedisSerializer的序列化策略
- * @param redisConnectionFactory
- * @return
- */
- @Bean
- public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
- //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
- Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
- ObjectMapper om = new ObjectMapper();
- // 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public
- om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
- // 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会抛出异常
- //om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);旧版本
- om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance ,
- ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
- jackson2JsonRedisSerializer.setObjectMapper(om);
- RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
- // 配置连接工厂
- redisTemplate.setConnectionFactory(redisConnectionFactory);
- //使用StringRedisSerializer来序列化和反序列化redis的key值
- //redisTemplate.setKeySerializer(new StringRedisSerializer());
- redisTemplate.setKeySerializer(jackson2JsonRedisSerializer);
- // 值采用json序列化
- redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
- redisTemplate.setHashKeySerializer(jackson2JsonRedisSerializer);
- redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
- redisTemplate.afterPropertiesSet();
- return redisTemplate;
- }
-
- /**
- * 默认采用的是String的序列化策略
- * @param redisConnectionFactory
- * @return
- */
- @Bean
- public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory){
- StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
- stringRedisTemplate.setConnectionFactory(redisConnectionFactory);
- return stringRedisTemplate;
- }
- }
RedisMain
- package com.liuyc.redis;
-
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
-
- @SpringBootApplication
- public class RedisMain {
-
- public static void main(String[] args) {
- SpringApplication.run(RedisMain.class);
- }
- }
RedisTestController
- package com.liuyc.redis.controller;
-
-
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.data.redis.core.RedisTemplate;
- import org.springframework.data.redis.core.StringRedisTemplate;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- @RestController
- public class RedisTestController {
-
- @Autowired
- private RedisTemplate<String,Object> redisTemplate;
-
- @Autowired
- private StringRedisTemplate stringRedisTemplate;
-
- @GetMapping("/getCount")
- public Object getCount(){
- // Long count = stringRedisTemplate.opsForValue().increment("count");
- Long count = redisTemplate.opsForValue().increment("count1");
- System.out.println(count);
- return count;
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。