使用spring测试Redis字符串操作
2018-08-16 20:37
169 查看
1.pom依赖
[code]<dependencies> <!-- https://mvnrepository.com/artifact/redis.clients/jedis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.7.2.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.2.6.RELEASE</version> </dependency> </dependencies>
2.配置文件applicationContext.xml
[code]<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd"> <!--spring的配置代码 --> <!--首先配置JedisPoolConfig对象 --> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <!-- 最大空闲数 --> <property name="maxIdle" value="100"/> <!-- 最大连接数 --> <property name="maxTotal" value="200"/> <!-- 最大等待时间 --> <property name="maxWaitMillis" value="20000"/> </bean> <!--配置JedisConnectionFactory --> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="127.0.0.1"/> <property name="port" value="6379"/> <!-- <property name="password" value="123456"/> --> <property name="poolConfig" ref="poolConfig"/> </bean> <!-- 配置spring RedisTemplate --> <!-- <bean id="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/> --> <bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="connectionFactory"/> <!--键序列器 --> <property name="keySerializer" ref="stringRedisSerializer"/> <!-- 值序列器--> <property name="valueSerializer" ref="stringRedisSerializer"/> </bean> </beans>
3.测试代码,运行结果(测试之前,记得开启redis服务)
[code]@Test public void funString() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); RedisTemplate redisTemplate = applicationContext.getBean(RedisTemplate.class); redisTemplate.opsForValue().set("key1", "value1"); redisTemplate.opsForValue().set("key2", "value2"); //除了delete操作,其他操作都是通过opsForValue()得到valueOps去操作的。 //通过key获取值 String value1 = (String) redisTemplate.opsForValue().get("key1"); System.out.println(value1); //通过key删除值 redisTemplate.delete("key1"); //求长度 Long length = redisTemplate.opsForValue().size("key2"); System.out.println(length); //设置新值返回旧值 String oldvalue2 = (String)redisTemplate.opsForValue().getAndSet("key2", "new_value2"); System.out.println(oldvalue2); //求子串 String rangeValue2 = redisTemplate.opsForValue().get("key2", 0, 3); System.out.println(rangeValue2); //追加字符串到末尾,返回新字符串长度 int newlength = redisTemplate.opsForValue().append("key2", "_append"); System.out.println(newlength); }
阅读更多
相关文章推荐
- 使用Spring-data-redis操作Redis的Sentinel
- 使用Spring-data-redis操作Redis的Sentinel
- jedis与spring整合及简单的使用RedisTemplate操作
- 使用Java(Springboot)操作Redis
- Spring整合redis使用Java操作redis
- 使用Java(Springboot)操作Redis
- 使用Java(Springboot)操作Redis
- 深入理解Spring Redis的使用 (九)、通过Redis 实现 分布式锁 的 BUG,以及和数据库加锁的性能测试
- spring boot 整合 redis,使用@Cacheable,@CacheEvict,@CachePut,jedisPool操作redis数据库
- 使用spring-data-redis进行对redis的操作,封装的一些操作方法
- 使用Java(Springboot)操作Redis
- Spring中使用RedisTemplate操作Redis(spring-data-redis)
- SpringBoot之Mybatis操作中使用Redis做缓存
- 使用spring-data-redis操作redis
- Spring RedisTemplate操作-序列化性能测试(12)
- 深入理解Spring Redis的使用 (九)、通过Redis 实现 分布式锁 的 BUG,以及和数据库加锁的性能测试
- Spring中使用RedisTemplate操作Redis(spring-data-redis)
- 使用Java(Springboot)操作Redis
- 使用Java(Springboot)操作Redis
- Springmvc+Redis使用jedisTemplete来操作redis的相关配置和代码示例