生活随笔
收集整理的這篇文章主要介紹了
Java-SpringDataRedis使用入门
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
<–start–> spring設計spring data的初衷就是為了統一持久層。spring data redis設計的目的就是為了簡化redis的操作,要使用spring data redis,分為以下幾個要點: ① 在pom文件中配置spring data redis的坐標。 ② 在spring的applicationContext.xml文件中配置jedis的連接工廠。 ③ 基于jedis的工廠構造redis的模板RedisTemplate。 ④ 將redis的模板RedisTemplate注入到程序代碼中。 ⑤ 在程序代碼中就可以通過操作RedisTemplate來操作redis。 在pom文件中引入spring data redis的坐標:
<dependency > <groupId > org.springframework.data
</groupId > <artifactId > spring-data-redis
</artifactId > <version > 1.4.1.RELEASE
</version >
</dependency >
在spring的配置文件applicationContext.xml中配置RedisTemplate。 JedisPoolConfig(連接池)?JedisConnectionFactory(連接工廠)?RedisTemplate(redis模板)?StringRedisSerializer(將key和value存儲為string類型)
<?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:context ="http://www.springframework.org/schema/context" xmlns:aop ="http://www.springframework.org/schema/aop" xmlns:tx ="http://www.springframework.org/schema/tx" xmlns:p ="http://www.springframework.org/schema/p" xmlns:jpa ="http://www.springframework.org/schema/data/jpa" xmlns:jaxws ="http://cxf.apache.org/jaxws" xmlns:cache ="http://www.springframework.org/schema/cache" xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/data/jpahttp://www.springframework.org/schema/data/jpa/spring-jpa.xsdhttp://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsdhttp://www.springframework.org/schema/cachehttp://www.springframework.org/schema/cache/spring-cache.xsd" > <bean id ="poolConfig" class ="redis.clients.jedis.JedisPoolConfig" > <property name ="maxIdle" value ="300" /> <property name ="maxWaitMillis" value ="3000" /> <property name ="testOnBorrow" value ="true" /> </bean > <bean id ="redisConnectionFactory" class ="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:host-name ="localhost" p:port ="6379" p:pool-config-ref ="poolConfig" p:database ="0" /> <bean id ="redisTemplate" class ="org.springframework.data.redis.core.RedisTemplate" > <property name ="connectionFactory" ref ="redisConnectionFactory" /> <property name ="keySerializer" > <bean class ="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property > <property name ="valueSerializer" > <bean class ="org.springframework.data.redis.serializer.StringRedisSerializer" > </bean > </property > </bean >
</beans >
測試代碼:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations =
"classpath:applicationContext.xml" )
public class RedisTemplateTest {@Autowired
private RedisTemplate<String, String> redisTemplate;@Test
public void testRedis () {redisTemplate.opsForValue().
set (
"city" ,
"武漢" ,
30 , TimeUnit.SECONDS);System.
out .println(redisTemplate.opsForValue().
get (
"city" ));}
}
<–end–>
總結
以上是生活随笔 為你收集整理的Java-SpringDataRedis使用入门 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。