ssm框架整合redis

第一步:导入redis需要的jar包

                
		
			org.springframework.data
			spring-data-redis
			1.5.0.RELEASE
		
		
			redis.clients
			jedis
			2.9.0
		

		
			org.apache.commons
			commons-pool2
			2.0
		

		
			commons-pool
			commons-pool
			1.3
		

第二步:在application-database.xml文件中将redis整合进来

        
        
		

		
			
		
		
			
		

		
			
		
		
			
		
	

第三步:创建redis需要的工具类

RedisCache.java

package com.runtai.redis;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import org.apache.ibatis.cache.Cache;
import org.springframework.beans.factory.annotation.Autowired;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class RedisCache implements Cache {

	private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
	/**
	 * Jedis客户端
	 */

	@Autowired
	private Jedis redisClient = createClient();

	private String id;

	public RedisCache(final String id) {
		if (id == null) {
			throw new IllegalArgumentException("必须传入ID");
		}
		System.out.println("MybatisRedisCache:id=" + id);
		this.id = id;
	}

	@Override
	public void clear() {
		redisClient.flushDB();
	}

	@Override
	public String getId() {
		return this.id;
	}

	@Override
	public Object getObject(Object key) {
		byte[] ob = redisClient.get(SerializeUtil.serialize(key.toString()));
		if (ob == null) {
			return null;
		}
		Object value = SerializeUtil.unSerialize(ob);
		return value;
	}

	@Override
	public ReadWriteLock getReadWriteLock() {
		return readWriteLock;
	}

	@Override
	public int getSize() {
		return Integer.valueOf(redisClient.dbSize().toString());
	}

	@Override
	public void putObject(Object key, Object value) {
		redisClient.set(SerializeUtil.serialize(key.toString()), SerializeUtil.serialize(value));
	}

	@Override
	public Object removeObject(Object key) {
		return redisClient.expire(SerializeUtil.serialize(key.toString()), 0);
	}

	protected static Jedis createClient() {

		try {
			@SuppressWarnings("resource")
			JedisPool pool = new JedisPool(new JedisPoolConfig(), "127.0.0.1", 6379);
			return pool.getResource();
		} catch (Exception e) {
			e.printStackTrace();
		}
		throw new RuntimeException("初始化连接池错误");
	}
}

SerializeUtil.java

package com.runtai.redis;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SerializeUtil {
	/**
	 *
	 * 序列化
	 */
	public static byte[] serialize(Object obj) {

		ObjectOutputStream oos = null;
		ByteArrayOutputStream baos = null;

		try {
			// 序列化
			baos = new ByteArrayOutputStream();
			oos = new ObjectOutputStream(baos);

			oos.writeObject(obj);
			byte[] byteArray = baos.toByteArray();
			return byteArray;

		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 *
	 * 反序列化
	 *
	 * @param bytes
	 * @return
	 */
	public static Object unSerialize(byte[] bytes) {

		ByteArrayInputStream bais = null;

		try {
			// 反序列化为对象
			bais = new ByteArrayInputStream(bytes);
			ObjectInputStream ois = new ObjectInputStream(bais);
			return ois.readObject();

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}

第四步:将pojp也就是实体类序列化

package com.runtai.entity;

import java.io.Serializable;

//实现redis得继承Serializable接口进行序列化
public class User implements Serializable{
    //定义一个序列号
    private static final long serialVersionUID = 1L;
	
    private Integer id;

    private String name;
    
    private String password;
    
    private String headPhoto; //头像图片
    
    private String src; //头像图片
    
    private String kind;
    
    private String createTime;
    
    private Integer status;//1正常2删除

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name == null ? null : name.trim();
    }

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getHeadPhoto() {
		return headPhoto;
	}

	public void setHeadPhoto(String headPhoto) {
		this.headPhoto = headPhoto;
	}

	public String getKind() {
		return kind;
	}

	public void setKind(String kind) {
		this.kind = kind;
	}

	public String getCreateTime() {
		return createTime;
	}

	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}

	public String getSrc() {
		return src;
	}

	public void setSrc(String src) {
		this.src = src;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}
	
	
}

第五步:在UserMapper.xml里面加入




	
	
	    
	    
	    
	    
	
    	
    
  
    
    
    
        update t_adminuser
        
            password = #{newpass} 
        
        
            id = #{userId}
        
    


你可能感兴趣的:(redis)