1 2 3 4

SSM整合redis

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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!-- <context:property-placeholder location="classpath:*.properties"/> -->
<context:property-placeholder location="classpath:redis.properties"/>
<!--设置数据池-->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}"></property>
<property name="minIdle" value="${redis.minIdle}"></property>
<property name="maxTotal" value="${redis.maxTotal}"></property>
<property name="maxWaitMillis" value="${redis.maxWaitMillis}"></property>
<property name="testOnBorrow" value="${redis.testOnBorrow}"></property>
</bean>
<!--链接redis-->
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.host}"></property>
<property name="port" value="${redis.port}"></property>
<property name="password" value="${redis.password}"></property>
<property name="poolConfig" ref="poolConfig"></property>
</bean>

<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="connectionFactory" >
<!--以下针对各种数据进行序列化方式的选择-->
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<!--<property name="hashValueSerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>-->
</bean>
</beans>

 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

redis.properties配置文件

#redis setting
redis.maxIdle=300
redis.minIdle=100
redis.maxWaitMillis=3000
redis.testOnBorrow=true
redis.maxTotal=500
redis.host=127.0.0.1
redis.port=6379
redis.password=

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

redis操作抽象类    jedis

package cn.mybatis.Util.redis;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;

import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* Created by cjq on 2017/12/05.
*/

public abstract class IRedisService<T> {

@Autowired
protected RedisTemplate<String, Object> redisTemplate;

/* @Resource
protected HashOperations<String, String, T> hashOperations;*/

/**
* 存入redis中的key
*
* @return
*/
protected abstract String getRedisKey();

/**
* 添加
*
* @param key key
* @param doamin 对象
* @param expire 过期时间(单位:秒),传入 -1 时表示不设置过期时间
*/
public void put(String key, T doamin, long expire) {
redisTemplate.opsForHash().put(getRedisKey(), key, doamin);
if (expire != -1) {
redisTemplate.expire(getRedisKey(), expire, TimeUnit.SECONDS);
}
}

/**
* 删除
*
* @param key 传入key的名称
*/
public void remove(String key) {
redisTemplate.opsForHash().delete(getRedisKey(), key);
}

/**
* 查询
*
* @param key 查询的key
* @return
*/
public Object get(String key) {
return redisTemplate.opsForHash().get(getRedisKey(), key);
}

/**
* 获取当前redis库下所有对象
*
* @return
*/
public List<Object> getAll() {
return redisTemplate.opsForHash().values(getRedisKey());
}

/**
* 查询查询当前redis库下所有key
*
* @return
*/
public Set<Object> getKeys() {
return redisTemplate.opsForHash().keys(getRedisKey());
}

/**
* 判断key是否存在redis中
*
* @param key 传入key的名称
* @return
*/
public boolean isKeyExists(String key) {
return redisTemplate.opsForHash().hasKey(getRedisKey(), key);
}

/**
* 查询当前key下缓存数量
*
* @return
*/
public long count() {
return redisTemplate.opsForHash().size(getRedisKey());
}

/**
* 清空redis
*/
public void empty() {
Set<Object> set = redisTemplate.opsForHash().keys(getRedisKey());
for(Object key : set){
redisTemplate.opsForHash().delete(getRedisKey(), key);
}

}
}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

redis抽象类的子类作为service使用   rediskey识别一个redis库

package cn.mybatis.Util.redis;
import org.springframework.stereotype.Service;
import cn.mybatis.Model.User;
/**
* Created by cjq on 2017/12/05.
*
*/
@Service
public class UserRedisServiceImpl extends IRedisService<User>{
private String redisKey = "UserRedisData";

public String getRedisKey() {
return this.redisKey;
}

}

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

实例中的使用  

注入抽象类子类

@Autowired
private UserRedisServiceImpl userRedisServiceImpl;

存入对象数据

userRedisServiceImpl.put(user.getAccount(), user, -1);

清空所有数据

userRedisServiceImpl.empty();

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

question1:

 

redis配置文件应尽早加载   不然会读取不到properties配置的值;quarzt配置文件应尽早加载,不然注解的定时任务无法启动

 

question2:

 

 

 clean 项目重新部署一次启动成功

question3:普通类无法使用注入,否则为null,只有spring容器下的类才可以使用注入形式

posted @ 2017-12-05 10:46  一缕清风丶  阅读(1147)  评论(0编辑  收藏  举报