Spring Data Redis 整合

  

    首先现在redis包,官方网址是 http://redis.io/download 

    下载好了后直接解压Redis压缩包到指定目录:

    

    

    使用对应位数操作系统文件夹下面redis-server.exe命令启动redis (测试命令redis-server.exe 服务启动程序redis-cli.exe 客户端命令行工具redis.conf 服务配置文件通过redis-server.exe启动服务,默认端口 6379通过redis-cli.exe 启动客户端工具)。

  

    1.在程序中通过maven坐标,引入spring data redis  ,命令如下所示:

  <dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>1.4.1.RELEASE</version>
  </dependency>

     2.配置applicationContext.xml代码如下所示:   

  <!-- 配置redis连接池 -->

  <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">

  <property name="hostName" value="localhost"/>

  <property name="port" value="6379"/>

  <property name="poolConfig" ref="poolConfig"/>

  </bean>

  <!-- 配置整合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"/>

  </property>

  </bean>

     3.编写测试代码如下所示:    

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringDataRedis {
  @Resource
  private RedisTemplate<String,String> redistemplate;
  @Test
  public void testRedis(){
    redistemplate.opsForValue().set("testkey", "myTestvaule",1, TimeUnit.DAYS);
    }
}

      

第一次写博客有许多不足之处请大佬们见谅哟!

猜你喜欢

转载自www.cnblogs.com/sjzxs/p/9353052.html