美文网首页
maven配置springmvc+spring data red

maven配置springmvc+spring data red

作者: _Rondo | 来源:发表于2018-03-11 12:27 被阅读0次

    一、概述

    redis,键值存储系统,面试中常问到的是redis数据结构,在公司中还能使用redis的主键生成uuid,其他的我也不知道了,因为还轮不到我做系统。

    二、配置使用

    1.添加依赖

            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-redis</artifactId>
                <version>1.5.2.RELEASE</version>
            </dependency>
    
            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>2.9.0</version>
            </dependency>
    

    2.配置applicationContext-Redis.xml,并把文件添加到web.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:aop="http://www.springframework.org/schema/aop"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:p="http://www.springframework.org/schema/p"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:amq="http://activemq.apache.org/schema/core"
           xmlns:jms="http://www.springframework.org/schema/jms"
           xmlns:mvc="http://www.alibaba.com/schema/stat"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.1.xsd
            http://activemq.apache.org/schema/core
            http://activemq.apache.org/schema/core/activemq-core-5.7.0.xsd
            http://www.alibaba.com/schema/stat http://www.alibaba.com/schema/stat.xsd
            http://www.springframework.org/schema/jms
            http://www.springframework.org/schema/jms/spring-jms-4.1.xsd ">
    
    
        <context:annotation-config />
    
        <!-- scanner redis properties  -->
        <context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true"/>
    
        <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
            <property name="maxIdle" value="${redis.maxIdle}" />
            <property name="minIdle" value="${redis.minIdle}" />
            <property name="maxTotal" value="${redis.maxTotal}" />
            <property name="testOnBorrow" value="${redis.testOnBorrow}" />
        </bean>
    
        <bean id="redisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
              p:host-name="${redis.host}"
              p:port="${redis.port}"
              p:password="${redis.pass}"
              p:pool-config-ref="poolConfig"/>
    
        <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
            <property name="connectionFactory"   ref="redisConnectionFactory" />
        </bean>
    
    </beans>
    

    redis.properties

    # Redis settings
    redis.host=localhost
    redis.port=6379
    redis.pass=
    
    redis.maxIdle=600
    redis.minIdle=300
    redis.maxTotal=1000
    redis.testOnBorrow=true
    

    3.直接使用spring data redis 中封装的方法进行操作

    package com.web.test.controller;
    
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import javax.annotation.Resource;
    
    
    @RestController
    public class TestController {
    
        @Resource(name="redisTemplate")
        private RedisTemplate<String,String> redisTemplate;
    
        @RequestMapping("/redisTest")
        public String redisTest(){
            return redisTemplate.opsForValue().get("username");
        }
    
    }
    
    

    -end-

    相关文章

      网友评论

          本文标题:maven配置springmvc+spring data red

          本文链接:https://www.haomeiwen.com/subject/oczifftx.html