Maven中Spring-Data-Redis存储对象(redisTemplate) (转)
Redis是一种nosql数据库,在开发中常用做缓存。Jedis是Redis在java中的redis- client.在此之前,希望已经了解redis的基本使用和Maven的使用。建立Maven Project之后,在POM.xml中添加jedis和spring-data-redis的依赖如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.0.0</version> <type>jar</type> <scope>compile</scope></dependency><!-- spring-redis --><dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.0.0.RELEASE</version></dependency> |
Redis连接数据库参数如下:applicationContext-redis.properties
|
1
2
3
4
5
6
7
8
|
#redis configredis.pool.maxActive=100redis.pool.maxIdle=20redis.pool.maxWait=1000redis.pool.testOnBorrow=trueredis.hostname=localhostredis.port=6379redis.password= |
在上下文配置中使用key-value读取方式读取properties中的值:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<!-- Jedis 连接池配置--><bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxActive" value="${redis.pool.maxActive}" /> <property name="maxIdle" value="${redis.pool.maxIdle}" /> <property name="maxWait" value="${redis.pool.maxWait}" /> <property name="testOnBorrow" value="${redis.pool.testOnBorrow}" /></bean><!-- Jedis ConnectionFactory 数据库连接配置--><bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="${redis.hostname}" /> <property name="port" value="${redis.port}" /> <property name="password" value="${redis.password}" /> <property name="poolConfig" ref="jedisPoolConfig" /></bean><!—- redisTemplate配置,redisTemplate是对Jedis的对redis操作的扩展,有更多的操作,封装使操作更便捷 --> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="jedisConnectionFactory" /> |
上面redisTemplate已经基本配置完成。
接下来创建User类,必须实现或者间接实现Serializable接口:
Redis存储对象是使用序列化,spring-data-redis已经将序列化的功能内置,不需要我们去管,我们只需要调用api就可以使用。SerialVersionUID字段对序列化扩展有用,为了以后扩展或者缩减字段时不会造成反序列化出错。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
public class User implements Serializable { private static final long serialVersionUID = -7898194272883238670L; public static final String OBJECT_KEY = "USER"; public User() { } public User(String id) { } public User(String id, String name) { this.id = id; this.name = name; } private String id; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String toString() { return "User [id=" + id + ", name=" + name + "]"; } public String getKey() { return getId(); } public String getObjectKey() { return OBJECT_KEY; }} |
创建userService类来操作redis增删查改缓存对象。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
public class UserService { RedisTemplate<String, User> redisTemplate; public RedisTemplate<String, User> getRedisTemplate() { return redisTemplate; } public void setRedisTemplate(RedisTemplate<String, User> redisTemplate) { this.redisTemplate = redisTemplate; } public void put(User user) { redisTemplate.opsForHash().put(user.getObjectKey(), user.getKey(), user); } public void delete(User key) { redisTemplate.opsForHash().delete(key.getObjectKey(), key.getKey()); } public User get(User key) { return (User) redisTemplate.opsForHash().get(key.getObjectKey(), key.getKey()); }} |
在上下文中配置redisTemplate注入,在使用bean方式来注入时,redisTemplate必须有setter/getter方法:
|
1
2
3
4
5
|
<bean id="userService" class="Service.UserService"> <property name="redisTemplate"> <ref bean="redisTemplate" /> </property></bean> |
======如果使用注解方式自动注入,则可以注释掉上面的bean配置方式======
在UserService注解@Service(“userService”),也可以在Service里写名字,默认是第一字母小写。
|
1
2
3
4
5
6
7
8
|
@Service("userService")public class UserService { @Autowired RedisTemplate<String, User> redisTemplate; …… ……} |
在上下文配置文件中,添加自动扫描包的context节点,Base-package的路径要覆盖包含注解的类文件:
|
1
|
<context:component-scan base-package="*" /> |
在main中来简单操作一下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
public class Main { public static void main( String[] args ) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath*:/conf/applicationContext.xml" ); UserService userService = (UserService) applicationContext.getBean("userService"); User user1 = new User("user1ID", "User 1"); User user2 = new User("user2ID", "User 2"); System.out.println("==== getting objects from redis ===="); System.out.println("User is not in redis yet: " + userService.get(user1)); System.out.println("User is not in redis yet: " + userService.get(user2)); System.out.println("==== putting objects into redis ===="); userService.put(user1); userService.put(user2); System.out.println("==== getting objects from redis ===="); System.out.println("User should be in redis yet: " + userService.get(user1)); System.out.println("User should be in redis yet: " + userService.get(user2)); System.out.println("==== deleting objects from redis ===="); userService.delete(user1); userService.delete(user2); System.out.println("==== getting objects from redis ===="); System.out.println("User is not in redis yet: " + userService.get(user1)); System.out.println("User is not in redis yet: " + userService.get(user2)); }} |
http://www.cnblogs.com/jifeng/p/4422435.html
Maven中Spring-Data-Redis存储对象(redisTemplate) (转)的更多相关文章
- spring mvc Spring Data Redis RedisTemplate [转]
http://maven.springframework.org/release/org/springframework/data/spring-data-redis/(spring-data包下载) ...
- Spring Data Redis简介以及项目Demo,RedisTemplate和 Serializer详解
一.概念简介: Redis: Redis是一款开源的Key-Value数据库,运行在内存中,由ANSI C编写,详细的信息在Redis官网上面有,因为我自己通过google等各种渠道去学习Redis, ...
- Spring Data Redis 2.x 中 RedisConfiguration 类的新编写方法
在 Spring Data Redis 1.x 的时候,我们可能会在项目中编写这样一个RedisConfig类: @Configuration @EnableCaching public class ...
- spring data redis RedisTemplate操作redis相关用法
http://blog.mkfree.com/posts/515835d1975a30cc561dc35d spring-data-redis API:http://docs.spring.io/sp ...
- 关于在项目中使用spring data redis与jedis的选择
项目中需要用到redis,主要用来作为缓存,redis的客户端有两种实现方式,一是可以直接调用jedis来实现,二是可以使用spring data redis,通过spring的封装来调用. 应该使用 ...
- 关于spring data redis repository @RedisHash注解的对象上有DateTime属性字段的问题
当你save保存的时候你会发现出现StackOverflow Exception,很明显出现了无限循环,可是仅仅是一个save操作,哪里来的无限循环呢? 最终发现就是DateTime导致的,因为将对象 ...
- Spring Data Redis示例
说明 关于Redis:一个基于键值对存储的NoSQL内存数据库,可存储复杂的数据结构,如List, Set, Hashes. 关于Spring Data Redis:简称SDR, 能让Spring应用 ...
- Redis与Spring Data Redis
1.Redis概述 1.1介绍 官网:https://redis.io/ Redis是一个开源的使用ANSIC语言编写.支持网络.可基于内存 亦可持久化的日志型.Key-Value型的高性能数据库. ...
- spring data redis 理解
前言 Spring Data Redis project,应用了Spring概念来开发使用键值形式的数据存储的解决方案.我们(官方)提供了一个 "template" ,这是一个高级 ...
- Spring Data Redis 详解及实战一文搞定
SDR - Spring Data Redis的简称. Spring Data Redis提供了从Spring应用程序轻松配置和访问Redis的功能.它提供了与商店互动的低级别和高级别抽象,使用户免受 ...
随机推荐
- 14.10.4 Defragmenting a Table 整理表
14.10.4 Defragmenting a Table 整理表: 随机插入或者删除从一个secondary index 可以导致index变的fragmented Fragmentation意味着 ...
- JDBC操作数据库的学习(1)
单单对数据库的操作,比如说MySQL,我们可以在命令行窗口中执行,但是一般是应用程序要操作数据库,因此我们应该在程序中的代码上体现对数据库的操作,那么使用程序应用如何操作数据库呢?那就要使用到数据库的 ...
- oracle逗号字符串拼接小工具
oracle逗号字符串拼接小工具 http://www.zui#dai#ma.com/share/1932670249667584.htm 在使用oracle进行数据查询时,常常需要使用到in语句,如 ...
- linux shell中的单引号与双引号的区别(看完就不会有引号的疑问了)(转)
tips: ============================= IFS - LINUX字段分隔符,内部字段分隔符 IFS(Internal Field Seperator)在Linux的she ...
- MySQL备份方案-->(利用mysqldump以及binlog二进制日志)
MySQL备份方案-->(利用mysqldump以及binlog二进制日志) 随着数据不 ...
- Delphi图像处理 -- 最大值
阅读提示: <Delphi图像处理>系列以效率为侧重点,一般代码为PASCAL,核心代码采用BASM. <C++图像处理>系列以代码清晰,可读性为主,全部使用C ...
- 14.2.5.6 Adaptive Hash Indexes 自适应Hash Indexes
14.2.5.6 Adaptive Hash Indexes 自适应Hash Indexes adaptive hash index(AHI) 让InnoDB 执行更加像在一个内存数据库里在, 在不牺 ...
- CorePlot学习
阅读这篇文章,指出它在国外 原文地址:https://github.com/core-plot/core-plot/wiki/High-Level-Design-Overview 强烈推荐阅读该 ...
- [Java学习笔记]Java Tips
1.Java没有sizeof关键字 , volatile是java关键字.详情见:http://www.cnblogs.com/aigongsi/archive/2012/04/01/2429166. ...
- pv操作 生产者消费者
#include <iostream> #include <stdlib.h> #include <pthread.h> #include <semaphor ...