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

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

如下:

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

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

    <!-- 开启IOC注解 -->
    <context:component-scan base-package="cn.xdl.jd.redis"/>

    <!-- JedisTemplate -->
    <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;
<!-- 导包要注意是这个包“jedis.JedisConnectionFactory”-->
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 {
    <!-- 注入RedisTemplate-->
    @Resource
    private RedisTemplate template;
    <!-- 向redis里放数据-->
    @Test
    public void test1(){
        ValueOperations opt = template.opsForValue();
        opt.set("template", "RedisTemplate");
    }
    <!-- 向redis里取数据-->
    @Test
    public void test2(){
        ValueOperations opt = template.opsForValue();
        Object value = opt.get("template");
        System.out.println(value);
    }   
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值