Spring整合Redis
中国加油,武汉加油!
篇幅较长,配合目录观看
项目准备
- 本案例基于liunx篇】 十二. Redis简介及安装 一
- liunx篇】 十二. Redis常用命令和数据类型 二
- liunx篇】 十二. Redis事务 三
- liunx篇】 十二. Redis发布订阅和超时指令 四
- liunx篇】 十二. Redis持久化机制和内存回收机制 五
1. 新建spring-redis
1.1 删掉src做站点工程
2. java操作Redis
2.1 spring-redis下新建java-redis(module-maven)
2.2 导包
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
2.3 Test
package com.wpj;
import org.junit.Test;
import redis.clients.jedis.Jedis;
public class JedisTest {
@Test
public void testJedis(){
// 连接Redis
Jedis jedis = new Jedis("192.168.59.100", 6379);
// 设置连接jedis密码
jedis.auth("admin");
// 设置一个键值对
jedis.set("name","wpj");
// 根据键获取值
String name = jedis.get("name");
System.out.println(name);
// 关闭连接
jedis.close();
}
}
3. spring操作redis
3.1 spring-redis下新建spring-redis2(module-maven)
3.2 导包
<!-- spring整合redis的依赖 -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
3.3 编写beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 创建一个连接的对象-->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="50" /> <!-- 最大空闲数 -->
<property name="maxTotal" value="100" /> <!-- 最大连接数 -->
<property name="maxWaitMillis" value="20000" /> <!-- 最大等待时间 -->
</bean>
<!-- 配置redis连接工厂 -->
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="poolConfig" ref="poolConfig" /> <!-- 连接池配置 -->
<property name="hostName" value="192.168.59.100" /> <!-- 连接主机 -->
<property name="port" value="6379" /> <!-- 端口 -->
<property name="password" value="admin" /> <!-- 密码 -->
</bean>
<!-- 配置redis模板对象 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory" /> <!-- 配置连接工厂 -->
<property name="keySerializer" ref="stringRedisSerializer" /> <!-- 设置字符串序列方式 -->
<property name="valueSerializer" ref="stringRedisSerializer" /> <!-- 设置字符串序列方式 -->
</bean>
<!-- 字符串序列化 -->
<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer">
</bean>
</beans>
3.3 Test
package com.wpj;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(value = SpringJUnit4ClassRunner.class)
@ContextConfiguration(value ="classpath:beans.xml")
public class SpringRedisTest {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void testSpringRedis(){
redisTemplate.opsForValue().set("username","admin");
System.out.println(redisTemplate.opsForValue().get("username"));
}
}
3.4 其他类型
package com.wpj;
import com.wpj.bean.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
import java.util.Set;
@RunWith(value = SpringJUnit4ClassRunner.class)
@ContextConfiguration(value ="classpath:beans.xml")
public class SpringRedisTest {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void testSpringRedis(){
redisTemplate.opsForValue().set("name","吴培杰");
System.out.println(redisTemplate.opsForValue().get("name"));
}
@Test
public void testAddUser() {
ValueOperations valueOperations = redisTemplate.opsForValue();
// 1.创建一个对象
User user = new User();
user.setId(10);
user.setUsername("admin");
// 2.添加到redis中,记得把stringRedisSerializer去掉
valueOperations.set("user", user);
// 3.获取
User user1 =(User) valueOperations.get("user");
System.out.println(user1.getUsername());
redisTemplate.delete("user");
}
/**
* 操作Hash类型
*/
@Test
public void testHash(){
HashOperations hash = redisTemplate.opsForHash();
// put key filed value
hash.put("stu","name","admin");
hash.put("stu","age","12");
Object o = hash.get("stu", "name");
System.out.println(o);
hash.delete("xxx","xx");
}
/**
* 操作List类型
*/
@Test
public void testList(){
ListOperations listOperations = redisTemplate.opsForList();
// lpul key a1 a2 a3
listOperations.leftPush("books","java");
listOperations.leftPush("books","js");
listOperations.leftPushAll("books","js","c#",".net");
List<String> books = listOperations.range("books", 0, -1);
for(String key :books){
System.out.println(key);
}
listOperations.leftPop("books");
listOperations.rightPop("books");
listOperations.remove("books",2,"java");
listOperations.index("books",3); // 获取指定位置的元素
Long books1 = listOperations.size("books"); // 获取key的长度
}
/**
* 操作Set类型
*/
@Test
public void testSet(){
SetOperations setOperations = redisTemplate.opsForSet();
setOperations.add("phones","xiaomi","hauwei","phone");
Set phones = setOperations.members("phones"); // 获取key中所有的数据
System.out.println(phones);
setOperations.pop("phones"); // 随机弹出一个元素
}
/**
* 操作Sorted Set类型
*/
@Test
public void testSortedSet(){
ZSetOperations zSetOperations = redisTemplate.opsForZSet();
zSetOperations.add("demo","zs",10.0);
zSetOperations.add("demo","lisi",20.0);
zSetOperations.add("demo","wagnwu",15.0);
Set demo = zSetOperations.range("demo", 0, -1);
System.out.println(demo);
}
}