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框架的整合以及使用.项目介绍: ...
随机推荐
- Ajax使用WCF实现小票pos机打印源码
通过ajax跨域方式调用WCF服务,实现小票pos机的打印,源码提供web方式,客户端方式测试,服务驻留右侧底部任务栏,可控制服务开启暂停,用户可自定义小票打印模板,配合零售录入. qq 22945 ...
- BPM配置故事之案例9-根据表单数据调整审批线路2
老李:好久不见啊,小明. 小明:-- 老李:不少部门有物资着急使用,现在的审批流程太慢了,申请时增加一个是否加急的选项吧.如果选加急,金额1000以下的直接到我这里,我审批完就通过,超过1000的直接 ...
- KOTLIN开发语言文档(官方文档) -- 2.基本概念
网页链接:https://kotlinlang.org/docs/reference/basic-types.html 2. 基本概念 2.1. 基本类型 从可以在任何变量处理调用成员函数和属性 ...
- Android中的flexboxlayout布局
提到FlexboxLayout大家估计有点模糊,它是谷歌最近开源的一个android排版库,它的前身Flexbox是2009年W3C提出了一种新的布局,可以简便.完整.响应式的实现页面布局,Flexb ...
- BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]
4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...
- ASP.NET 5 改名 ASP.NET Core 1.0
今天,Scott Hanselman在其博客上宣布<ASP.NET 5 is dead - Introducing ASP.NET Core 1.0 and .NET Core 1.0>, ...
- 斗天斗地斗控件 -- 与 Flyout 控件的斗争史
前言 在淘宝的收藏夹页面本地化过程中,收藏的宝贝和店铺的分类展示通过一个下拉菜单的方式进行展示.如果单独为此从头重写一个控件,那么不但费时费力,包括所有的动画方式都要全新设计,而且还容易出 bug.好 ...
- On cloud, be cloud native
本来不想起一个英文名,但是想来想去都没能想出一个简洁地表述该意思的中文释义,所以就用了一个英文名称,望见谅. Cloud Native是一个刚刚由VMware所提出一年左右的名词.其表示在设计并实现一 ...
- MyBatis5:MyBatis集成Spring事物管理(上篇)
前言 有些日子没写博客了,主要原因一个是工作,另一个就是健身,因为我们不仅需要努力工作,也需要有健康的身体嘛. 那有看LZ博客的网友朋友们放心,LZ博客还是会继续保持更新,只是最近两三个月LZ写博客相 ...
- SpringMVC的Action在同一时间里只允许同一个浏览器的单次进入?
最近用SpringMVC写了一个很简单的测试程序,代码如下: @Controller public class LongTimeTaskController { @RequestMapping(val ...