springdata----->spring集成redis(一)

  使用spring-data-redis与redis集成,今天我们就通过例子来学习一下。当时间和耐心都已经变为奢侈,我们只能靠星座了解彼此。

spring与redis集成的实例

注意:这里我们测试的是安装在window本地的redis,版本为redis-3-2.0。测试的项目结构如下,主体有三个文件。

一、在pom.xml中添加spring-data-redis的依赖

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>1.8.1.RELEASE</version>
</dependency>

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>

二、定义一个主的配置文件spring-redis.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:context="http://www.springframework.org/schema/context" 
    xmlns:p
="http://www.springframework.org/schema/p" 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"
> <context:property-placeholder location="classpath:redis.properties"/> <bean id="jedisPoolConfig" 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="true" /> </bean> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:host-name="${redis.host}" p:port="${redis.port}" p:pool-config-ref="jedisPoolConfig"/> <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"> <property name="connectionFactory" ref="connectionFactory" /> </bean> </beans>

三、一些属性的配置,放在redis.properties文件中

# Redis settings
redis.host=localhost
redis.port=6379

redis.maxIdle=100
redis.maxTotal=300
redis.maxWaitMillis=10000

这里面用到的jedis版本比较新,网上比较旧的配置属性是不一样的。

配置文件中千万要注意前后的空格问题,如果localhost有空格,那么会报找不到host的。而且这个错有点难以发现,关于空格的问题要注意一下。

四、主体测试类RedisMain.java

我们这里简单的演示一下,string数据的插入与取得过程。

package com.linux.huhx.springRedis;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

/**
 * Created by huhx on 2017-07-18.
 */
public class RedisMain {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-redis.xml");
        final RedisTemplate redisTemplate = (StringRedisTemplate) context.getBean("redisTemplate");

        // 存放key-value
        redisTemplate.execute(new RedisCallback<Boolean>() {
            @Override
            public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
                RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
                byte[] key = serializer.serialize("username");
                byte[] value = serializer.serialize("liuling");
                return connection.setNX(key, value);
            }
        });

        // 得到key,得到value
        redisTemplate.execute(new RedisCallback<Boolean>() {
            @Override
            public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
                RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
                byte[] key = serializer.serialize("username");
                String username = serializer.deserialize(connection.get(key));
                System.out.println(username); // liuling
                return null;
            }
        });
    }
}

 五、在本地启动redis,运行RedisMain程序。

在redis-cli的控制台,可以看出已经成功插入数据到redis中。

友情链接

原文地址:https://www.cnblogs.com/huhx/p/baseusespringdataredis1.html