第一步:导入redis需要的jar包
<!--redis-->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.5.0.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.3</version>
</dependency>
第二步:在application-database.xml文件中将redis整合进来
<!-- redis操作模板,这里采用尽量面向对象的模板 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="keySerializer">
<bean
class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer">
<bean
class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
</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>
第三步:创建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里面加入<cache type="com.runtai.redis.RedisCache" />
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.runtai.mapper.UserMapper" >
<cache type="com.runtai.redis.RedisCache" />
<resultMap type="com.runtai.entity.User" id="user">
<id column="id" property="id" jdbcType="INTEGER"/>
<result column="name" property="name" jdbcType="VARCHAR"/>
<result column="password" property="password" jdbcType="VARCHAR"/>
<result column="head_photo" property="headPhoto" jdbcType="VARCHAR"/>
</resultMap>
<select id="findUserByName" resultMap="user">
select
id, name, password ,head_photo as headPhoto,kind
from
t_adminuser
<where>
name = #{name}
</where>
</select>
<select id="findUserByid" resultMap="user">
select
id, name, password, head_photo as headPhoto
from
t_adminuser
<where>
id = #{userId}
</where>
</select>
<update id="updatePwdByid">
update t_adminuser
<set>
password = #{newpass}
</set>
<where>
id = #{userId}
</where>
</update>
</mapper>