SpringBoot系列之集成Jedis教程,Jedis是老牌的redis客户端框架,提供了比较齐全的redis使用命令,是一款开源的Java 客户端框架,本文使用Jedis3.1.0
加上Springboot2.0
,配合spring-boot-starter-data-redis
使用,只给出简单的使用demo
软件环境:
-
JDK 1.8
-
SpringBoot 2.2.1
-
Maven 3.2+
-
Mysql 8.0.26
-
spring-boot-starter-data-redis 2.2.1
-
jedis3.1.0
-
开发工具
-
IntelliJ IDEA
-
smartGit
-
项目搭建
使用Spring官网的https://start.spring.io
快速创建Spring Initializr
项目
选择maven、jdk版本
选择需要的Dependencies
,选择一下Spring Data Redis
点击next就可以生成一个Springboot项目,不过jedis客户端配置还是要自己加的,所以对pom文件做简单的修改,spring-boot-starter-data-redis
默认使用lettuce
,所以不用的可以exclusion
,然后再加上jedis
的配置
<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><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId>
</dependency>
新建一个application.yml
配置文件,加上redis一些配置
spring:redis:port: 6379host: 127.0.0.1password:timeout: 3000database: 1jedis:pool:max-idle: 8max-active: 8min-idle: 2
新增Redis配置,配置RedisConnectionFactory
为JedisConnectionFactory
,JedisPool
,RedisTemplate
需要用到也可以配置一下
package com.example.jedis.configuration;import lombok.extern.slf4j.Slf4j;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnection;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;@Configuration
@ConditionalOnClass({GenericObjectPool.class, JedisConnection.class, Jedis.class})
@EnableRedisRepositories(basePackages = "com.example.jedis.repository")
@Slf4j
public class RedisConfiguration {@Beanpublic JedisPoolConfig jedisPoolConfig() {return new JedisPoolConfig();}@Beanpublic JedisPool jedisPool() {return new JedisPool(jedisPoolConfig());}@Beanpublic RedisConnectionFactory jedisConnectionFactory() {return new JedisConnectionFactory();}@Beanpublic RedisTemplate<String, Object> redisTemplate() {RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();template.setConnectionFactory(jedisConnectionFactory());template.setKeySerializer(new StringRedisSerializer());template.setValueSerializer(new GenericJackson2JsonRedisSerializer());return template;}
}
写一个实体类,@RedisHash
定义存储的hash key
package com.example.jedis.model;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.springframework.data.redis.core.RedisHash;import java.io.Serializable;@RedisHash("user")
@Data
@SuperBuilder(toBuilder = true)
@EqualsAndHashCode
@AllArgsConstructor
@NoArgsConstructor
public class UserDto implements Serializable {private static final long serialVersionUID = 5962011647926411830L;public enum Gender {MALE, FEMALE}private Long id;private String name;private Gender gender;}
使用Sping Data Redis
的API来实现一个CRUD接口
package com.example.jedis.repository;import com.example.jedis.model.UserDto;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;@Repository
public interface UserRepository extends CrudRepository<UserDto, Long> {
}
写一个测试类来进行测试,ContextConfiguration
指定一个配置类
package com.example.jedis;import cn.hutool.core.util.IdUtil;
import com.example.jedis.configuration.RedisConfiguration;
import com.example.jedis.model.UserDto;
import com.example.jedis.repository.UserRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;@SpringBootTest
@ContextConfiguration(classes = RedisConfiguration.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
class SpringbootJedisApplicationTests {@AutowiredJedisPool jedisPool;@AutowiredRedisTemplate redisTemplate;@AutowiredUserRepository userRepository;@Testvoid contextLoads() {Jedis jedis= jedisPool.getResource();jedis.set("tKey","你好呀");jedis.close();}@Testvoid testRedisTemplate() {redisTemplate.opsForValue().set("rtKey","你好呀");}@Testvoid testCrud() {final UserDto userDto = UserDto.builder().id(IdUtil.getSnowflake().nextId()).name("用户1").gender(UserDto.Gender.MALE).build();userRepository.save(userDto);}}