SSH框架和Redis的整合(1)
一个已有的Struts+Spring+Hibernate项目,以前使用MySQL数据库,现在想把Redis也整合进去。
1. 相关Jar文件
下载并导入以下3个Jar文件:
commons-pool2-2.4.2.jar、jedis-2.3.1.jar、spring-data-redis-1.3.4.RELEASE.jar。
2. Redis配置文件
在src文件夹下面新建一个redis.properties文件,设置连接Redis的一些属性。
redis.host=127.0.0.1
redis.port=6379
redis.default.db=1
redis.timeout=100000
redis.maxActive=300
redis.maxIdle=100
redis.maxWait=1000
redis.testOnBorrow=true
再新建一个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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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-4.0.xsd"> <context:property-placeholder location="classpath:redis.properties"/> <bean id="propertyConfigurerRedis"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="1" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="systemPropertiesMode" value="1" />
<property name="searchSystemEnvironment" value="true" />
<property name="locations">
<list>
<value>classpath:redis.properties</value>
</list>
</property>
</bean> <bean id="jedisPoolConfig"
class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean> <bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="usePool" value="true"></property>
<property name="hostName" value="${redis.host}" />
<property name="port" value="${redis.port}" />
<property name="timeout" value="${redis.timeout}" />
<property name="database" value="${redis.default.db}"></property>
<constructor-arg index="0" ref="jedisPoolConfig" />
</bean> <bean id="redisTemplate"
class="org.springframework.data.redis.core.StringRedisTemplate"
p:connectionFactory-ref="jedisConnectionFactory"
>
</bean> <bean id="redisBase" abstract="true">
<property name="template" ref="redisTemplate"/>
</bean> <context:component-scan base-package="com.school.redisclient" /> </beans>
3. Redis类
新建一个com.school.redisclient包,结构如下:
接口IRedisService:
public interface IRedisService<K, V> {
public void set(K key, V value, long expiredTime);
public V get(K key);
public Object getHash(K key, String name);
public void del(K key);
}
抽象类AbstractRedisService,主要是对RedisTemplate进行操作:
public abstract class AbstractRedisService<K, V> implements IRedisService<K, V> { @Autowired
private RedisTemplate<K, V> redisTemplate; public RedisTemplate<K, V> getRedisTemplate() {
return redisTemplate;
} public void setRedisTemplate(RedisTemplate<K, V> redisTemplate) {
this.redisTemplate = redisTemplate;
} @Override
public void set(final K key, final V value, final long expiredTime) {
BoundValueOperations<K, V> valueOper = redisTemplate.boundValueOps(key);
if (expiredTime <= 0) {
valueOper.set(value);
} else {
valueOper.set(value, expiredTime, TimeUnit.MILLISECONDS);
}
} @Override
public V get(final K key) {
BoundValueOperations<K, V> valueOper = redisTemplate.boundValueOps(key);
return valueOper.get();
} @Override
public Object getHash(K key, String name){
Object res = redisTemplate.boundHashOps(key).get(name);
return res;
} @Override
public void del(K key) {
if (redisTemplate.hasKey(key)) {
redisTemplate.delete(key);
}
} }
实现类RedisService:
@Service("redisService")
public class RedisService extends AbstractRedisService<String, String> { }
工具类RedisTool:
public class RedisTool { private static ApplicationContext factory;
private static RedisService redisService; public static ApplicationContext getFactory(){
if (factory == null){
factory = new ClassPathXmlApplicationContext("classpath:redis.xml");
}
return factory;
} public static RedisService getRedisService(){
if (redisService == null){
redisService = (RedisService) getFactory().getBean("redisService");
}
return redisService;
} }
4. 查询功能的实现
新建一个Action:RClasQueryAction,返回Redis里面所有的课程数据。
@SuppressWarnings("serial")
public class RClasQueryAction extends ActionSupport { RedisService rs = RedisTool.getRedisService();
List<Clas> claslist = new ArrayList<Clas>();
Clas c; public String execute(){
if (rs != null){
System.out.println("RedisService : " + rs);
getAllClas();
}
ServletActionContext.getRequest().setAttribute("claslist", claslist);
return SUCCESS;
} private void getAllClas(){
claslist = new ArrayList<Clas>();
int num = Integer.parseInt(rs.get("clas:count"));
for (int i=0; i<num; i++){
String cid = "clas:" + (i+1);
c = new Clas();
int id = Integer.parseInt(String.valueOf(rs.getHash(cid, "ID")));
c.setId(id);
System.out.println("ID:" + id);
String name = (String) rs.getHash(cid, "NAME");
c.setName(name);
System.out.println("NAME:" + name);
String comment = (String) rs.getHash(cid, "COMMENT");
c.setComment(comment);
System.out.println("COMMENT:" + comment);
claslist.add(c);
}
} }
Struts的设置和jsp文件就不详细讲了。
5. Redis数据库
Redis数据库里面的内容(使用的是Redis Desktop Manager):
最后是运行结果:
当然,这只是实现了从Redis查询数据,还没有实现将Redis作为MySQL的缓存。
SSH框架和Redis的整合(1)的更多相关文章
- SSH框架和Redis的整合(2)
5. 添加功能的实现 新建一个Action:RClasAction,实现向Redis添加课程数据,并同步到MySQL. package com.school.action; import java.u ...
- J2EE进阶(六)SSH框架工作流程项目整合实例讲解
J2EE进阶(六)SSH框架工作流程项目整合实例讲解 请求流程 经过实际项目的进行,结合三大框架各自的运行机理可分析得出SSH整合框架的大致工作流程. 首先查看一下客户端的请求信息: 对于一个Web项 ...
- SSH框架整合
SSH框架整合 一.原理图 action:(struts2) 1.获取表单的数据 2.表单的验证,例如非空验证,email验证等 3.调用service,并把数据传递给service Service: ...
- ssh框架整合---- spring 4.0 + struts 2.3.16 + maven ss整合超简单实例
一 . 需求 学了这么久的ssh,一直都是别人整合好的框架去写代码,自己实际动手时才发现框架配置真是很坑爹,一不小心就踏错,真是纸上得来终觉浅! 本文将记录整合struts + spring的过程 , ...
- dwr与ssh框架整合教程
(1)dwr与ssh框架整合教程dwr框架介绍. DWR(Direct Web Remoting)是一个用于改善web页面与Java类交互的远程服务器端Ajax开源框架,可以帮助开 发人员开发包含AJ ...
- [Java] SSH框架笔记_框架整合示例(一)
本文描述的是框架SSH集成的示例,由于在这个过程中有一些小的细节容易被遗忘,特别撰写了一篇小的博文来记录这个过程,希望对自己以及后来者能够起到积极意义. 本文中使用的框架和版本号为: struts-2 ...
- 【转载】SSH框架总结(将网上朋友写的给整合了下)
一.Struts 在没有学习SSH框架前,我们一般采用Jsp+javabean+servlet开发,这里就是MVC架构.而Struts其实就是替代了Servlet,我们知道Servlet在一般的开发中 ...
- ssh框架整合之登录以及增删改查
1.首先阐述一下我用得开发工具,myeclipse2017+oracle,所以我的基本配置步骤可能不一样,下面我用几张图来详解我的开发步骤. ---1先配置structs (Target 选择apac ...
- Struts2+Spring+Hibernate实现员工管理增删改查功能(一)之ssh框架整合
前言 转载请标明出处:http://www.cnblogs.com/smfx1314/p/7795837.html 本项目是我写的一个练习,目的是回顾ssh框架的整合以及使用.项目介绍: ...
随机推荐
- 代码的坏味道(15)——冗余类(Lazy Class)
坏味道--冗余类(Lazy Class) 特征 理解和维护类总是费时费力的.如果一个类不值得你花费精力,它就应该被删除. 问题原因 也许一个类的初始设计是一个功能完全的类,然而随着代码的变迁,变得没什 ...
- Flex 布局教程:实例篇
该教程整理自 阮一峰Flexible教程 今天介绍常见布局的Flex写法.你会看到,不管是什么布局,Flex往往都可以几行命令搞定. 我的主要参考资料是Landon Schropp的文章和Solved ...
- Android开发学习—— Fragment
#Fragment* 用途:在一个Activity里切换界面,切换界面时只切换Fragment里面的内容* 生命周期方法跟Activity一致,可以理解把其为就是一个Activity* 定义布局文件作 ...
- Atitit 管理原理与实践attilax总结
Atitit 管理原理与实践attilax总结 1. 管理学分类1 2. 我要学的管理学科2 3. 管理学原理2 4. 管理心理学2 5. 现代管理理论与方法2 6. <领导科学与艺术4 7. ...
- docker4dotnet #1 – 前世今生 & 世界你好
作为一名.NET Developer,这几年看着docker的流行实在是有些眼馋.可惜的是,Docker是基于Linux环境的,眼瞧着那些 java, python, node.js, go 甚至连p ...
- Microsoft Visual Studio 2017 for Mac Preview 下载+安装+案例Demo
目录: 0. 前言 1. 在线安装器 2. 安装VS 3. HelloWorld 4. ASP.NET MVC 5. 软件下载 6. 结尾 0. 前言: 工作原因,上下班背着我的雷神,一个月瘦了10斤 ...
- 自定义ConfigSection
CCustom configuration section with intelisense
- MapReduce
2016-12-21 16:53:49 mapred-default.xml mapreduce.input.fileinputformat.split.minsize 0 The minimum ...
- 【C#】获取网页内容及HTML解析器HtmlAgilityPack的使用
最近经常需要下载一些东西,而这个下载地址又会经过层层跳转,每个页面上都有很多广告,烦不胜烦,所以做了一个一键获得最终下载地址的小工具.使用C#,来获取网页内容,然后通过HtmlAgilityPack获 ...
- ASP.NET MVC Model绑定(一)
ASP.NET MVC Model绑定(一) 前言 ModelMetadata系列的结束了,从本篇开始就进入Model绑定部分了,这个系列阅读过后你会对Model绑定有个比较清楚的了解, 本篇对于Mo ...