随着非关系型数据库(NoSQL数据库)概念的流行,Spring也开始提供非关系型数据库的支持,Spring主要提供以下几种非关系型数据库的支持:
MongoDB -----文档数据库,不是通用的数据库,它们所擅长解决的是一个很小的问题集
Neo4j -----------图数据库。
Redis -----------键值对数据库
现如今用的比较多的NoSQL数据库是Redis数据库,我们以Spring整合Redis数据库为例了解Spring对NoSQL的支持
一、Spring Data Redis体系结构分析
Spring-data-redis提供了在srping应用中通过简单的配置访问redis服务,对reids底层开发包(Jedis, JRedis, and RJC)进行了高度封装,RedisTemplate提供了redis各种操作、异常处理及序列化,支持发布订阅,并对spring 3.1 cache进行了实现。
连接到 Redis
Redis 连接工厂会生成到 Redis 数据库服务器的连接。 Spring Data Redis 为四种 Redis 客户端实现提供了连接工厂:
JedisConnectionFactory----------最为常用
SrpConnectionFactory
LettuceConnectionFactory
JredisConnectionFactory
操作Redis
RedisTemplate对应不同需求封装了如下操作:
opsForValue()------普通键值对操作
opsForList()---------ArrayList键值对操作
opsForSet()---------HashSet键值对操作
opsForHash()------HashMap键值对操作
二、Spring 整合使用Spring Data Redis
①引入依赖
org.springframework.data spring-data-redis 2.1.5.RELEASE redis.clients jedis 2.9.1
②编写redis配置文件:redis.properties
#redis地址
redis.host=127.0.0.1
#redis端口
redis.port=6379
#redis密码,一般不需要
redis.password=“”
#最大空闲时间
redis.maxIdle=400
#最大连接数
redis.maxTotal=6000
#最大等待时间
redis.maxWaitMillis=1000
#连接耗尽时是否阻塞,false报异常,true阻塞超时 默认:true
redis.blockWhenExhausted=true
#在获得链接的时候检查有效性,默认false
redis.testOnBorrow=true
#超时时间,默认:2000
redis.timeout=100000
③编写Spring配置文件并配置Redis:application.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:context=“http://www.springframework.org/schema/context”
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop
http://www.springframework.org/schema
/aop/spring-aop.xsd">
context:annotation-config/
<context:component-scan base-package=“com.my.spring”/>
<context:property-placeholder location=“classpath*:redis.properties”/>
④编写Redis工具类:RedisManager,注入RedisTemplate
@Data
public class RedisHelper {
//注入RedisTemplate
private RedisTemplate<String,Object> redisTemplate;
/**
-
设置过期时间
-
@param key
-
@param time
-
@return
*/
public boolean expire(String key, long time) {
return this.redisTemplate.expire(key,time, TimeUnit.SECONDS);
}
/**
-
是否存在key
-
@param key
-
@return
*/
public Object hasKey(String key){
return this.redisTemplate.hasKey(key);
}
/**
-
获取过期时间
-
@param key
-
@return
*/
public long getExpire(String key){
return this.redisTemplate.getExpire(key);
}
/**
-
根据key获取值
-
@param key
-
@return
*/
public Object get(String key){
Object o = redisTemplate.opsForValue().get(key);
return o;
}
/**
-
存储key-value
-
@param key
-
@param value
-
@return
*/
public boolean set(String key,Object value){
redisTemplate.opsForValue().set(key,value);
return true;
}
/**
-
存入key-value并设置过期时间,以秒为单位
-
@param key
-
@param value
-
@param time
-
@return
*/
public boolean set(String key,Object value,long time){
redisTemplate.opsForValue().set(key,value,time,TimeUnit.SECONDS);
return true;
}
/**
-
将值以set形式存入redis中
-
@param key
-
@param values
-
@return
*/
public long sSet(String key,Object …values){
return this.redisTemplate.opsForSet().add(key,values);
}
/**
-
获取键为key的set
-
@param key
-
@return
*/
public Set sGet(String key){
return this.redisTemplate.opsForSet().members(key);
}
}
⑤编写测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {“classpath:application.xml”})
public class AppTest {
@Autowired
private RedisHelper redisHelper;
@Test
public void testSet(){
this.redisHelper.set(“testKey”,“testValue”)
}
@Test
public void testGet(){
System.out.println(this.redisHelper.get(“testKey”));
}
@Test
public void testsSet(){
this.redisHelper.sSet(“testKey2”,“testValue1”,“testValue2”,“testValue3”)
}
@Test
public void testsGet(){
Set set = this.redisHelper.sGet(“testKey2”);
System.out.println(set.toString());
}
}
运行测试,测试结果:
运行testSet():
运行testGet():
testValue
运行testsSet():
运行testsGet():
[testValue1, testValue3, testValue2]