pom文件:
<?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>org.example</groupId> <artifactId>springredis</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.5</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 依赖Spring Boot Data Redis --> <!-- 依赖Spring Boot Data Redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <exclusions> <exclusion> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> </exclusion> </exclusions> </dependency> <!--对象池化组件,配合Java客户端JedisPool使用--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency> <!--jedis的jar包--> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.3.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency> </dependencies> </project>
配置文件:
#Redis\u6570\u636E\u5E93\u7D22\u5F15\uFF08\u9ED8\u8BA4\u4E3A0\uFF09 spring.redis.database=0 #Redis\u670D\u52A1\u5668\u5730\u5740\uFF08\u9ED8\u8BA4\u4E3Alocalhost\uFF09 spring.redis.host=127.0.0.1 #Redis\u670D\u52A1\u5668\u8FDE\u63A5\u7AEF\u53E3\uFF08\u9ED8\u8BA4\u4E3A6379\uFF09 spring.redis.port=6379 #Redis\u670D\u52A1\u5668\u8FDE\u63A5\u5BC6\u7801\uFF08\u9ED8\u8BA4\u4E3A\u7A7A\uFF09 spring.redis.password= #\u8FDE\u63A5\u8D85\u65F6\u65F6\u95F4\uFF08\u6BEB\u79D2\uFF09 spring.redis.timeout=3600 #\u662F\u5426\u4F7F\u7528ssl\uFF0C\u9ED8\u8BA4\u4E3Afalse spring.redis.ssl=false #\u914D\u7F6E\u8FDE\u63A5\u6C60 #\u5982\u679C\u4F7F\u7528lettuce \u5219\u5C06jedis\u6539\u6210lettuce\u5373\u53EF #\u8FDE\u63A5\u6C60\u6700\u5927\u963B\u585E\u7B49\u5F85\u65F6\u95F4\uFF08\u4F7F\u7528\u8D1F\u503C\u8868\u793A\u6CA1\u6709\u9650\u5236\uFF09 \u9ED8\u8BA4\u503C\u4E3A-1 spring.redis.lettuce.pool.max-wait=-1 #\u8FDE\u63A5\u6C60\u6700\u5927\u8FDE\u63A5\u6570\uFF08\u4F7F\u7528\u8D1F\u503C\u8868\u793A\u6CA1\u6709\u9650\u5236\uFF09 \u9ED8\u8BA4\u503C\u4E3A8 spring.redis.lettuce.pool.max-active=8 #\u8FDE\u63A5\u6C60\u4E2D\u7684\u6700\u5927\u7A7A\u95F2\u8FDE\u63A5 \u9ED8\u8BA4\u503C\u4E3A8 spring.redis.lettuce.pool.max-idle=8 #\u8FDE\u63A5\u6C60\u4E2D\u7684\u6700\u5C0F\u7A7A\u95F2\u8FDE\u63A5 \u9ED8\u8BA4\u503C\u4E3A0 spring.redis.lettuce.pool.min-idle=0 #\u8FDE\u63A5\u6C60\u6700\u5927\u963B\u585E\u7B49\u5F85\u65F6\u95F4\uFF0C\u5355\u4F4D\u6BEB\u79D2\uFF08\u4F7F\u7528\u8D1F\u503C\u8868\u793A\u6CA1\u6709\u9650\u5236\uFF09 spring.redis.jedis.pool.max-wait=3600 #\u8FDE\u63A5\u6C60\u6700\u5927\u8FDE\u63A5\u6570\uFF08\u4F7F\u7528\u8D1F\u503C\u8868\u793A\u6CA1\u6709\u9650\u5236\uFF09 \u9ED8\u8BA4\u503C\u4E3A8 spring.redis.jedis.pool.max-active=8 #\u8FDE\u63A5\u6C60\u4E2D\u7684\u6700\u5927\u7A7A\u95F2\u8FDE\u63A5 \u9ED8\u8BA4\u503C\u4E3A8 spring.redis.jedis.pool.max-idle=8 #\u8FDE\u63A5\u6C60\u4E2D\u7684\u6700\u5C0F\u7A7A\u95F2\u8FDE\u63A5 \u9ED8\u8BA4\u503C\u4E3A0 spring.redis.jedis.pool.min-idle=0
redis配置文件:
key序列化为String;
value序列化为json对象;
package org.example.config; 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.*; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.RedisSerializer; @Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { // 创建一个RedisTemplate对象 RedisTemplate<String, Object> template = new RedisTemplate<>(); // 设置连接工厂 template.setConnectionFactory(redisConnectionFactory); // 创建json序列化工具 GenericJackson2JsonRedisSerializer jsonRedisSerializer = new GenericJackson2JsonRedisSerializer(); //配置key序列化 template.setKeySerializer(RedisSerializer.string()); template.setHashKeySerializer(RedisSerializer.string()); //配置value序列化 template.setValueSerializer(jsonRedisSerializer); template.setHashValueSerializer(jsonRedisSerializer); return template; } }
POJO类:
package org.example.Entity; public class MyUser { private int id; private String name; public MyUser() { } public MyUser(int id, String name, int age) { this.id = id; this.name = name; this.age = age; } private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "MyUser{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}'; } }
controller:
package org.example.controller; import org.example.Entity.MyUser; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.*; @RestController public class MyUserController { @Autowired private RedisTemplate<String,Object> redisTemplate; /** * 查询所有用户信息 * @return */ @GetMapping(value = "/get") public MyUser get() { redisTemplate.opsForValue().set("abc",new MyUser(10,"a",11)); MyUser s = (MyUser) redisTemplate.opsForValue().get("abc"); System.out.println(s); return s; } }
标签:java,示例,spring,redis,u8FDE,org,u63A5,public From: https://www.cnblogs.com/xiaobaibailongma/p/17111748.html