您的位置:首页 > 数据库 > Redis

使用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);

}

 

 

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: