zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Java-SpringDataRedis使用入门

JAVA入门 使用
2023-09-14 09:01:58 时间

<–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.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/data/jpa
    http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
    http://cxf.apache.org/jaxws
    http://cxf.apache.org/schemas/jaxws.xsd
    http://www.springframework.org/schema/cache
    http://www.springframework.org/schema/cache/spring-cache.xsd">

    <!-- jedis 连接池配置 -->
     <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">  
        <property name="maxIdle" value="300" />        
        <property name="maxWaitMillis" value="3000" />  
        <property name="testOnBorrow" value="true" />  
    </bean>  

    <!-- jedis 连接工厂 -->
    <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" />  

    <!-- spring data 提供 redis模板  -->
    <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() {
        // 保存key value
        // 设置30秒失效
        redisTemplate.opsForValue().set("city", "武汉", 30, TimeUnit.SECONDS);

        System.out.println(redisTemplate.opsForValue().get("city"));
    }
}

<–end–>