SpringDataRedis这个框架我这里不多说,不懂的去百度,以下直接贴代码
@未经博主允许不得转载
配置文件:
1.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
2.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}" />
</bean>
<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"/>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="JedisConnectionFactory" />
</bean>
</beans>
工程目录结构:
pom核心文件:
<!-- 缓存 -->
<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>
pom全部文件:
<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>cn.itcast.demo</groupId>
<artifactId>SpringDataRedisDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 集中定义依赖版本号 -->
<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>
<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>
<build>
<plugins>
<!-- java编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
以上准备工作接下里直接敲代码
1.值类型操作
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;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")//指定配置文件所在路径
public class TestVlaue {
@Autowired
private RedisTemplate redisTemplate;//注入模版
@Test
public void setValue() {
redisTemplate.boundValueOps("name").set("晓哥");//存值
}
@Test
public void getValue() {
String str = (String) redisTemplate.boundValueOps("name").get();//取值
System.out.println(str);
}
@Test
public void delValue() {
redisTemplate.delete("name");//删除
}
}
2.set集合操作
package test;
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;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")//指定配置文件所在路径
public class TestSet {
@Autowired
private RedisTemplate redisTemplate;//注入模版
@Test
public void setValue() {
//set集合
redisTemplate.boundSetOps("nameSet").add("t211");
redisTemplate.boundSetOps("nameSet").add("t212");
redisTemplate.boundSetOps("nameSet").add("t216");
}
@Test
public void getValue() {
//set集合取直
Set set = redisTemplate.boundSetOps("nameSet").members();
System.out.println(set);
}
@Test
public void delValued() {
//移除集合的元素
redisTemplate.boundSetOps("nameSet").remove("t216");
}
@Test
public void delete() {
//移除集合所有元素
redisTemplate.delete("nameSet");
}
}
3.List类型操作
package test;
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;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")//指定配置文件所在路径
public class TestList {
@Autowired
private RedisTemplate redisTemplate;//注入模版
/**
*
* 右压栈:后加的元素排在后边先进先出
*
*/
@Test
public void SetValue() {
//向list里面存值
redisTemplate.boundListOps("nameList").rightPush("t211");
redisTemplate.boundListOps("nameList").rightPush("t212");
redisTemplate.boundListOps("nameList").rightPush("t216");
redisTemplate.boundListOps("nameList").rightPush("zking");
redisTemplate.boundListOps("nameList").rightPush("小小");
}
@Test
public void getValue() {
//从下标为0查到下标为10的元素
List list = redisTemplate.boundListOps("nameList").range(0, 10);
System.out.println(list);
}
/**
* 左压栈:和右压栈相反
*
*/
@Test
public void setValue2() {
redisTemplate.boundListOps("nameList2").leftPush("t211");
redisTemplate.boundListOps("nameList2").leftPush("t212");
redisTemplate.boundListOps("nameList2").leftPush("t216");
redisTemplate.boundListOps("nameList2").leftPush("zking");
redisTemplate.boundListOps("nameList2").leftPush("小小");
}
@Test
public void getValue2() {
//从下标为0查到下标为10的元素
List list = redisTemplate.boundListOps("nameList2").range(0, 10);
System.out.println(list);
}
@Test
public void find() {
//获取指定的下标的值
String list = (String) redisTemplate.boundListOps("nameList").index(0);
System.out.println(list);
}
@Test
public void delValue() {
//删除
redisTemplate.boundListOps("nameList2").remove(2, "t216");
}
}
Hash类型操作
package test;
import java.util.List;
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;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/applicationContext-redis.xml") // 指定配置文件所在路径
public class MapTest {
@Autowired
private RedisTemplate redisTemplate;// 注入模版
/**
* 存值
*
*/
@Test
public void setMap() {
redisTemplate.boundHashOps("map").put("a", "t11");
redisTemplate.boundHashOps("map").put("b", "t12");
redisTemplate.boundHashOps("map").put("c", "t16");
redisTemplate.boundHashOps("map").put("d", "zking");
redisTemplate.boundHashOps("map").put("e", "小小");
}
@Test
public void getMapKey() {
// 获取所有map集合的key
Set map = (Set) redisTemplate.boundHashOps("map").keys();
System.out.println(map);
}
@Test
public void getMapValue() {
// 获取所有map集合的values
List map = (List) redisTemplate.boundHashOps("map").values();
System.out.println(map);
}
@Test
public void getMap() {
// 根据key获取值
String map = (String) redisTemplate.boundHashOps("map").get("a");
System.out.println(map);
}
@Test
public void delMap() {
// 根据key移除值
redisTemplate.boundHashOps("map").delete("a");
}
}
懒人不想装Redis的话直接打开以下文件就好咯了双击打开就可以了无需安装Redis
下载地址:https://download.youkuaiyun.com/download/a604435713/10992620