spring整合redis详细步骤代码

mac2026-04-12  4

1.依赖

集中定义依赖版本号

<properties> <spring.version>4.2.4.RELEASE</spring.version> </properties> <dependencies> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> </dependency> <!-- 缓存 --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.8.1</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.7.2.RELEASE</version> </dependency> </dependencies> <build> <plugins> <!-- java编译插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build>

2.配置文件

applicationContext-redis.xml 文件 在 spring文件夹下

<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:cache="http://www.springframework.org/schema/cache" 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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> <context:property-placeholder location="classpath*:properties/*.properties" /> <!-- redis 相关配置 --> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="${redis.maxIdle}" /> <property name="maxWaitMillis" value="${redis.maxWait}" /> <property name="testOnBorrow" value="${redis.testOnBorrow}" /> </bean> <bean id="JedisConnectionFactory" 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.RedisTemplate"> <property name="connectionFactory" ref="JedisConnectionFactory" /> </bean> </beans>

redis-config.properties文件 在properties文件夹下 注意:改端口号

# Redis settings # server IP redis.host=192.168.25.130 # server port redis.port=6379 # server pass redis.pass= # use dbIndex redis.database=0 # \u63A7\u5236\u4E00\u4E2Apool\u6700\u591A\u6709\u591A\u5C11\u4E2A\u72B6\u6001\u4E3Aidle(\u7A7A\u95F2\u7684)\u7684jedis\u5B9E\u4F8B redis.maxIdle=300 # \u8868\u793A\u5F53borrow(\u5F15\u5165)\u4E00\u4E2Ajedis\u5B9E\u4F8B\u65F6\uFF0C\u6700\u5927\u7684\u7B49\u5F85\u65F6\u95F4\uFF0C\u5982\u679C\u8D85\u8FC7\u7B49\u5F85\u65F6\u95F4(\u6BEB\u79D2)\uFF0C\u5219\u76F4\u63A5\u629B\u51FAJedisConnectionException\uFF1B redis.maxWait=3000 # \u5728borrow\u4E00\u4E2Ajedis\u5B9E\u4F8B\u65F6\uFF0C\u662F\u5426\u63D0\u524D\u8FDB\u884Cvalidate\u64CD\u4F5C\uFF1B\u5982\u679C\u4E3Atrue\uFF0C\u5219\u5F97\u5230\u7684jedis\u5B9E\u4F8B\u5747\u662F\u53EF\u7528\u7684 redis.testOnBorrow=true

3.代码

String类型 package cn.wys.test; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:spring/applicationContext-redis.xml") public class StringTest { @Autowired private RedisTemplate redisTemplate; @Test //添加 public void add(){ //key相同修改 redisTemplate.boundValueOps("Str1").set("哈哈"); } @Test //查询 public void select(){ String str1 = (String) redisTemplate.boundValueOps("Str1").get(); System.out.println(str1); } @Test//删除 public void delete(){ redisTemplate.delete("Str1"); } }

hash类型

package cn.wys.test; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.List; import java.util.Set; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:spring/applicationContext-redis.xml") public class HashTest { @Autowired private RedisTemplate redisTemplate; @Test //添加 public void add(){ //key相同修改 redisTemplate.boundHashOps("hash").put("一","你荣"); redisTemplate.boundHashOps("hash").put("二","你啊啊"); } @Test //查询 public void select(){ String o = (String) redisTemplate.boundHashOps("hash").get("一"); System.out.println(o); Set<String> hash = redisTemplate.boundHashOps("hash").keys(); for (String str : hash) { System.out.println(str+":"+redisTemplate.boundHashOps("hash").get(str)); } //遍历所有的key Set<String> keys = redisTemplate.boundHashOps("hash").keys(); for (String key : keys) { System.out.println("key="+key); } //遍历所有的value List<String> values = redisTemplate.boundHashOps("hash").values(); for (String value : values) { System.out.println("value="+value); } /* 倒计时设置 redisTemplate.boundHashOps("testHash").expire(1, TimeUnit.MINUTES ); */ } @Test//删除 public void delete(){ redisTemplate.boundHashOps("hash").delete("一"); } }

list类型

package cn.wys.test; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.List; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:spring/applicationContext-redis.xml") public class ListTest { @Autowired private RedisTemplate redisTemplate; @Test //添加 public void add(){ //key相同修改 redisTemplate.boundListOps("list1").leftPush("1"); redisTemplate.boundListOps("list1").leftPush("2"); redisTemplate.boundListOps("list1").leftPush("3"); redisTemplate.boundListOps("list1").leftPush("3"); redisTemplate.boundListOps("list1").leftPush("4"); } @Test //查询 public void select(){ // String list1 = (String) redisTemplate.boundListOps("list1").rightPop(); // System.out.println(list1); //只取值 List<String> list1 = redisTemplate.boundListOps("list1").range(0, -1); for (String str : list1) { System.out.println(str); } } @Test//删除 public void delete(){ redisTemplate.boundListOps("list1").remove(1,"3"); } }

set类型

package cn.wys.test; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.Set; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:spring/applicationContext-redis.xml") public class SetTest { @Autowired private RedisTemplate redisTemplate; @Test //添加 public void add(){ //key相同修改 redisTemplate.boundSetOps("set").add("a"); redisTemplate.boundSetOps("set").add("b"); redisTemplate.boundSetOps("set").add("c"); } @Test //查询 public void select(){ Set<String> set = redisTemplate.boundSetOps("set").members(); for (String str : set) { System.out.println(str); } } @Test//删除 public void delete(){ redisTemplate.boundSetOps("set").remove("b"); } } zset类型 package cn.wys.test; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.DefaultTypedTuple; import org.springframework.data.redis.core.RedisOperations; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.Set; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:spring/applicationContext-redis.xml") public class ZSetTest { @Autowired private RedisTemplate redisTemplate; @Test //添加 public void add(){ //key相同修改 redisTemplate.boundZSetOps("zset").add("set",5); redisTemplate.boundZSetOps("zset").add("se2",6); redisTemplate.boundZSetOps("zset").add("set3",7); redisTemplate.boundZSetOps("zset").add("set4",9); redisTemplate.boundZSetOps("zset").add("set5",10); } @Test //查询 public void select(){ //正序 Set zset = redisTemplate.boundZSetOps("zset").range(0,-1); System.out.println(zset); System.out.println(); //倒序 Set zset1 = redisTemplate.boundZSetOps("zset").reverseRange(0,-1); System.out.println(zset1); System.out.println(); //查询带分值 Set<DefaultTypedTuple> zSet1 = redisTemplate.boundZSetOps("zset").rangeWithScores(0, -1); for (DefaultTypedTuple o : zSet1) { System.out.println(o.getValue() + ":" + o.getScore()); } System.out.println(); //按分值查询 Set<DefaultTypedTuple> zSet2 = redisTemplate.boundZSetOps("zset").rangeByScoreWithScores(5, 8); for (DefaultTypedTuple o : zSet2) { System.out.println(o.getValue() + ":" + o.getScore()); } } @Test//删除 public void delete(){ redisTemplate.boundZSetOps("zset").remove("set"); } }

4.注意

在使用SpringDataRedis时注意数据一致性(增删改或其他合适时机清空缓存)

最新回复(0)