springdata设计初衷是位简化数据类型和数据的持久化存储,它并不局限是关系型数据库还是nosql数据库,都提供了简化的数据库连接,让数据获取变得更加的简单。所有这些的实现有统一的api提供。
本文主要设置spring-data-redis的相关配置特性:
1.RedisTemplate:高度封装的,自动连接池管理类;
2.对数据类型进行了归类,封装了操作接口类:
a) ValueOperations:key-value操作
b) setOperations:set的相关操作
c) ZsetOperations:
d) HashOperations:hash数据类型操作
e) ListOperations:list数据类型操作
3.对事务进行 封装,通过容器进行控制。
4.序列化机制,提供各种序列化策略选择。
集成配置详解:
1.提供简单封装保存查询操作接口,以及实现类。
public interface RedisCommand { public void save(String key,String value); public String get(String key); public <T> T getObject(String key,Class<T> clazz); }
@Service
public class RedisHandle implements RedisCommand { @Autowired
protected RedisTemplate<String, String> redisTemplate; @Override
public void save(final String key, final String value) {
redisTemplate.execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection)
throws DataAccessException {
connection.set(
redisTemplate.getStringSerializer().serialize(
"user.uid." + key), redisTemplate
.getStringSerializer().serialize(value));
return null;
}
});
} @Override
public String get(final String key) {
return redisTemplate.execute(new RedisCallback<String>() {
@Override
public String doInRedis(RedisConnection connection)
throws DataAccessException {
byte[] keys = redisTemplate.getStringSerializer().serialize(
"user.uid." + key);
if (connection.exists(keys)) {
byte[] value = connection.get(keys);
String name = redisTemplate.getStringSerializer()
.deserialize(value);
return name;
}
return null;
}
});
} @Override
public <T> T getObject(String key, Class<T> clazz) {
// TODO Auto-generated method stub
return null;
} }
2.业务逻辑类
@Service(value="userRedis")
public class UserRedisImpl implements UserRedis{ @Autowired
protected RedisHandle handler; @Override
public void saveUser(final User user) {
handler.save(user.getId()+"", JSONObject.toJSONString(user));
} @Override
public User getUser(final long id) {
String value =handler.get(id+"");
User u= JSONObject.parseObject(value, User.class);
return u;
}
}
3.测试类:
public class TestUseRedis { @SuppressWarnings("resource")
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:/spring-redis.xml");
UserRedis userDAO = (UserRedis)ac.getBean("userRedis");
System.out.println("userDao"+JSONObject.toJSONString(userDAO));
User user1 = new User();
user1.setId(4);
user1.setName("oba2sssss220a");
userDAO.saveUser(user1);
User user2 = userDAO.getUser(2);
System.out.println(user2.getName());
}
}
4.配置文件相关:
spring-redis:
<?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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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"> <context:property-placeholder location="classpath:redis.properties" />
<context:component-scan base-package="com.hoo.report.web.service">
</context:component-scan>
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxActive" value="${redis.maxActive}" />
<property name="maxWait" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean> <bean id="connectionFactory" 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.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean> </beans>
pom引用:
<!-- spring data -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.0.2.RELEASE</version>
</dependency> <dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.1.0</version>
</dependency>
redis.properties:
redis.host=127.0.0.1
redis.port=6379
redis.pass= redis.maxIdle=300
redis.maxActive=600
redis.maxWait=1000
redis.testOnBorrow=true