首页 > 代码库 > j2ee spring 整合redis2.6

j2ee spring 整合redis2.6

废话不多说,先贴上代码链接: 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 = http://www.mamicode.com/serializer.deserialize(values);  > 测试方法: 
@Resource
	private RedisClient<String, String> redisClient;
	
	
    @RequestMapping(value="http://www.mamicode.com/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


j2ee spring 整合redis2.6