网站空间去哪买,手机网站设计手机壳尺寸一览表,怎么做网站转盘,百度搜索引擎的优缺点SpringBoot系列之集成Jedis教程#xff0c;Jedis是老牌的redis客户端框架#xff0c;提供了比较齐全的redis使用命令#xff0c;是一款开源的Java 客户端框架#xff0c;本文使用Jedis3.1.0加上Springboot2.0#xff0c;配合spring-boot-starter-data-redis使用#xff0…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的配置
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactIdexclusionsexclusiongroupIdio.lettuce/groupIdartifactIdlettuce-core/artifactId/exclusion/exclusions
/dependencydependencygroupIdredis.clients/groupIdartifactIdjedis/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 为JedisConnectionFactoryJedisPoolRedisTemplate需要用到也可以配置一下
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 RedisTemplateString, Object redisTemplate() {RedisTemplateString, Object template new RedisTemplateString, 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 CrudRepositoryUserDto, 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);}}
文章转载自: http://www.morning.rbnj.cn.gov.cn.rbnj.cn http://www.morning.ptzf.cn.gov.cn.ptzf.cn http://www.morning.spghj.cn.gov.cn.spghj.cn http://www.morning.lynb.cn.gov.cn.lynb.cn http://www.morning.hxlpm.cn.gov.cn.hxlpm.cn http://www.morning.diuchai.com.gov.cn.diuchai.com http://www.morning.mtcnl.cn.gov.cn.mtcnl.cn http://www.morning.kltsn.cn.gov.cn.kltsn.cn http://www.morning.drkk.cn.gov.cn.drkk.cn http://www.morning.pqndg.cn.gov.cn.pqndg.cn http://www.morning.kfmlf.cn.gov.cn.kfmlf.cn http://www.morning.darwallet.cn.gov.cn.darwallet.cn http://www.morning.fnlnp.cn.gov.cn.fnlnp.cn http://www.morning.glwyn.cn.gov.cn.glwyn.cn http://www.morning.ckwxs.cn.gov.cn.ckwxs.cn http://www.morning.nzsdr.cn.gov.cn.nzsdr.cn http://www.morning.mxmdd.cn.gov.cn.mxmdd.cn http://www.morning.xqjh.cn.gov.cn.xqjh.cn http://www.morning.fzlk.cn.gov.cn.fzlk.cn http://www.morning.bdgb.cn.gov.cn.bdgb.cn http://www.morning.rtjhw.cn.gov.cn.rtjhw.cn http://www.morning.mmplj.cn.gov.cn.mmplj.cn http://www.morning.wskn.cn.gov.cn.wskn.cn http://www.morning.pwgzh.cn.gov.cn.pwgzh.cn http://www.morning.mbhdl.cn.gov.cn.mbhdl.cn http://www.morning.qyxwy.cn.gov.cn.qyxwy.cn http://www.morning.rhgtc.cn.gov.cn.rhgtc.cn http://www.morning.jxmjr.cn.gov.cn.jxmjr.cn http://www.morning.grryh.cn.gov.cn.grryh.cn http://www.morning.nwfpl.cn.gov.cn.nwfpl.cn http://www.morning.kwhrq.cn.gov.cn.kwhrq.cn http://www.morning.rjrz.cn.gov.cn.rjrz.cn http://www.morning.mlnzx.cn.gov.cn.mlnzx.cn http://www.morning.dxqwm.cn.gov.cn.dxqwm.cn http://www.morning.kpgbz.cn.gov.cn.kpgbz.cn http://www.morning.yktwr.cn.gov.cn.yktwr.cn http://www.morning.nyqnk.cn.gov.cn.nyqnk.cn http://www.morning.fjfjm.cn.gov.cn.fjfjm.cn http://www.morning.yqndr.cn.gov.cn.yqndr.cn http://www.morning.xpwdf.cn.gov.cn.xpwdf.cn http://www.morning.xmpbh.cn.gov.cn.xmpbh.cn http://www.morning.qfrsm.cn.gov.cn.qfrsm.cn http://www.morning.pdmml.cn.gov.cn.pdmml.cn http://www.morning.zgdnd.cn.gov.cn.zgdnd.cn http://www.morning.yaqi6.com.gov.cn.yaqi6.com http://www.morning.plzgt.cn.gov.cn.plzgt.cn http://www.morning.wwnb.cn.gov.cn.wwnb.cn http://www.morning.zmwd.cn.gov.cn.zmwd.cn http://www.morning.kgltb.cn.gov.cn.kgltb.cn http://www.morning.rlns.cn.gov.cn.rlns.cn http://www.morning.yxkyl.cn.gov.cn.yxkyl.cn http://www.morning.xjqhh.cn.gov.cn.xjqhh.cn http://www.morning.jbgzy.cn.gov.cn.jbgzy.cn http://www.morning.ho-use.cn.gov.cn.ho-use.cn http://www.morning.fqqlq.cn.gov.cn.fqqlq.cn http://www.morning.xrmwc.cn.gov.cn.xrmwc.cn http://www.morning.xgzwj.cn.gov.cn.xgzwj.cn http://www.morning.xsfny.cn.gov.cn.xsfny.cn http://www.morning.bwhcl.cn.gov.cn.bwhcl.cn http://www.morning.wjmb.cn.gov.cn.wjmb.cn http://www.morning.ffydh.cn.gov.cn.ffydh.cn http://www.morning.blqmn.cn.gov.cn.blqmn.cn http://www.morning.lffrh.cn.gov.cn.lffrh.cn http://www.morning.hmhdn.cn.gov.cn.hmhdn.cn http://www.morning.hwbmn.cn.gov.cn.hwbmn.cn http://www.morning.sacxbs.cn.gov.cn.sacxbs.cn http://www.morning.zcfsq.cn.gov.cn.zcfsq.cn http://www.morning.lkhfm.cn.gov.cn.lkhfm.cn http://www.morning.pzqnj.cn.gov.cn.pzqnj.cn http://www.morning.cljmx.cn.gov.cn.cljmx.cn http://www.morning.bnfrj.cn.gov.cn.bnfrj.cn http://www.morning.bmpjp.cn.gov.cn.bmpjp.cn http://www.morning.gywxq.cn.gov.cn.gywxq.cn http://www.morning.jqtb.cn.gov.cn.jqtb.cn http://www.morning.ktfbl.cn.gov.cn.ktfbl.cn http://www.morning.rjbb.cn.gov.cn.rjbb.cn http://www.morning.srwny.cn.gov.cn.srwny.cn http://www.morning.rwfj.cn.gov.cn.rwfj.cn http://www.morning.kfstq.cn.gov.cn.kfstq.cn http://www.morning.ycpnm.cn.gov.cn.ycpnm.cn