1.概念:Spring data 提供了RedisTemplate模板,它封装了redis连接池管理的逻辑,业务代码无须关心获取、释放连接资源等逻辑。Spring Redis 同时支持Jedis等客户端操作。
2.RedisTemplate中提供了几个常用的接口方法的使用,如下
private ValueOperations<K,V> valueOps;
private ListOperations<K,V> listOps;
private SetOperation<K,V> setOps;
private ZSetOperations<K,V> zSetOps;
3.导入Redis和Spring整合的架包
<!--Jedis客户端需要依赖jar Redis-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.4.2</version>
</dependency>
<!--Springdata整合Redis-->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.4.2.RELEASE</version>
</dependency>
4.对应实体bean进行序列化操作
5.编写相应的配置文件
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--扫描service包-->
<context:component-scan base-package="service"></context:component-scan>
<!--1.配置连接池信息-->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!--最大连接数-->
<property name="maxTotal" value="50"></property>
<!--最大空闲连接数-->
<property name="maxIdle" value="5"></property>
</bean>
<!--2.Spring整合Jedis(Redis)-->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<!--指定服务器地址-->
<property name="hostName" value="192.168.17.129"></property>
<!--指定服务器端口号-->
<property name="port" value="6379"></property>
<!--指定密码-->
<property name="password" value="123456"></property>
<!--自定义连接池配置-->
<property name="poolConfig" ref="jedisPoolConfig"></property>
</bean>
<!--3.RedisTemplate-->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"></property>
<!--key进行序列化设置 默认JDK更改为String-->
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
</property>
<!--value 默认JDK更改为String-->
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
</property>
</bean>
</beans>
Hash测试
@Service
public class UserServiceImpl implements UserService{
@Autowired
RedisTemplate<String,String> redisTemplate;
@Resource(name = "redisTemplate")
HashOperations<String,Integer,UserInfo> hash;
/**Redis String类型测试
*通过某个key得到值
* 如果key在Redis中不存在,到数据库进行查询
* 如果存在,就到redis中查询
*/
@Override
public String getString(String key) {
ValueOperations<String,String> string = redisTemplate.opsForValue();
redisTemplate.opsForValue().set("java666","这是一个测试数据",2, TimeUnit.HOURS);
if (redisTemplate.hasKey(key)){
//在Redis中取出并返回
System.out.println("在redis中取出返回的!");
return string.get(key);
}else {
String result = "RedisTenplate模板练习";
string.set(key,result);
System.out.println("在MySQL中取出并返回!");
return result;
}
}
//RedisTemplate 针对Hash案例
//存储 往Redis中存储Hash数据
public void add(UserInfo u){
redisTemplate.opsForHash().put("user",u.getId(),u);
}
/**
* 根据用户主键ID查询 对象
*/
public UserInfo selectById(int id){
//1.先判断Redis中是否存在该KEY,如果存在,从Redis中获取,并返回
if (hash.hasKey("user",id)){
UserInfo u = hash.get("user",id);
System.out.println("redis中查询出的对象!");
return u;
}else {//如果不存在,从数据库中查询,取出赋值,并返回
UserInfo u = new UserInfo();
u.setId(id);
u.setAge(22);
u.setName("666");
u.setSex("男");
System.out.println("mysql数据库中查询出的数据!");
hash.put("user",u.getId(),u);
return u;
}
}
}
TestHash.java
public class TestHash {
public static void main(String[] args) {
testHash1();
}
private static void testHash1() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring_redis.xml");
UserService userServiceImpl = applicationContext.getBean("userServiceImpl", UserService.class);
userServiceImpl.selectById(5);
}
}