所需依赖
<dependencies>
<!-- jedis:负责让java程序连接redis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.6.0</version>
</dependency>
<!-- Spring集成redis的依赖 -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.5.0</version>
</dependency>
<!-- Jackson序列化依赖 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.3</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
</dependencies>
将Redis交给Spring容器管理
本次使用配置类的方式进行配置IoC容器
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.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;
@Configuration
public class RedisConfig {
/**
* Jedis连接池配置
* @return
*/
public JedisPoolConfig jedisPoolConfig() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMinIdle(10);
jedisPoolConfig.setMinIdle(2);
jedisPoolConfig.setMaxTotal(100);
return jedisPoolConfig;
}
/**
* Jedis连接工厂
* @return
*/
@Bean
public JedisConnectionFactory jedisConnectionFactory() {
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(jedisPoolConfig());
return jedisConnectionFactory;
}
/**
* Redis工具类
* @return
*/
@Bean
public RedisTemplate redisTemplate(){
RedisTemplate template = new RedisTemplate();
template.setConnectionFactory(jedisConnectionFactory());
//创建序列化对象
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
//指定redis的键值对的序列化方式默认JDK的序列化方式
template.setKeySerializer(stringRedisSerializer);
template.setValueSerializer(genericJackson2JsonRedisSerializer);
template.setHashKeySerializer(stringRedisSerializer);
template.setHashValueSerializer(genericJackson2JsonRedisSerializer);
return template;
}
}
对Redis的String数据类型操作
import com.entor.config.RedisConfig;
import com.entor.entity.User;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.concurrent.TimeUnit;
public class Test04 {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(RedisConfig.class);
RedisTemplate template = context.getBean(RedisTemplate.class);
//value是String类型的操作方法
template.opsForValue().set("id","1");
template.opsForValue().set("name","张三");
template.opsForValue().set("sex","男",10, TimeUnit.SECONDS);
template.opsForValue().set("age",20,20,TimeUnit.SECONDS);
template.opsForValue().set("姓名","李四",20,TimeUnit.SECONDS);
User user = new User();
user.setName("李四");
user.setId(1);
template.opsForValue().set("李四",user,20,TimeUnit.SECONDS);
System.out.println("id="+template.opsForValue().get("id"));
System.out.println("name="+template.opsForValue().get("name"));
System.out.println("sex="+template.opsForValue().get("sex"));
System.out.println("age="+template.opsForValue().get("age"));
System.out.println("姓名="+template.opsForValue().get("姓名"));
System.out.println("李四="+template.opsForValue().get("李四"));
}
}
对Redis的Hash数据类型操作
import com.entor.config.RedisConfig;
import com.entor.entity.User;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Test05 {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(RedisConfig.class);
RedisTemplate template = context.getBean(RedisTemplate.class);
//value是hash类型的操作方法
//hash设置多个属性的值
HashMap hashMap = new HashMap();
hashMap.put("学号一",new User(1,20,"张三"));
hashMap.put("学号二",new User(2,20,"张三"));
hashMap.put("学号三",new User(3,20,"张三"));
hashMap.put("学号四",new User(4,20,"张三"));
template.opsForHash().putAll("班级1",hashMap);
//hash设置单个属性的值
template.opsForHash().put("班级1","学号五",new User(5,20,"张三"));
System.out.println("--------------获取所有键值对----------------");
Map map = template.opsForHash().entries("班级1");
map.forEach((k,v)->{
System.out.println(k+"----------"+v);
});
System.out.println("--------------获取所有键----------------");
Set set = template.opsForHash().keys("班级1");
set.forEach(key-> System.out.println(key));
System.out.println("--------------获取所有值----------------");
List list = template.opsForHash().values("班级1");
list.forEach(val-> System.out.println(val));
}
}
对Redis的List数据类型操作
import com.entor.config.RedisConfig;
import com.entor.entity.User;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.ArrayList;
import java.util.List;
public class Test06 {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(RedisConfig.class);
RedisTemplate template = context.getBean(RedisTemplate.class);
//value为list类型的操作方法
List<String> list = new ArrayList<>();
list.add("张三");
list.add("李四");
list.add("王五");
List<String> list2 = new ArrayList<>();
list2.add("aa");
list2.add("bb");
list2.add("cc");
List<User> list3 = new ArrayList<>();
list3.add(new User(1,20,"张三"));
list3.add(new User(2,20,"张三"));
list3.add(new User(3,20,"张三"));
list3.add(new User(4,20,"张三"));
//头部插入
template.opsForList().leftPushAll("names",list);
template.opsForList().leftPushAll("names",list3);
template.opsForList().leftPush("names","赵六");
//尾部插入
template.opsForList().rightPushAll("names",list2);
template.opsForList().rightPush("names","黄七");
System.out.println("---------------获取所有值------------------");
System.out.println("元素个数:"+template.opsForList().size("names"));
List values = template.opsForList().range("names", 0, -1);
values.forEach(val-> System.out.println(val));
System.out.println("---------------获取指定元素,第一个元素序号为0------------------");
System.out.println(template.opsForList().index("names", 1));
}
}
对Redis的Set数据类型操作
import com.entor.config.RedisConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.Set;
public class Test07 {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(RedisConfig.class);
RedisTemplate template = context.getBean(RedisTemplate.class);
//value为set类型的操作方法
//添加数据
template.opsForSet().add("set1", "张三", "李四", "王五");
//集合元素个数
System.out.println(template.opsForSet().size("set1"));
System.out.println("-------------集合元素的获取和遍历------------------");
Set set1 = template.opsForSet().members("set1");
set1.forEach(val -> System.out.println(val));
System.out.println("-------------随机移除集合内的元素 -----------------");
Object o = template.opsForSet().pop("set1");
System.out.println("移除的元素:" + o);
template.opsForSet().add("set2", "张三", "李四", "王五", "李四", "赵六");
template.opsForSet().add("set3", "张一", "朱二", "王三", "李四", "赵七");
System.out.println("-------------两个集合的交集 -----------------");
Set intersect = template.opsForSet().intersect("set2", "set3");
intersect.forEach(val -> System.out.println(val));
System.out.println("-------------两个集合的并集 -----------------");
Set union = template.opsForSet().union("set2", "set3");
union.forEach(val -> System.out.println(val));
System.out.println("-------------两个集合的差集 -----------------");
Set difference = template.opsForSet().difference("set2", "set3");
difference.forEach(val -> System.out.println(val));
System.out.println("---------------两个集合的交集存入到另个的集合中(将set4集合清空)------------------");
Long store = template.opsForSet().intersectAndStore("set2", "set3", "set4");
System.out.println("元素个数:" + store);
System.out.println("---------------两个集合的并集存入到另个的集合中(将set5集合清空)------------------");
store = template.opsForSet().unionAndStore("set2", "set3", "set5");
System.out.println("元素个数:" + store);
System.out.println("---------------两个集合的差集存入到另个的集合中(将set6集合清空)------------------");
store = template.opsForSet().differenceAndStore("set2", "set3", "set6");
System.out.println("元素个数:" + store);
}
}
补充
RedisTemplate:默认JDK的序列化方式
StringRedisTemplate:默认设置序列化方式为:StringRedisSerializer.UTF_8,所以序列化的数据必须是字符串的属性。
注意:本次序列化的对象没有序列化ID,程序能够正常序列化和反序列化。为了保险还是为对象加上序列化。
获取连接对象(Jedis ,Lettuce ):本次使用的是Jedis 对象。
Jedis nativeConnection = (Jedis) redis TempLate . getConnectionFactory() . getConnection() .getNativeConnection();