第十一章 SpringBoot整合SpringDataRedis
2021-03-02 13:26
标签:snap suse ann srp nec Fix 数据 redis 整合 Jedis是Redis官方推出的一款面向Java的客户端,提供了很多接口供Java语言调用。可以在Redis官网下载,当然还有一些开源爱好者提供的客户端,如Jredis、SRP等等,推荐使用Jedis。 Spring-data-redis是spring大家族的一部分,提供了在srping应用中通过简单的配置访问redis服务,对reids底层开发包(Jedis, JRedis, and RJC)进行了高度封装,RedisTemplate提供了redis各种操作、异常处理及序列化,支持发布订阅,并对spring 3.1 cache进行了实现。 SetOperations:set类型数据操作 (1)构建Maven工程SpringDataRedisDemo (4)在src/main/resources下创建properties文件夹,建立application.properties (5) 添加Redis的配置类 第十一章 SpringBoot整合SpringDataRedis 标签:snap suse ann srp nec Fix 数据 redis 整合 原文地址:https://www.cnblogs.com/ly447742/p/14409042.html1. SpringDataRedis简介
1.1.Jedis(过时)
1.2.Spring Data Redis(推荐)
spring-data-redis针对jedis提供了如下功能:
1.连接池自动管理,提供了一个高度封装的“RedisTemplate”类
2.针对jedis客户端中大量api进行了归类封装,将同一类型操作封装为operation接口
ValueOperations:简单K-V操作
ZSetOperations:zset类型数据操作
HashOperations:针对map类型的数据操作
ListOperations:针对list类型的数据操作2.Spring Data Redis入门小Demo
2.1.准备工作
(2)引入Springboot和SpringDataRedis依赖spring.redis.jedis.pool.max-idle=10
spring.redis.jedis.pool.min-idle=5
spring.redis.pool.max-total=20
spring.redis.hostName=122.51.50.249
spring.redis.port=6379
添加Redis的java配置类,设置相关的信息。package com.bruceliu.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;
/**
* @author bruceliu
* @create 2019-10-29 11:55
* @description
*/
@Configuration
public class RedisConfig {
/**
* 1.创建JedisPoolConfig对象。在该对象中完成一些链接池配置
* @ConfigurationProperties:会将前缀相同的内容创建一个实体。
*/
@Bean
@ConfigurationProperties(prefix="spring.redis.pool")
public JedisPoolConfig jedisPoolConfig(){
JedisPoolConfig config = new JedisPoolConfig();
/*//最大空闲数
config.setMaxIdle(10);
//最小空闲数
config.setMinIdle(5);
//最大链接数
config.setMaxTotal(20);*/
System.out.println("默认值:"+config.getMaxIdle());
System.out.println("默认值:"+config.getMinIdle());
System.out.println("默认值:"+config.getMaxTotal());
return config;
}
/**
* 2.创建JedisConnectionFactory:配置redis链接信息
*/
@Bean
@ConfigurationProperties(prefix="spring.redis")
public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig config){
System.out.println("配置完毕:"+config.getMaxIdle());
System.out.println("配置完毕:"+config.getMinIdle());
System.out.println("配置完毕:"+config.getMaxTotal());
JedisConnectionFactory factory = new JedisConnectionFactory();
//关联链接池的配置对象
factory.setPoolConfig(config);
//配置链接Redis的信息
//主机地址
/*factory.setHostName("192.168.70.128");
//端口
factory.setPort(6379);*/
return factory;
}
/**
* 3.创建RedisTemplate:用于执行Redis操作的方法
*/
@Bean
public RedisTemplate
2.2.实体类
package com.bruceliu.bean;
import java.io.Serializable;
/**
* @author bruceliu
* @create 2019-10-29 11:56
* @description
*/
public class Users implements Serializable {
private static final long serialVersionUID = 6206472024994638411L;
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Users [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
3. 字符串类型操作
package com.bruceliu.test;
import com.bruceliu.APP;
import com.bruceliu.bean.Users;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.test.context.junit4.SpringRunner;
/**
* @author bruceliu
* @create 2019-10-29 11:59
* @description
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = APP.class)
public class SpringbootRedisDemoApplicationTests {
@Autowired
private RedisTemplate
4. SET类型操作
package com.bruceliu.test;
import com.bruceliu.APP;
import org.junit.Test;
import org.junit.runner.RunWith;
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.context.junit4.SpringRunner;
import java.util.Set;
/**
* @author bruceliu
* @create 2019-10-29 12:10
* @description
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = APP.class)
public class TestSet {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void setValue() {
redisTemplate.boundSetOps("nameset").add("曹操");
redisTemplate.boundSetOps("nameset").add("刘备");
redisTemplate.boundSetOps("nameset").add("孙权");
}
@Test
public void getValue() {
Set set = redisTemplate.boundSetOps("nameset").members();
System.out.println(set);
}
@Test
public void removeValue() {
redisTemplate.boundSetOps("nameset").remove("孙权");
}
@Test
public void delete() {
redisTemplate.delete("nameset");
}
}
5. List类型操作
package com.bruceliu.test;
import com.bruceliu.APP;
import org.junit.Test;
import org.junit.runner.RunWith;
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.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
import java.util.Set;
/**
* @author bruceliu
* @create 2019-10-29 12:12
* @description
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = APP.class)
public class TestList {
@Autowired
private RedisTemplate redisTemplate;
/*
* 右压栈 : 后添加的元素排在后边
*/
@Test
public void testSetValue1() {
redisTemplate.boundListOps("namelist1").rightPush("刘备");
redisTemplate.boundListOps("namelist1").rightPush("关羽");
redisTemplate.boundListOps("namelist1").rightPush("张飞");
}
/**
* 显示右压栈的值
*/
@Test
public void testGetValue1() {
List list = redisTemplate.boundListOps("namelist1").range(0, 10);
System.out.println(list);
}
@Test
public void delete() {
redisTemplate.delete("namelist1");
}
/**
* 左压栈
*/
@Test
public void testSetValue2() {
redisTemplate.boundListOps("namelist2").leftPush("刘备");
redisTemplate.boundListOps("namelist2").leftPush("关羽");
redisTemplate.boundListOps("namelist2").leftPush("张飞");
}
/**
* 显示左压栈的值
*/
@Test
public void testGetValue2() {
List list = redisTemplate.boundListOps("namelist2").range(0, 10);
System.out.println(list);
}
/**
* 删除值
*/
@Test
public void removeValue() {
redisTemplate.boundListOps("namelist1").remove(0, "刘备");
}
}
6. Hash类型操作
package com.bruceliu.test;
import com.bruceliu.APP;
import org.junit.Test;
import org.junit.runner.RunWith;
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.context.junit4.SpringRunner;
import java.util.List;
import java.util.Set;
/**
* @author bruceliu
* @create 2019-10-29 12:14
* @description
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = APP.class)
public class TestHash {
@Autowired
private RedisTemplate redisTemplate;
/**
* 存值
*/
@Test
public void testSetValue() {
redisTemplate.boundHashOps("namehash").put("a", "唐僧");
redisTemplate.boundHashOps("namehash").put("b", "悟空");
redisTemplate.boundHashOps("namehash").put("c", "八戒");
redisTemplate.boundHashOps("namehash").put("d", "沙僧");
}
/**
* 获取所有的key
*/
@Test
public void testGetKes() {
Set keys = redisTemplate.boundHashOps("namehash").keys();
System.out.println(keys);
}
/**
* 获取所有的值
*/
@Test
public void testGetValues() {
List list = redisTemplate.boundHashOps("namehash").values();
System.out.println(list);
}
/**
* 根据KEY取值
*/
@Test
public void searchValueByKey() {
String str = (String) redisTemplate.boundHashOps("namehash").get("b");
System.out.println(str);
}
/**
* 移除某个小key的值
*/
@Test
public void removeValue() {
redisTemplate.boundHashOps("namehash").delete("c");
}
}
7. Zset类型操作
package com.bruceliu.test;
import com.bruceliu.APP;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.DefaultTypedTuple;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
/**
* @author bruceliu
* @create 2019-10-29 12:17
* @description
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = APP.class)
public class TestZset {
@Autowired
RedisTemplate redisTemplate;
/**
* Boolean add(V value, double score);
* 添加元素到变量中同时指定元素的分值。
*/
@Test
public void testAdd() {
redisTemplate.boundZSetOps("zSetValue").add("A", 1);
redisTemplate.boundZSetOps("zSetValue").add("B", 3);
redisTemplate.boundZSetOps("zSetValue").add("C", 2);
redisTemplate.boundZSetOps("zSetValue").add("D", 5);
redisTemplate.boundZSetOps("zSetValue").add("E", 4);
}
/**
* Set
文章标题:第十一章 SpringBoot整合SpringDataRedis
文章链接:http://soscw.com/index.php/essay/59044.html