xl_echo编辑整理,欢迎转载,转载请声明文章来源。更多IT、编程案例、资料请联系QQ:1280023003
百战不败,依不自称常胜,百败不颓,依能奋力前行。——这才是真正的堪称强大!!
创建一个Maven管理的jar工程:SpringDataRedisDemo
第一步:在pom.xml文件中引入依赖,依赖主要有一下几块
- spring基本框架依赖
- junit依赖
- jedis依赖
- spring-data-reids依赖
详细如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.echo.demo</groupId>
<artifactId>SpringDataRedisDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringDataRedisDemo</name>
<!-- 集中定义依赖版本号 -->
<properties>
<spring.version>4.2.4.RELEASE</spring.version>
</properties>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- junit依赖 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
</dependency>
<!-- 缓存 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.2.RELEASE</version>
</dependency>
</dependencies>
</project>
第二步:编写一个applicationContext-redis.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" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
<context:property-placeholder location="classpath*:properties/*.properties" />
<!-- redis 相关配置 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" /> <!-- 最大空闲数 -->
<property name="maxWaitMillis" value="${redis.maxWait}" /> <!-- 连接时的最大等待毫秒数 -->
<property name="testOnBorrow" value="${redis.testOnBorrow}" /> <!-- 在提取一个 jedis 实例时,是否提前进行验证操作;如果为 true,则得到的 jedis
实例均是可用的; -->
</bean>
<!-- jedis链接工厂类 -->
<bean id="JedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig"/>
<!-- redis操作模版 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="JedisConnectionFactory" />
</bean>
</beans>
第三步:编写一个redis-config.properties文件
# Redis settings
# server IP 链接地址
redis.host=127.0.0.1
# server port 默认链接端口
redis.port=6379
# server pass 链接密码
redis.pass=
# use dbIndex
redis.database=0
#\u63A7\u5236\u4E00\u4E2Apool\u6700\u591A\u6709\u591A\u5C11\u4E2A\u72B6\u6001\u4E3Aidle(\u7A7A\u95F2\u7684)\u7684jedis\u5B9E\u4F8B
redis.maxIdle=300
#\u8868\u793A\u5F53borrow(\u5F15\u5165)\u4E00\u4E2Ajedis\u5B9E\u4F8B\u65F6\uFF0C\u6700\u5927\u7684\u7B49\u5F85\u65F6\u95F4\uFF0C\u5982\u679C\u8D85\u8FC7\u7B49\u5F85\u65F6\u95F4(\u6BEB\u79D2)\uFF0C\u5219\u76F4\u63A5\u629B\u51FAJedisConnectionException\uFF1B
redis.maxWait=3000
#\u5728borrow\u4E00\u4E2Ajedis\u5B9E\u4F8B\u65F6\uFF0C\u662F\u5426\u63D0\u524D\u8FDB\u884Cvalidate\u64CD\u4F5C\uFF1B\u5982\u679C\u4E3Atrue\uFF0C\u5219\u5F97\u5230\u7684jedis\u5B9E\u4F8B\u5747\u662F\u53EF\u7528\u7684
redis.testOnBorrow=true
完成以上三步,并启动Redis服务,就完成了基本的准备工作,下面就是正式编写Redis使用案例了
第四步:
- 实现简单的往Redis中存、取和删一个键值对(注意:一定要保证Redis已经启动了)
package test;
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;
@SuppressWarnings("unchecked")
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")
public class TestValue {
@SuppressWarnings("rawtypes")
@Autowired
private RedisTemplate redisTemplate;
/**
* 往Redis中存值
*/
@Test
public void testSetValue(){
redisTemplate.boundValueOps("name").set("xlecho");
}
/**
* 从Redis中取出key为name的值
*/
@Test
public void getValue(){
String name = (String) redisTemplate.boundValueOps("name").get();
System.out.println("name:= " + name);
}
/**
* 从Redis中删除一个key为name的值
*/
@Test
public void deleteValue(){
redisTemplate.delete("name");
}
}
- 实现一个Set集合的数据存、取和删
package test;
import java.util.HashSet;
import java.util.Set;
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;
@SuppressWarnings("unchecked")
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")
public class TestSetValue {
@SuppressWarnings("rawtypes")
@Autowired
private RedisTemplate redisTemplate;
/**
* 往Redis存入一个set集合的数据
*/
@Test
public void testSetValue(){
Set<String> s = new HashSet<String>();
s.add("1");
s.add("2");
s.add("3");
s.add("4");
s.add("5");
for(String str : s){
redisTemplate.boundSetOps("xlecho").add(str);
}
}
/**
* 从Redis中取出一个set集合
*/
@Test
public void testgetSetValue(){
Set<String> xlecho = redisTemplate.boundSetOps("xlecho").members();
System.out.println(xlecho);
}
/**
* 从Redis中删除set集合的某一个值
*/
@Test
public void removeSetValue(){
redisTemplate.boundSetOps("xlecho").remove("1");
}
/**
* 将members这个set集合从Redis删除
*/
@Test
public void deleteSetValue(){
redisTemplate.delete("xlecho");
}
}
- 实现一个List集合的数据存、取和删
package test;
import java.util.ArrayList;
import java.util.List;
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;
@SuppressWarnings("all")
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")
public class TestList {
@Autowired
private RedisTemplate redisTemplate;
/**
* 以右压栈的形式往Redis中存入一个List集合
* 右压栈形式,先存的在前面,后存的在后面
*/
@Test
public void testSetList1(){
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
for(int i : list){
redisTemplate.boundListOps("list1").rightPush(i);
}
}
/**
* 显示右压栈list的数据
*/
@Test
public void testGetList(){
List list = redisTemplate.boundListOps("list1").range(0, 10);
System.out.println(list);
}
/**
* 以左压栈的形式往Redis中存入一个List集合
* 左压栈形式,先存的在后面,后存的在前面
*/
@Test
public void testSetList2(){
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
for(int i : list){
redisTemplate.boundListOps("list2").leftPush(i);
}
}
/**
* 显示左压栈list的数据
*/
@Test
public void testGetList2(){
List list = redisTemplate.boundListOps("list2").range(0, 10);
System.out.println(list);
}
/**
* 根据索引查询某个list集合的数据
*/
@Test
public void testSearchByIndex(){
Integer index = (Integer) redisTemplate.boundListOps("list1").index(1);
System.out.println(index);
}
/**
* 移除list集合中的某个元素
*/
@Test
public void removeListValue(){
redisTemplate.boundListOps("list1").remove(1, 2);//1为移除的个数,因为list中可以重复,所以如果有2个2,设置为1就是移除一个2
}
}
- 实现一个HashMap的数据存、取和删
package test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
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;
@SuppressWarnings("all")
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")
public class TestHash {
@Autowired
private RedisTemplate redisTemplate;
/**
* 存入一个HashMap集合
*/
@Test
public void testSetHash(){
Map<String, String> map = new HashMap<>();
map.put("a", "路人甲");
map.put("b", "土匪丙");
map.put("c", "强盗丁");
map.put("d", "孔已乙");
for(String str : map.keySet()){
String s = map.get(str);
redisTemplate.boundHashOps("Hash").put(str, s);
}
}
/**
* 提取HashMap集合中的所有Key
*/
@Test
public void testGetHashKeys(){
Set keys = redisTemplate.boundHashOps("Hash").keys();
System.out.println(keys);
}
/**
* 提取HashMap集合中的所有value值
*/
@Test
public void testGetHashValues(){
List values = redisTemplate.boundHashOps("Hash").values();
System.out.println(values);
}
/**
* 根据key获取某一个值
*/
@Test
public void testGetHashValue(){
Object object = redisTemplate.boundHashOps("Hash").get("a");
System.out.println(object);
}
/**
* 根据key删除一个键值对
*/
@Test
public void testRemoveValueByKey(){
redisTemplate.boundHashOps("Hash").delete("a");
}
}
做一个有底线的博客主