zoukankan      html  css  js  c++  java
  • 使用SpringDataRedis的入门

    在使用ssm框架下,我们会到redis做缓存。

    1> 第一步,导包。

    <!-- Redis客户端 -->  
    <dependency>  
    	<groupId>redis.clients</groupId>   
    	<artifactId>jedis</artifactId>  
    	<version>2.9.0</version>  
    </dependency>  
    <!-- redis Spring 基于注解配置 -->   
    <dependency>    
      <groupId>org.springframework.data</groupId>    
      <artifactId>spring-data-redis</artifactId>    
      <version>1.7.2.RELEASE</version>    
    </dependency>
    

    2> 第二步,创建redis的配置文件。

    redis.properties

     	   #redis地址
            redis.host=127.0.0.1 
            #端口
            redis.port=6379 
            # 密码,没有则可以不写
            redis.pass=123456 
            redis.database=0 
            redis.maxIdle=300 
            redis.maxWait=3000 
            redis.testOnBorrow=true 
    

    spring-redis.xml,但是建议直接写在applicationContext.xml(也就是spring的配置文件,在web.xml中已经配置了的)中,这样就可以直接引用.

    如果是新建了一个xml文件,就要在spring配置文件中引入,或者在web.xml中配置。

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

    几个小插曲
    1.在xml中引用外部xml文件

    <import resource="spring-redis.xml"/> spring-redis.xml就是外部文件
    

    2.解决一个xml中只能引入一个.properties文件的问题
    在引用的时候,用如下写法

    <context:property-placeholder ignore-unresolvable="true" location="classpath:first.properties" />
    <context:property-placeholder ignore-unresolvable="true" location="classpath:second.properties" />
    

    3. redis中要存入对象时,对象要实现Serializable接口

  • 相关阅读:
    公司的OA系统基础框架系统(光标办公平台)
    通用权限控制系统--系统设计
    聘.Net软件工程师(昆明)
    对AgileFramework的思考
    iTextSharp.text.Rectangle 使用方法说明
    Castle Aspect# 难倒只支持一个拦截器?
    聘云南昆明地区的.Net工程师
    招聘云南软件销售人员
    给vncviewer 添加调用函数 GIS
    分享一个c++ 加密算法 ,在百度贴吧找的,比较好玩 GIS
  • 原文地址:https://www.cnblogs.com/Lyn4ever/p/11046002.html
Copyright © 2011-2022 走看看