SSM 集成Redis

ssm 集成 redis 做缓存

1.配置 文件redis.properties

#redis pool config
redis.pool.maxActive=200
redis.pool.maxIdle=100
redis.pool.maxWaitMillis=100
redis.pool.testOnBorrow=true

#redis config
redis.host=127.0.0.1 //连接地址
redis.port=6379  //默认端口
redis.timeout=2000
redis.password= //我这里没有密码
redis.database=0  //第几个库
redis.dbindex=8
redis.usePool=1
redis.default.expire=1800000

2.spring-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:aop="http://www.springframework.org/schema/aop"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    
	<!-- redis 配置数据 -->
	<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> 
        <property name="maxIdle" value="${redis.pool.maxIdle}"></property> 
        <property name="maxTotal" value="${redis.pool.maxActive}" />
        <property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}" />
        <property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
    </bean> 
	<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy"> 
        <property name="poolConfig" ref="jedisPoolConfig"/>
		<property name="hostName" value="${redis.host}"/> 
		<property name="port" value="${redis.port}"/>
		<property name="database" value="${redis.database}"/>
     <!-- <property name="password" value="${redis.password}"></property>  -->
		<property name="timeout" value="${redis.timeout}"></property> 
 	</bean> 
	<!-- 配置redisTemplate 模版 -->
	<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory" />
        <property name="keySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
        <property name="valueSerializer">
            <bean
                class="org.springframework.data.redis.serializer.StringRedisSerializer">
            </bean>
        </property>
    </bean>
</beans>
 

3.redis service  我这里没有采用注解形式的

package com.cn.ag.redis;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

public class RedisService {
	@Autowired
	private RedisTemplate<String, String> redisTemplate;
	private static final Logger log=Logger.getLogger(RedisService.class);

	/**
	 * 
	 * @Title: put
	 * @Description: 插入
	 * @param @param keyId
	 * @param @param entity
	 * @param @return    设定文件
	 * @return boolean    返回类型
	 * @date 2018年12月27日 下午2:09:37
	 * @throws
	 */
	public boolean put(String keyId, String entity) {
		boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
			public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
				RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
				byte[] key = serializer.serialize(keyId);
				byte[] value = serializer.serialize(entity);
				return connection.setNX(key, value);
			}
		});
		log.info("【插入数据成功】");
		return result;
	}

	/**
	 * 
	 * @Title: remove
	 * @Description: 删除
	 * @param @param key    设定文件
	 * @return void    返回类型
	 * @date 2018年12月27日 下午2:10:53
	 * @throws
	 */
	public void remove(String key) {
		redisTemplate.delete(key);
		log.info("【删除数据成功】");
	}

	/**
	 * 
	 * @Title: get
	 * @Description: 查询
	 * @param @param keyId
	 * @param @return    设定文件
	 * @return String    返回类型
	 * @date 2018年12月27日 下午2:10:31
	 * @throws
	 */
	public String get(final String keyId) {
		String entity = redisTemplate.execute(new RedisCallback<String>() {
			public String doInRedis(RedisConnection connection) throws DataAccessException {
				RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
				byte[] key = serializer.serialize(keyId);
				byte[] value = connection.get(key);
				if (value == null) {
					return null;
				}
				return serializer.deserialize(value);
			}
		});
		log.info("【查询数据成功】");
		return entity;
	}
}

4.Controller 调用

package com.cn.ag.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cn.ag.redis.RedisService;

@Controller
@RequestMapping("/test")
public class Test {
	
	@Autowired
	private RedisService redisService;
	
	@RequestMapping(value = "/test", produces = "application/json")
	@ResponseBody
	public String create() {
        //简单查询
		return redisService.get("k1");
	}
	
}

猜你喜欢

转载自blog.csdn.net/qq_36514766/article/details/85395328
今日推荐