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= redis.maxIdle=
redis.maxTotal=
redis.maxWaitMillis=
这里面用到的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中。

友情链接
- 详细的spring data redis官方文档:http://docs.spring.io/spring-data/redis/docs/1.8.4.RELEASE/reference/html/
springdata----->spring集成redis(一)的更多相关文章
- Spring集成Redis集群(含spring集成redis代码)
代码地址如下:http://www.demodashi.com/demo/11458.html 一.准备工作 安装 Redis 集群 安装参考: http://blog.csdn.net/zk6738 ...
- spring 集成 redis -- pub/sub
redis除了常用的当做缓存外,还可以当做简单的消息中间件,实现消息发布订阅 spring集成redis,可以使用spring-data-redis 首先引入相关maven依赖(此处我spring相关 ...
- spring集成redis
redis是一种非关系型数据库,与mongoDB不同的是redis是内存数据库,所以访问速度很快.常用作缓存和发布-订阅式的消息队列.redis官方没有提供windows版本的软件.windows版本 ...
- spring 集成redis客户端jedis(java)
spring集成jedis简单实例 jedis是redis的java客户端,spring将redis连接池作为一个bean配置. “redis.clients.jedis.JedisPool”,这 ...
- spring集成redis,集成redis集群
原文:http://chentian114.iteye.com/blog/2292323 1.通过spring-data-redis集成redis pom.xml依赖包 <project xml ...
- Spring集成Redis方案(spring-data-redis)(基于Jedis的单机模式)(待实践)
说明:请注意Spring Data Redis的版本以及Spring的版本!最新版本的Spring Data Redis已经去除Jedis的依赖包,需要自行引入,这个是个坑点.并且会与一些低版本的Sp ...
- spring集成redis——主从配置以及哨兵监控
Redis主从模式配置: Redis的主从模式配置是非常简单的,首先我们需要有2个可运行的redis环境: master node : 192.168.56.101 8887 slave node: ...
- Spring集成Redis缓存
作者:13 GItHub:https://github.com/ZHENFENG13 版权声明:本文为原创文章,未经允许不得转载. 整合Redis 本来以为类似的Redis教程和整合代码应该会很多,因 ...
- Spring+SpringMVC+MyBatis+easyUI整合进阶篇(十二)Spring集成Redis缓存
作者:13 GitHub:https://github.com/ZHENFENG13 版权声明:本文为原创文章,未经允许不得转载. 整合Redis 本来以为类似的Redis教程和整合代码应该会很多,因 ...
- Spring集成Redis使用注解
转载:http://blog.csdn.net/u013725455/article/details/52129283 使用Maven项目,添加jar文件依赖: <project xmlns=& ...
随机推荐
- 推荐一款编辑SQL的工具:jsqlparser
这个工具真的挺好用的,采用的是观察者模式,Visitor. 虽然我学过这个模式,但是乍一用还是有点懵逼的. 给好一个SQL语句之后,jsqlparser可以把这个sql语句给分解成all kind o ...
- VS2008 express + opencv配置
刚开始接触opencv,不是很熟悉,配置过程主要参考了这篇博客,大家可以去看看 http://www.cnblogs.com/micky-zhou/archive/2012/08/06/2624433 ...
- Hibernate使用createSqlQuery进行模糊查询时找不到数据
1. 首先明确一点,使用createSqlQuery如下两种方式的占位符都可以使用,这个在官方的文档可以查到. 注意使用模糊查询时,赋值两边不可以添加单引号. Query query = sess.c ...
- 老生常谈:vim 配置
1.自动补全 插件名字:NeoComplCache 下载地址:http://www.vim.org/scripts/script.php?script_id=2620 配置:把文件直接复制到vimfi ...
- 谈API网关的背景、架构以及落地方案
Chris Richardson曾经在他的博客上详细介绍过API网关,包括API网关的背景.解决方案以及案例.对于大多数基于微服务的应用程序而言,API网关都应该是系统的入口,它会负责服务请求路由.组 ...
- MySql C++调用库Connector/c++编译 和 接口封装【三】Connector/c++ 使用总结及封装
工程需要加上各个路径: 库使用需要几个文件: 1.include 文件夹 c/c++ /常规/附加包含目录 Connector/c++ 的安装版里面的Include 文件夹.或者把 /driver以及 ...
- smo算法
文章在word里写好了,公式没法贴到论坛里,所以直接截图了
- 关于Cocos2d-x中使用完Blink动作后精灵突然消失的问题的解决
精灵使用Blink 执行完动作之后,消失不见了,原因是闪烁的过程中精灵刚好到空纹理(透明)的那部分,这时候用户通过某种操作中断闪烁动作,导致下个状态的时候,精灵依然停留在空纹理的状态.所以最好在精灵执 ...
- 帝国CMS“建立目录不成功!请检查目录权限”的解决办法
初次安装帝国CMS就遇到了一个问题,在提交或者修改信息的时候提示“建立目录不成功!请检查目录权限”,无法生成页面.检查了文件夹的读写权限和用户访问权限,发现都一切正常.那么到底是哪里出错了呢? 其实是 ...
- 【转】IIS日志-网站运维的好帮手
对于一个需要长期维护的网站来说,如何让网站长久稳定运行是件很有意义的事情. 有些在开发阶段没有暴露的问题很有可能就在运维阶段出现了,这也是很正常的. 还有些时候,我们希望不断地优化网站,让网站更快速的 ...