j2ee spring 整合redis2.6

原创
2014/12/22 11:06
阅读数 7.4K

废话不多说,先贴上代码链接: http://git.oschina.net/alexgaoyh/alexgaoyh

之后项目效果部分如下图:


附上redis 在win32环境下的数据查询显示图:


下面附上部分pom.xml代码:


<dependency>  
	        <groupId>org.springframework</groupId>  
	        <artifactId>spring-core</artifactId>  
	        <version>4.0.2.RELEASE</version>  
	    </dependency>
	    <dependency>  
	        <groupId>org.springframework.data</groupId>  
	        <artifactId>spring-data-redis</artifactId>  
	        <version>1.4.0.RELEASE</version>  
	    </dependency>
		<dependency>  
	        <groupId>redis.clients</groupId>  
	        <artifactId>jedis</artifactId>  
	        <version>2.6.0</version>  
	    </dependency>
		<dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.0</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>1.6.1</version>
        </dependency>
	<dependency>
		<groupId>org.slf4j</groupId>
		<artifactId>slf4j-log4j12</artifactId>
		<version>1.7.0</version>
	</dependency>
redis-config.xml
<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-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 ">

  <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
  </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>
redis-config.properties



redis.host=127.0.0.1
redis.port=6379
redis.pass=
备注: 引入*.properties 文件的方法是:



<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:*-config.properties</value>
        </list>
    </property>
</bean>

创建redisClient的java类:

package com.alexgaoyh.redis.util;

import java.io.Serializable;

import javax.annotation.Resource;

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;
import org.springframework.stereotype.Component;

@Component("redisClient")
public class RedisClient<K extends Serializable, V extends Serializable> {

	@Resource(name="redisTemplate")
    protected RedisTemplate<K, V> redisTemplate;
	
	/**
	 * 添加
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean add(final String key, final String value) {
		boolean resultBoolean = false;
		if(redisTemplate != null) {
			resultBoolean = redisTemplate.execute(new RedisCallback<Boolean>() {  
				public Boolean doInRedis(RedisConnection connection)  
						throws DataAccessException {  
					RedisSerializer<String> serializer = getRedisSerializer();
					byte[] keys  = serializer.serialize(key);
					byte[] values = serializer.serialize(value);  
					return connection.setNX(keys, values);  
				}  
			});  
		}else{
			System.out.println(redisTemplate == null);
		}
		return resultBoolean;
    }
	
	/**
     * 根据key获取对象
     */
    public String get(final String key) {
    	String resultStr = null;
    	if(redisTemplate != null) {
    		resultStr = redisTemplate.execute(new RedisCallback<String>() {
    			public String doInRedis(RedisConnection connection)  
    					throws DataAccessException {  
    				RedisSerializer<String> serializer = getRedisSerializer();  
    				byte[] keys = serializer.serialize(key);  
    				byte[] values = connection.get(keys);  
    				if (values == null) {  
    					return null;  
    				}  
    				String value = serializer.deserialize(values);  
    				return value;  
    			}  
    		});  
    	}
        return resultStr;  
    }  
	
	 /** 
     * 获取 RedisSerializer 
     * 
     */  
    protected RedisSerializer<String> getRedisSerializer() {  
        return redisTemplate.getStringSerializer();  
    }  

	public RedisTemplate<K, V> getRedisTemplate() {
		return redisTemplate;
	}

	public void setRedisTemplate(RedisTemplate<K, V> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}
	
}
测试方法:
@Resource
	private RedisClient<String, String> redisClient;
	
	
    @RequestMapping(value="login")  
    public ModelAndView login(){
    	//发送邮件demo
    	//EmailUtil.send("subject", "content", "924099504@qq.com");
    	redisClient.add("aaaa", "aaaa");
    	System.out.println(redisClient.get("aaaa"));
        return new ModelAndView("views/test");
    }
采用注解方式创建bean:
<context:component-scan base-package="com.alexgaoyh" />


reids win32 win64环境下服务端

http://git.oschina.net/alexgaoyh/alexgaoyh/attach_files


展开阅读全文
加载中

作者的其它热门文章

打赏
1
15 收藏
分享
打赏
0 评论
15 收藏
1
分享
返回顶部
顶部