pom构建:
- <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>
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.x.dao.impl.UserDao" />
- </beans>
redis.properties
- # Redis settings
- redis.host=localhost
- redis.port=6379
- redis.pass=java2000_wl
- redis.maxIdle=300
- redis.maxActive=600
- redis.maxWait=1000
- redis.testOnBorrow=true
Java代码:
- package com.x.entity;
- import java.io.Serializable;
- /**
- * @author http://blog.csdn.net/java2000_wl
- * @version <b>1.0</b>
- */
- public class User implements Serializable {
- private static final long serialVersionUID = -6011241820070393952L;
- private String id;
- private String name;
- private String password;
- /**
- * <br>------------------------------<br>
- */
- public User() {
- }
- /**
- * <br>------------------------------<br>
- */
- 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;
- }
- }
- package com.x.dao;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.data.redis.core.RedisTemplate;
- import org.springframework.data.redis.serializer.RedisSerializer;
- /**
- * AbstractBaseRedisDao
- * @author http://blog.csdn.net/java2000_wl
- * @version <b>1.0</b>
- */
- 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();
- }
- }
- package com.x.dao;
- import java.util.List;
- import com.x.entity.User;
- /**
- * @author http://blog.csdn.net/java2000_wl
- * @version <b>1.0</b>
- */
- public interface IUserDao {
- /**
- * 新增
- * <br>------------------------------<br>
- * @param user
- * @return
- */
- boolean add(User user);
- /**
- * 批量新增 使用pipeline方式
- * <br>------------------------------<br>
- * @param list
- * @return
- */
- boolean add(List<User> list);
- /**
- * 删除
- * <br>------------------------------<br>
- * @param key
- */
- void delete(String key);
- /**
- * 删除多个
- * <br>------------------------------<br>
- * @param keys
- */
- void delete(List<String> keys);
- /**
- * 修改
- * <br>------------------------------<br>
- * @param user
- * @return
- */
- boolean update(User user);
- /**
- * 通过key获取
- * <br>------------------------------<br>
- * @param keyId
- * @return
- */
- User get(String keyId);
- }
- package com.x.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.x.dao.AbstractBaseRedisDao;
- import com.x.dao.IUserDao;
- import com.x.entity.User;
- /**
- * Dao
- * @author http://blog.csdn.net/java2000_wl
- * @version <b>1.0</b>
- */
- public class UserDao extends AbstractBaseRedisDao<String, User> implements IUserDao {
- /**
- * 新增
- *<br>------------------------------<br>
- * @param user
- * @return
- */
- public boolean add(final User user) {
- boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
- public Boolean doInRedis(RedisConnection connection)
- throws DataAccessException {
- RedisSerializer<String> serializer = getRedisSerializer();
- byte[] key = serializer.serialize(user.getId());
- byte[] name = serializer.serialize(user.getName());
- return connection.setNX(key, name);
- }
- });
- return result;
- }
- /**
- * 批量新增 使用pipeline方式
- *<br>------------------------------<br>
- *@param list
- *@return
- */
- public boolean add(final List<User> list) {
- Assert.notEmpty(list);
- boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
- public Boolean doInRedis(RedisConnection connection)
- throws DataAccessException {
- RedisSerializer<String> serializer = getRedisSerializer();
- for (User user : list) {
- byte[] key = serializer.serialize(user.getId());
- byte[] name = serializer.serialize(user.getName());
- connection.setNX(key, name);
- }
- return true;
- }
- }, false, true);
- return result;
- }
- /**
- * 删除
- * <br>------------------------------<br>
- * @param key
- */
- public void delete(String key) {
- List<String> list = new ArrayList<String>();
- list.add(key);
- delete(list);
- }
- /**
- * 删除多个
- * <br>------------------------------<br>
- * @param keys
- */
- public void delete(List<String> keys) {
- redisTemplate.delete(keys);
- }
- /**
- * 修改
- * <br>------------------------------<br>
- * @param user
- * @return
- */
- public boolean update(final User user) {
- String key = user.getId();
- if (get(key) == null) {
- throw new NullPointerException("数据行不存在, key = " + key);
- }
- boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
- public Boolean doInRedis(RedisConnection connection)
- throws DataAccessException {
- RedisSerializer<String> serializer = getRedisSerializer();
- byte[] key = serializer.serialize(user.getId());
- byte[] name = serializer.serialize(user.getName());
- connection.set(key, name);
- return true;
- }
- });
- return result;
- }
- /**
- * 通过key获取
- * <br>------------------------------<br>
- * @param keyId
- * @return
- */
- public User get(final String keyId) {
- User result = redisTemplate.execute(new RedisCallback<User>() {
- public User doInRedis(RedisConnection connection)
- throws DataAccessException {
- RedisSerializer<String> serializer = getRedisSerializer();
- byte[] key = serializer.serialize(keyId);
- byte[] value = connection.get(key);
- if (value == null) {
- return null;
- }
- String name = serializer.deserialize(value);
- return new User(keyId, name, null);
- }
- });
- return result;
- }
- }
- import java.util.ArrayList;
- import java.util.List;
- import junit.framework.Assert;
- import org.junit.Test;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.test.context.ContextConfiguration;
- import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
- import com.x.dao.IUserDao;
- import com.x.entity.User;
- /**
- * 测试
- * @author http://blog.csdn.net/java2000_wl
- * @version <b>1.0</b>
- */
- @ContextConfiguration(locations = {"classpath*:applicationContext.xml"})
- public class RedisTest extends AbstractJUnit4SpringContextTests {
- @Autowired
- private IUserDao userDao;
- /**
- * 新增
- * <br>------------------------------<br>
- */
- @Test
- public void testAddUser() {
- User user = new User();
- user.setId("user1");
- user.setName("java2000_wl");
- boolean result = userDao.add(user);
- Assert.assertTrue(result);
- }
- /**
- * 批量新增 普通方式
- * <br>------------------------------<br>
- */
- @Test
- public void testAddUsers1() {
- List<User> list = new ArrayList<User>();
- for (int i = 10; i < 50000; i++) {
- User user = new User();
- user.setId("user" + i);
- user.setName("java2000_wl" + i);
- list.add(user);
- }
- long begin = System.currentTimeMillis();
- for (User user : list) {
- userDao.add(user);
- }
- System.out.println(System.currentTimeMillis() - begin);
- }
- /**
- * 批量新增 pipeline方式
- * <br>------------------------------<br>
- */
- @Test
- public void testAddUsers2() {
- List<User> list = new ArrayList<User>();
- for (int i = 10; i < 1500000; i++) {
- User user = new User();
- user.setId("user" + i);
- user.setName("java2000_wl" + i);
- list.add(user);
- }
- long begin = System.currentTimeMillis();
- boolean result = userDao.add(list);
- System.out.println(System.currentTimeMillis() - begin);
- Assert.assertTrue(result);
- }
- /**
- * 修改
- * <br>------------------------------<br>
- */
- @Test
- public void testUpdate() {
- User user = new User();
- user.setId("user1");
- user.setName("new_password");
- boolean result = userDao.update(user);
- Assert.assertTrue(result);
- }
- /**
- * 通过key删除单个
- * <br>------------------------------<br>
- */
- @Test
- public void testDelete() {
- String key = "user1";
- userDao.delete(key);
- }
- /**
- * 批量删除
- * <br>------------------------------<br>
- */
- @Test
- public void testDeletes() {
- List<String> list = new ArrayList<String>();
- for (int i = 0; i < 10; i++) {
- list.add("user" + i);
- }
- userDao.delete(list);
- }
- /**
- * 获取
- * <br>------------------------------<br>
- */
- @Test
- public void testGetUser() {
- String id = "user1";
- User user = userDao.get(id);
- Assert.assertNotNull(user);
- Assert.assertEquals(user.getName(), "java2000_wl");
- }
- /**
- * 设置userDao
- * @param userDao the userDao to set
- */
- public void setUserDao(IUserDao userDao) {
- this.userDao = userDao;
- }
- }
相关推荐
本文将深入探讨如何将Spring与Redis进行整合,为新手提供一个简单易懂的实践指南。 一、Spring与Redis整合的背景及优势 Spring框架提供了丰富的集成支持,使得与其他技术的整合变得非常方便。整合Spring和Redis...
总的来说,Spring整合Redis使得在Java应用中使用Redis变得更加便捷,无论是简单的字符串操作还是复杂的对象存储,都能通过Spring提供的工具轻松实现。通过上述步骤,你可以构建一个基本的Spring-Redis集成系统,...
Spring整合Redis项目是一种常见的数据存储和缓存解决方案,特别是在高并发、大数据量的Web应用中。Redis是一款开源的、高性能的键值对数据库,而Spring是Java领域广泛使用的框架,提供了一整套的企业级应用开发工具...
Spring整合Redis完整实例代码,下载经简单的修改配置文件,即可运行,代码中有详细的注释,关于代码的详情可参见博文:http://blog.csdn.net/l1028386804/article/details/52108758
总结来说,Spring整合Redis哨兵模式涉及了Spring Boot的配置、哨兵系统的理解、连接工厂的创建、数据序列化、CRUD操作以及异常处理等多个知识点。通过这样的整合,我们可以构建出高可用、容错性强的Redis服务,为...
Spring 整合 Redis 是在 Java 开发中常用的一种技术,用于提升应用的缓存和数据处理性能。Redis 是一个开源的、基于键值对的数据存储系统,常被用作数据库、缓存和消息中间件。它支持多种数据结构,如字符串、哈希、...
以上就是关于Spring整合Redis的详细解析,涵盖了Spring模板、连接池、JSON序列化和Redis集群的配置与使用。在实际项目中,理解并熟练掌握这些知识点,能帮助我们构建出高效、稳定的Redis集成方案。
本文将详细介绍如何在Spring MVC中整合Redis,并利用连接池来优化性能。 首先,我们需要引入Redis的相关依赖。在Maven项目中,可以在pom.xml文件中添加`spring-data-redis`和`jedis`库,这两个库分别提供了Spring对...
**Spring整合Redis哨兵** 在高可用性系统中,Redis Sentinel是至关重要的组件,它提供了主从监控、故障检测和自动故障转移的功能。Spring作为Java生态中的主流框架,提供了与Redis Sentinel集成的能力,使得我们...
本文将深入探讨如何在Spring项目中整合Redis,以及在测试过程中如何使用JUnit进行单元测试。 1. **Spring Data Redis** Spring Data Redis是Spring Framework的一个模块,它提供了对Redis数据库的抽象层,使得...
至此,你已经掌握了Spring整合Redis作为缓存的基本步骤。这种方式能够有效地提高应用程序的性能,减少对数据库的访问,同时利用Redis的高速读写能力。在实际开发中,还可以根据需求调整序列化策略、缓存策略等,以...
在本案例中,我们将深入探讨如何在Spring框架中整合Redis数据存储系统,使用Jedis作为客户端库。Redis是一个高性能的键值对存储系统,适用于缓存、消息队列等多种应用场景。而Spring是Java领域广泛使用的应用程序...
期末向Spring整合Redis源码
spring整合redis(spring模板+连接池+json序列化+cluster集群)
总结来说,Spring整合Redis的目的是优化数据存取,提高应用性能。通过配置RedisTemplate、连接池和序列化策略,我们可以实现高效、灵活的Redis操作。在这个非集群环境中,我们专注于单实例的使用,但在生产环境中,...
SpringCloud整合Redis缓存;版本:SpringCloud :Dalston.SR4;SpringBoot:1.5.14.RELEASE;Redis:3.2.0;Maven :3.5.3.代码下载下来即可用
"Spring3.0整合redis相关jar"这个主题主要涉及的是如何在Spring 3.0版本中集成Redis作为数据存储或缓存解决方案。Spring 3.0虽然相对较老,但在当时是广泛使用的版本,因此了解其与Redis的集成方式对维护旧项目或...