- <!--jedis-->
- <dependency>
- <groupId>redis.clients</groupId>
- <artifactId>jedis</artifactId>
- <version>3.7.0</version>
- </dependency>
- <!--单元测试-->
- <dependency>
- <groupId>org.junit.jupiter</groupId>
- <artifactId>junit-jupiter</artifactId>
- <version>5.7.0</version>
- <scope>test</scope>
- </dependency>
- public class JedisConnectionFacotry {
-
- private static final JedisPool jedisPool;
-
- static {
- //配置连接池
- JedisPoolConfig poolConfig = new JedisPoolConfig();
- poolConfig.setMaxTotal(8);
- poolConfig.setMaxIdle(8);
- poolConfig.setMinIdle(0);
- poolConfig.setMaxWaitMillis(1000);
- //创建连接池对象
- jedisPool = new JedisPool(poolConfig,
- "192.168.150.101",6379,1000,"123321");
- }
-
- public static Jedis getJedis(){
- return jedisPool.getResource();
- }
- }
- @BeforeEach
- void setUp(){
- //建立连接
- /*jedis = new Jedis("127.0.0.1",6379);*/
- jedis = JedisConnectionFacotry.getJedis();
- //选择库
- jedis.select(0);
- }
-
- @AfterEach
- void tearDown() {
- if (jedis != null) {
- jedis.close();
- }
- }
- "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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0modelVersion>
- <parent>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-parentartifactId>
- <version>2.5.7version>
- <relativePath/>
- parent>
- <groupId>com.heimagroupId>
- <artifactId>redis-demoartifactId>
- <version>0.0.1-SNAPSHOTversion>
- <name>redis-demoname>
- <description>Demo project for Spring Bootdescription>
- <properties>
- <java.version>1.8java.version>
- properties>
- <dependencies>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-data-redisartifactId>
- dependency>
-
- <dependency>
- <groupId>org.apache.commonsgroupId>
- <artifactId>commons-pool2artifactId>
- dependency>
-
- <dependency>
- <groupId>com.fasterxml.jackson.coregroupId>
- <artifactId>jackson-databindartifactId>
- dependency>
- <dependency>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- <optional>trueoptional>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- <scope>testscope>
- dependency>
- dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-maven-pluginartifactId>
- <configuration>
- <excludes>
- <exclude>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- exclude>
- excludes>
- configuration>
- plugin>
- plugins>
- build>
-
- project>
- spring:
- redis:
- host: 192.168.150.101
- port: 6379
- password: 123321
- lettuce:
- pool:
- max-active: 8 #最大连接
- max-idle: 8 #最大空闲连接
- min-idle: 0 #最小空闲连接
- max-wait: 100ms #连接等待时间
- @SpringBootTest
- class RedisDemoApplicationTests {
-
- @Autowired
- private RedisTemplate
redisTemplate; -
- @Test
- void testString() {
- // 写入一条String数据
- redisTemplate.opsForValue().set("name", "虎哥");
- // 获取string数据
- Object name = redisTemplate.opsForValue().get("name");
- System.out.println("name = " + name);
- }
- }
- @Configuration
- public class RedisConfig {
-
- @Bean
- public RedisTemplate
redisTemplate(RedisConnectionFactory connectionFactory){ - // 创建RedisTemplate对象
- RedisTemplate
template = new RedisTemplate<>(); - // 设置连接工厂
- template.setConnectionFactory(connectionFactory);
- // 创建JSON序列化工具
- GenericJackson2JsonRedisSerializer jsonRedisSerializer =
- new GenericJackson2JsonRedisSerializer();
- // 设置Key的序列化
- template.setKeySerializer(RedisSerializer.string());
- template.setHashKeySerializer(RedisSerializer.string());
- // 设置Value的序列化
- template.setValueSerializer(jsonRedisSerializer);
- template.setHashValueSerializer(jsonRedisSerializer);
- // 返回
- return template;
- }
- }
整体可读性有了很大提升,并且能将Java对象自动的序列化为JSON字符串,并且查询时能自动把JSON反序列化为Java对象。不过,其中记录了序列化时对应的class名称,目的是为了查询时实现自动反序列化。这会带来额外的内存开销。
