ssm不用springboot 配置spring data redis 的redisTemplate

首先maven依赖,别用最新版,用这版,因为新版jedisConnectionFactory的配置过时了,新版怎么配置我也没搞清楚

<!--redis-->
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-redis</artifactId>
      <version>1.3.0.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.4.2</version>
    </dependency>
redis.properties
# 设置最大连接数
REDIS.MAXTOTAL=50
#设置获取连接的最大等待时间
REDIS.MAXWAITMILLIS=1000
#开启获取连接时可用性校验,保证拿到的连接都是可用的
REDIS.TESTONBORROW=true
#设置IP
REDIS.HOSTNAME=localhost
#设置端口
REDIS.PORT=6379
#密码
REDIS.PASSWORD=
#设置是否使用连接池
REDIS.USEPOOL=true

 

 

然后配置applicationContext-redis.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       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.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true" />
    <!--构建连接池配置信息-->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <!--设置最大连接数-->
        <property name="maxTotal" value="${REDIS.MAXTOTAL}"></property>
        <!--设置获取连接的最大等待时间-->
        <property name="maxWaitMillis" value="${REDIS.MAXWAITMILLIS}"></property>
        <!--开启获取连接时可用性校验,保证拿到的连接都是可用的-->
        <property name="testOnBorrow" value="${REDIS.TESTONBORROW}"></property>
    </bean>
    <!--构建Spring-data-redis连接工厂,其实就是连接池-->
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <constructor-arg index="0" ref="jedisPoolConfig"></constructor-arg>
        <!--设置IP-->
        <property name="hostName" value="${REDIS.HOSTNAME}"></property>
        <!--设置端口-->
        <property name="port" value="${REDIS.PORT}"></property>
        <!--设置是否使用连接池-->
        <property name="usePool" value="${REDIS.USEPOOL}"></property>
        <!--设置密码默认是没有密码-->
        <property name="password" value="${REDIS.PASSWORD}"></property>
    </bean>
    <!--stringRedisTemplate-->
    <!-- String类型的RedisTemplate模板 -->
    <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <constructor-arg index="0" ref="jedisConnectionFactory" />
    </bean>
</beans>

在applicationContext.xml中引入上面的xml

<import resource="applicationContext-redis.xml"/>



然后就可以在需要的地方注入;如下:
@Autowired
    private RedisTemplate<String, String> redisTemplate;


/**
             * 存储token到redis并设置过期时间10分钟
             */
            redisTemplate.opsForValue().set(Const.TOKEN_PREFIX + username, forgetToken, 10, TimeUnit.MINUTES);

/**
         * 从redis中取出对应用户的token
         */
        String token  = redisTemplate.opsForValue().get(Const.TOKEN_PREFIX + username);


/**
                 * 修改成功,删除redis中的存储
                 */
                redisTemplate.delete(Const.TOKEN_PREFIX + username);

 

 




posted @ 2019-09-28 09:33  云晴  阅读(996)  评论(0编辑  收藏  举报