SSM框架快速整合redis

CatalpaFlat發表於2017-11-12

SSM框架快速整合redis

1.新增maven依賴

<!-- config redis data and client jar-->
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>${spring.redis.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
    <version>${commons.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>${jedis.version}</version>
</dependency>複製程式碼

注:org.springframework.data、org.apache.commons、redis.clients三者的版本必須統一,不然編譯時會出現各種版本衝突。(即這種錯誤資訊:java.lang.NoSuchMethodError)這裡三者的版本分別為(親測可用):

<spring.redis.version>1.6.0.RELEASE</spring.redis.version>
<jedis.version>2.7.2</jedis.version>
<commons.version>2.4.2</commons.version>複製程式碼

2.編輯redis連線資訊

redis.host=192.168.137.111
redis.port=6379
redis.password=

redis.maxIdle=300
redis.maxWaitMillis=1000
redis.maxTotal=600
redis.testOnBorrow=true
redis.testOnReturn=true

redis.projectNam=ssm_redis複製程式碼

3.配置redis-context.xml

<?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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       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">
    <!--掃描redis配置檔案-->
    <context:property-placeholder ignore-unresolvable="true" location="classpath:properties/redis.properties"/>
    <!--設定連線池-->
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="${redis.maxIdle}"/>
        <property name="maxTotal" value="${redis.maxTotal}" />
        <property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />
        <property name="testOnReturn" value="${redis.testOnReturn}" />
    </bean>
    <!--設定連結屬性-->
    <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
          p:hostName="${redis.host}"
          p:port="${redis.port}"
          p:password="${redis.password}"
          p:pool-config-ref="poolConfig"
          p:timeout="100000"/>
    <!-- Jedis模板配置  -->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <property name="connectionFactory"   ref="connectionFactory" />
    </bean>

</beans>複製程式碼

4.spring引入redis配置檔案

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

    <!-- 引入redis屬性配置檔案 -->
    <import resource="classpath:cache/redis-context.xml"/>

</beans>複製程式碼

5.單元測試redis

/**
 * @Author: CatalpaFlat
 * @Descrition:
 * @Date: Create in 10:08 2017/11/8
 * @Modified BY:
 */
@RunWith(SpringRunner.class)
@ContextConfiguration({"classpath:spring/*.xml"})
public class TestRedis {

    @Autowired
    private RedisTemplate redisTemplate;

    private static final Logger log  = Logger.getLogger(TestRedis.class.getName());

    @Test
    public void test(){
        redisTemplate.opsForValue().set("chen", "陳梓平");
        log.info("value:"+redisTemplate.opsForValue().get("chen"));
    }
}複製程式碼

到此ssm整合redis已經完美嵌入。

相關文章