liuyazhuang 通过本文主要向大家介绍了spring data redis,spring boot redis,spring整合redis,spring session redis,springmvc redis等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
做过大型软件系统的同学都知道,随着系统数据越来越庞大,越来越复杂,随之带来的问题就是系统性能越来越差,尤其是频繁操作数据库带来的性能损耗更为严重。很多业绩大牛为此提出了众多的解决方案和开发了很多框架以优化这种频繁操作数据库所带来的性能损耗,其中,尤为突出的两个缓存服务器是Memcached和Redis。今天,我们不讲Memcached和Redis本身,这里主要为大家介绍如何使spring与Redis整合。
1、pom构建
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.x.redis</groupId> <artifactId>springredis</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>3.1.2.RELEASE</version> <scope>test</scope> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.1.0</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> <scope>test</scope> </dependency> </dependencies> </project></div>
2、spring配置文件(applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:redis.properties" />
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxActive" value="${redis.maxActive}" />
<property name="maxWait" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</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>
<bean id="userDao" class="com.lyz.dao.impl.UserDaoImpl" />
</beans>
</div>
3、redis.properties
# Redis settings redis.host=192.168.157.130 redis.port=6379 redis.pass=liuyazhuang redis.maxIdle=300 redis.maxActive=600 redis.maxWait=1000 redis.testOnBorrow=true</div>
4、User实体类
package com.lyz.entity;
import java.io.Serializable;
/**
* user实体类
* @author liuyazhuang
*
*/
public class User implements Serializable {
private static final long serialVersionUID = -6011241820070393952L;
private String id;
private String name;
private String password;
public User() {
}
public User(String id, String name, String password) {
super();
this.id = id;
this.name = name;
this.password = password;
}
/**
* 获得id
* @return the id
*/
public String getId() {
return id;
}
/**
* 设置id
* @param id the id to set
*/
public void setId(String id) {
this.id = id;
}
/**
* 获得name
* @return the name
*/
public String getName() {
return name;
}
/**
* 设置name
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* 获得password
* @return the password
*/
public String getPassword() {
return password;
}
/**
* 设置password
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
}
</div>
5、User操作的接口IUserDao
package com.lyz.dao;
import java.util.List;
import com.lyz.entity.User;
/**
* user操作接口
* @author liuyazhuang
*
*/
public interface IUserDao {
/**
* 新增
* @param user
* @return
*/
boolean add(User user);
/**
* 批量新增 使用pipeline方式
* @param list
* @return
*/
boolean add(List<User> list);
/**
* 删除
* @param key
*/
void delete(String key);
/**
* 删除多个
* @param keys
*/
void delete(List<String> keys);
/**
* 修改
* @param user
* @return
*/
boolean update(User user);
/**
* 通过key获取
* @param keyId
* @return
*/
User get(String keyId);
}
</div>
6、基本的抽象类
package com.lyz.dao.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
/**
* 基本的抽象类
* @author liuyazhuang
*
* @param <K>
* @param <V>
*/
public abstract class AbstractBaseRedisDao<K, V> {
@Autowired
protected RedisTemplate<K, V> redisTemplate;
/**
* 设置redisTemplate
* @param redisTemplate the redisTemplate to set
*/
public void setRedisTemplate(RedisTemplate<K, V> redisTemplate) {
this.redisTemplate = redisTemplate;
}
/**
* 获取 RedisSerializer
* <br>------------------------------<br>
*/
protected RedisSerializer<String> getRedisSerializer() {
return redisTemplate.getStringSerializer();
}
}
</div>
7、IUserDao的实现类UserDaoImpl
package com.lyz.dao.impl;
import java.util.ArrayList;
import java.util.List;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.util.Assert;
import com.lyz.dao.IUserDao;
import com.lyz.entity.User;
/**
* 接口的实现类
* @author liuyazhuang
*
*/
public class UserDaoImpl extends AbstractBaseRedisDao<String, User> implements IUserDao {
/**

