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:
redis.clients
jedis
2.8.1
org.springframework.data
spring-data-redis
1.7.2.RELEASE
pom全部文件:
4.0.0
cn.itcast.demo
SpringDataRedisDemo
0.0.1-SNAPSHOT
4.2.4.RELEASE
org.springframework
spring-context
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-aspects
${spring.version}
org.springframework
spring-jms
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-test
${spring.version}
junit
junit
4.9
redis.clients
jedis
2.8.1
org.springframework.data
spring-data-redis
1.7.2.RELEASE
org.apache.maven.plugins
maven-compiler-plugin
3.2
1.7
UTF-8
以上准备工作接下里直接敲代码
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.csdn.net/download/a604435713/10992620