杜引强 2020-03-01
1.导入依赖
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:jms="http://www.springframework.org/schema/jms" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:oxm="http://www.springframework.org/schema/oxm" xmlns:task="http://www.springframework.org/schema/task" xmlns:tool="http://www.springframework.org/schema/tool" xmlns:websocket="http://www.springframework.org/schema/websocket" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd http://www.springframework.org/schema/tool http://www.springframework.org/schema/tool/spring-tool.xsd http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket.xsd"> </beans>
2.编写配置文件(Redis默认使用JDK的序列化方式)
<!-- 声明一个string序列化方式 -->
<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
<!-- 声明一个默认的jdk的序列化方式 -->
<bean id="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"></bean>
<!-- 声明一个jackson的序列化方式 -->
<bean id="jackson2JsonRedisSerializer" class="org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer">
<constructor-arg value="java.lang.Object"></constructor-arg>
</bean>
<!-- 声明一个redis的模板,用来操作redis增删改查的类 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<!-- 引入连接工厂,让模板对象加载的时候,就初始化redis的连接 -->
<property name="connectionFactory" ref="jedisConnectionFactory"></property>
<!--指定redis可以的序列化方式 -->
<property name="keySerializer" ref="stringRedisSerializer"></property>
<!-- 指定value的序列化方式 -->
<property name="valueSerializer" ref="jdkSerializationRedisSerializer"></property>
<!-- 指定hashkey的序列化方式 -->
<property name="hashKeySerializer" ref="stringRedisSerializer"></property>
<!-- 指定hashvalue的序列化方式 -->
<property name="hashValueSerializer" ref="jdkSerializationRedisSerializer"></property>
</bean>
<!-- 声明一个连接工厂 -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<!-- 指定redis的ip和端口号 -->
<property name="hostName" value="192.168.26.130"></property>
<property name="port" value="6379"></property>
</bean>
要指定序列化方式,如果不指定在Redis里会是这样的形式
127.0.0.1:6379> keys * 1) "\xac\xed\x00\x05t\x00\x06myzset" 2) "\xac\xed\x00\x05t\x00\x06mylist" 3) "\xac\xed\x00\x05t\x00\x06myset1" 4) "\xac\xed\x00\x05t\x00\x06myhash"
3.创建一个测试类,注入redis的模板,用操作redis的crud(增删改查)
@Autowired RedisTemplate redisTemplate;
4.加入注解
//去警告的注解 @SuppressWarnings("rawtypes") //spring整合junit单元测试 @RunWith(SpringJUnit4ClassRunner.class) //加载配置文件的注解 @ContextConfiguration("classpath:redis.xml")
5.测试Redis的string类型
@Test public void testString() { //往Redis存string类型 redisTemplate.opsForValue().set("name", "zhangsan"); System.out.println("保存到Redis成功"); //从Redis中取 String value = (String) redisTemplate.opsForValue().get("name"); System.out.println("获取的值是:"+value); //删除指定的key //redisTemplate.delete("name"); }
6.测试Redis的list类型
创建一个实体类,这个实体类必须实现序列化接口
public class User implements Serializable{ private int id; private String name; public User(int id, String name) { super(); this.id = id; this.name = name; } public User() { super(); // TODO Auto-generated constructor stub } @Override public String toString() { return "User [id=" + id + ", name=" + name + "]"; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
测试list
第一种方式:直接以数组的方式存入
@Test public void testList() { User u1 = new User(1,"zhangsan"); User u2 = new User(2,"lisi"); User u3 = new User(3,"wangwu"); User u4 = new User(4,"zhaoliu"); //往Redis中存入list类型的对象 redisTemplate.opsForList().leftPushAll("mylist", u1,u2,u3,u4); //从redis获取list类型的数据 List range = redisTemplate.opsForList().range("mylist", 0, -1); for (Object object : range) { System.out.println(object); } }
第二种方式:存入集合,再转换成数组
ArrayList<User> list = new ArrayList<User>(); list.add(u1); list.add(u2); list.add(u3); list.add(u4); redisTemplate.opsForList().leftPushAll("mylist", list.toArray());//可变参
原理
7.测试hash
第一种存入单个键值对
//存单个键值对 //redisTemplate.opsForHash().put("myhash", "username", "zhangsan");
第二种存入多个键值对,用map的形式
//存多个键值对 User u1 = new User(1,"zhangsan"); User u2 = new User(2,"lisi"); User u3 = new User(3,"wangwu"); User u4 = new User(4,"zhaoliu"); HashMap<String, User> map = new HashMap<String, User>(); map.put("u1", u1); map.put("u2",u2); map.put("u3", u3); map.put("u4", u4); //在hash中存入多个键值对,就用map的方式 redisTemplate.opsForHash().putAll("myhash", map);
获取所有的key值
Set keys = redisTemplate.opsForHash().keys("myhash"); for (Object key : keys) { System.out.println(key); }
获取所有的value值
List values = redisTemplate.opsForHash().values("myhash"); for (Object value : values) { System.out.println(value); }
获取所有的键值对
Map entries = redisTemplate.opsForHash().entries("myhash"); Set entrySet = entries.entrySet(); for (Object object : entrySet) { System.out.println(object); }
8.测试set
在set中存入数据
redisTemplate.opsForSet().add("myset1", "a","b","c","d");
在set中查看数据
Set members = redisTemplate.opsForSet().members("myset1"); for (Object object : members) { System.out.println(object); }
求两个集合的交集
Set intersect = redisTemplate.opsForSet().intersect("myset1", "myset2"); for (Object object : intersect) { System.out.println(object); }
求两个集合的并集
Set union = redisTemplate.opsForSet().union("myset1", "myset2"); for (Object object : union) { System.out.println(object); }
求两个集合的差集(前面集合中有,后面集合中没有)
Set diff = redisTemplate.opsForSet().difference("myset1", "myset2"); for (Object ob : diff) { System.out.println(ob); }
9.测试zset
存数据
redisTemplate.opsForZSet().add("myzset", "zhangsan", 80); redisTemplate.opsForZSet().add("myzset", "lisi", 90); redisTemplate.opsForZSet().add("myzset", "wangwu", 60);
取(按分值从小到大的顺序取的)
Set range = redisTemplate.opsForZSet().range("myzset", 0, -1); for (Object object : range) { System.out.println(object); }
按照分值从大到小的顺序
Set reverseRange = redisTemplate.opsForZSet().reverseRange("myzset", 0, -1); for (Object object : reverseRange) { System.out.println(object); }