Maven中Spring-Data-Redis存储对象(redisTemplate)

Redis是一种nosql数据库,在开发中常用做缓存。Jedis是Redis在Java中的redis- client.在此之前,希望已经了解redis的基本使用和Maven的使用。建立Maven Project之后,在POM.xml中添加jedis和spring-data-redis的依赖如下:

<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

#redis config  
redis.pool.maxActive=100  
redis.pool.maxIdle=20  
redis.pool.maxWait=1000  
redis.pool.testOnBorrow=true  
redis.hostname=localhost  
redis.port=6379  
redis.password= 
在上下文配置中使用key-value读取方式读取properties中的值:

<!-- 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字段对序列化扩展有用,为了以后扩展或者缩减字段时不会造成反序列化出错。

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增删查改缓存对象。

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());  }  
}  
使用注解方式自动注入, 在UserService注解@Service(“userService”),也可以在Service里写名字,默认是第一字母小写。

@Service("userService")  
public class UserService {  @Autowired  RedisTemplate<String, User> redisTemplate;  ……  ……  
}  
在上下文配置文件中,添加自动扫描包的context节点, Base-package的路径要覆盖包含注解的类文件

<context:component-scan base-package="*" />  
在main中来简单操作一下:

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));  }  
}  
确保redis服务器是开启状态之后就可以运行程序。运行结果如下:



转载自:点击打开链接





本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/419455.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

opc服务器网站,OPC 服务器

OPC服务器OPC服务器, 是指按照OPC基金组织规定的OPC规范群开发的软件驱动。OPC服务器作为中间媒介负责从数据源读取数据再跟另外一端的客户端通信。在OPC客户端/服务器的结构图中, 通信的发起端是, 也只能是OPC客户端。客户端和服务器的对话是双向的, 也就是说, 客户端既可以从…

LeetCode 292 Nim Game

LeetCode 292 Nim Game https://leetcode.com/problems/nim-game/ 当能被4整除时&#xff0c;才会输。 bool canWinNim(int n) {return n%4; } 转载于:https://www.cnblogs.com/walker-lee/p/4878435.html

前端学习(1620):前端系列实战课程之提取行间样式

<!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><title>Document</title><!-- 放在头部 --><…

ThreadLocal知识介绍

ThreadLocal为解决多线程的并发问题提供了一种新的思路&#xff0c;使用这个工具类可以优美的编写出多线程程序。 ThreadLocal他不是一个线程&#xff0c;而是线程的一个本地化对象。当工作于多个线程的对象使用ThreadLocal维护变量时&#xff0c;ThreadLocal为每个使用改变量的…

维修变频器和服务器赚钱吗,【转】一个变频器维修老手的7年经验与心得

到今年为止&#xff0c;我在电路板维修行业已经七年了&#xff0c;手里经过的板子不计其数&#xff0c;大的小的&#xff0c;简单的难的都有&#xff0c;帮助很多的有名企业度过很多的难关。现在每个企业都不同程度的用到许多变频器&#xff0c;而不管哪种变频器他 的寿命好像都…

第十三章、拷贝控制

一、拷贝控制操作 1、拷贝构造函数&#xff1a;一个构造函数的一个参数是自身类类型的引用&#xff0c;且额外参数都有默认值 class Foo{ public:Foo(const Foo&); //拷贝构造函数&#xff1b;&#xff1b;最好是const类型&#xff1b;不应该是explicit的 }拷贝构造…

ThreadLocal和线程同步机制的对比

ThreadLocal和线程同步都是为了解决多线程中相同变量的访问冲突问题&#xff0c;那么&#xff0c;二者的区别在哪里&#xff1f;和线程同步机制对比&#xff0c;ThreadLocal有什么优势&#xff1f; 同步机制中通过对象的锁机制保证同一时间只有一个线程来访问变量&#xff0c;这…

关于电脑自动获取和静态IP的问题

无法设置静态IP和设置了静态IP&#xff0c;但是无效的问题。 1、先是重新安装TCP/IP协议 选TCP/IP&#xff0c;点安装&#xff0c;再选择协议&#xff0c;点添加&#xff0c;选从磁盘安装&#xff0c;路径为&#xff1a;C:\Windows\inf\1394.inf&#xff0c;点打开 此时&#x…

javaweb中报404错误

这里只提供了一点建议&#xff0c;404大家都懂得是找不到请求的页面&#xff0c;一般就是URL路径请求错误 但是我最近发现自己出现这个错误的原因是&#xff1a;每次的struts的配置文件的命名空间忘记加在了form表单的action名字所对应的前面&#xff0c;一定不要忘了当自己定…

深入浅出: Java回调机制(异步)

一、什么是回调 回调&#xff0c;回调。要先有调用&#xff0c;才有调用者和被调用者之间的回调。所以在百度百科中是这样的&#xff1a; 软件模块之间总是存在着一定的接口&#xff0c;从调用方式上&#xff0c;可以把他们分为三类&#xff1a;同步调用、回调和异步调用。 回调…

WCF资料

Windows Communication Foundation (WCF) and Windows Workflow Foundation (WF) Samples for .NET Framework 4转载于:https://www.cnblogs.com/HQFZ/p/4884243.html

SSH中为什么action需要用多例而dao层和service层为什么就用单例就可以

很简单的道理&#xff0c;就跟你自来水一样&#xff0c;有很多的水龙头&#xff0c;但水管只有一个。为啥要很多水龙头&#xff0c;因为有多个人同时用&#xff1b;为了避免长队&#xff0c;只能这样来分担压力为啥只有一个水管&#xff0c;易维护&#xff0c;集中处理。 使用…

Nagios配置

其实上篇Nogios安装只是安装了Nagios基本组件&#xff0c;虽然能够打开主页&#xff0c;但是如果不配置相关配置文件文件&#xff0c;那么左边菜单很多页面都打不开&#xff0c;相当于只是一个空壳子。接下来&#xff0c;我们来学习研究一下Nagios的配置,了解一下基本的配置和了…

服务器启动时Webapp的web.xml中配置的加载顺序

一 1、启动一个WEB项目的时候&#xff0c;WEB容器会去读取它的配置文件web.xml&#xff0c;读取<listener>和<context-param>两个结点。 2、紧急着&#xff0c;容创建一个ServletContext&#xff08;servlet上下文&#xff09;&#xff0c;这个web项目的所有部分都…

JQuery.autocomplete扩展功能:实现多列自动提示

最近做一个项目&#xff0c;用到了JQuery的自动补全函数&#xff0c;但默认的是只显示一列数据&#xff0c;所以就略加修改&#xff0c;拿出来献丑了。 下面这个是默认调用本地数据&#xff1a; $("#tags").autocomplete(["c","java", &…

用java创建UDF,并用于Hive

典型代码如下&#xff1a; 导入UDF类&#xff1a; import org.apache.hadoop.hive.ql.exec.UDF; public class UpperCassUDF extends UDF{ public String evaluate(String input){ if(inputnull){ return null; } else{ return new String(input.toUpperCase()); } } } 导出jar…

单元素枚举类实现单例模式

本文转载自&#xff1a;点击打开链接 Inspired by Effective Java. Singleton模式是在编程实践中应用最广泛的几种设计模式之一。以前知道的&#xff0c;实现单例的方法有两种(下面的A、B)。刚刚在读《Effective Java的时候》学到一种新的更好的方法(E)&#xff1a;单元素的枚举…

java web开发之 spring单元测试

以前开发web项目从来不喜欢用单元测试&#xff0c;每次都需要启动服务器&#xff0c;在浏览器中调试&#xff0c;有些错误还不一定发现得到。 最近公司开发一个项目&#xff0c;任务繁重&#xff0c;不由觉得以前那种测试模式太笨拙了&#xff0c;于是学习了使用Junit&#xf…

对于SpringMVC框架使用的时候出现“警告: No mapping found for HTTP request with URI [/login]”的问题解决方案...

今天&#xff0c;在myeclipse上导入了前几天的一个项目&#xff0c;但是怎么都运行不起来&#xff0c;可是在别人的电脑上都可以。从早上一直调到了现在&#xff08;期间也看了好多关于此类的帖子&#xff0c;但是都没能解决我的问题&#xff09;&#xff0c;终于找到了解决方案…

MySQL数据库事务中的行级锁,表级锁,页级锁

锁定用于确保事务完整性和数据库一致性。 锁定可以防止用户读取其他用户正在更改的数据&#xff0c;并防止多个用户同时更改相同的数据。 如果不使用锁定&#xff0c;数据库中的数据可能在逻辑上变得不正确&#xff0c;而针对这些数据进行查询可能会产生想不到的结果。 在计算机…