Spring+Redis结合(插入和提取数据)

spring框架和redis结合,需要使用spring-data框架功能

如下:

1.导入spring-data-redis.jar
2.在spring中配置下面组件


<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" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-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/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">

    
    <context:component-scan base-package="cn.xdl.jd.redis"/>

    
    <bean id="template" 
        class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="redisConnectionFactory">property>
    bean>

    
    <bean id="redisConnectionFactory" 
        class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        
        <property name="hostName" value="localhost">property>
        
        <property name="port" value="6379">property>
        <property name="poolConfig" ref="redisPoolConfig">property>
    bean>
    
    <bean id="redisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
    
        <property name="maxTotal" value="100">property>
        <property name="minIdle" value="5">property>
    bean>

beans>


建立测试类

测试redis中存取数据

import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.data.redis.connection.RedisConnectionFactory;

import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import cn.xdl.jd.entity.Address;
import redis.clients.jedis.JedisPoolConfig;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:spring-redis.xml"})
public class TestRedisTemplate {
    
    @Resource
    private RedisTemplate template;
    
    @Test
    public void test1(){
        ValueOperations opt = template.opsForValue();
        opt.set("template", "RedisTemplate");
    }
    
    @Test
    public void test2(){
        ValueOperations opt = template.opsForValue();
        Object value = opt.get("template");
        System.out.println(value);
    }   
}

你可能感兴趣的:(springmvc)