spring-redis

一.序言

     这里简单介绍spring-redis 的例子,官网介绍的:http://projects.spring.io/spring-data-redis/#quick-start

 

 

二.小例子

     1.MAVEN 配置,因为是子工程,版本和日志之类的,自己搞定吧~。~

      

       <!-- 3.0 没出来- -先用2.7吧 -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.7.0</version>
        </dependency>

        <!-- spring data的结合  -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.5.0.RELEASE</version>
        </dependency>

        <!-- spring 基础 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

 

    2.spring 配置

    

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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">

       <!-- 加载redis配置文件,可以后面用,为了简单我用得默认配置 -->
       <context:property-placeholder location="classpath:redis.properties"/>

       <bean id="jedisFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"/>

       <!-- 操作模板 -->
       <bean id="jedisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
              <property name="connectionFactory" ref="jedisFactory"/>
              <property name="keySerializer">
                     <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
              </property>
              <property name="valueSerializer">
                     <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
              </property>
       </bean>
</beans>

 

   3.后面自己redis.properties配置

    

# Redis settings

#redis的服务器地址
redis.host=localhost
#redis的服务端口
redis.port=6379
#密码
redis.pass=123
#链接数据库
redis.default.db= 0
#客户端超时时间单位是毫秒
redis.timeout=100000
#最大连接数
redis.maxTotal=300
#最大空闲数
redis.maxIdle=100
#最大建立连接等待时间
redis.maxWaitMillis=1000
#指明是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个
redis.testOnBorrow=true

 

  4.测试基本测试

     

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * 基本测试
 * Created by qqr on 15/4/15.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/spring-redis.xml")
public class SpringRedisDemo {

    @Autowired
    RedisTemplate jedisTemplate;

    @Test
     public void putAndGet(){
        jedisTemplate.opsForHash().put("user","name","张三");
        Object name =  jedisTemplate.opsForHash().get("user","name");
        System.out.println(name);
    }
}

 

 

三.小结

      1.本文仅仅是基础常识,第一次不想太麻烦,先跑起来再说

      2.如果需要正式环境用,配置复杂一些,但是都差不多,可以去官网看看

         地址:http://docs.spring.io/spring-data/redis/docs/1.5.0.RELEASE/reference/html/

       3.spring-data 系列的东西还是比较多,可以多玩玩 挺方便的

 

其他命令详细:

http://www.cnblogs.com/stephen-liu74/archive/2012/03/19/2352932.html

你可能感兴趣的:(redis,spring,spring-data)