使用spring测试Redis字符串操作

1.pom依赖

<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

<?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服务)

@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);

		
	}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值