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的整合
为什么80%的码农都做不了架构师?>>> 一个已有的Struts+Spring+Hibernate项目,以前使用MySQL数据库,现在想把Redis也整合进去. 1. 相关Ja ...
- ssh mysql环境搭建 myeclipse_MyEclipse整合ssh三大框架环境搭载用户注册源码下载
前言 SSH不是一个框架,而是多个框架(struts+spring+hibernate)的集成,是目前较流行的一种Web应用程序开源集成框架,用于构建灵活.易于扩展的多层Web应用程序. 集成SSH框 ...
- SSH框架整合-慕课课程
SSH框架整合-慕课课程 学习时间:2018年12月3日 慕课链接:基于SSH实现员工管理系统之框架整合篇 内容:Struts2+Hibernate+Spring框架整合,分为Struts2整合Spr ...
- java web的ssh框架_JavaWeb_(SSH论坛)_二、框架整合
基于SSH框架的小型论坛项目 一.项目入门 传送门 二.框架整合 传送门 三.用户模块 传送门 四.页面显示 传送门 五.帖子模块 传送门 六.点赞模块 传送门 七.辅助模块传送门 导入Jar包 导入 ...
- 【struts2+spring+hibernate】ssh框架整合开发
SSH框架整合 1 Struts2+Spring+Hibernate导包 Struts2导入jar包: * struts2/apps/struts2-blank.war/WEB-INF/lib/*.j ...
- SSH框架整合——基于注解
SSH框架整合--基于注解 @(Spring)[Spring, hibernate, struts2, 框架整合] SSH框架整合基于注解 SSH框架整合 第一步导入Jar包 第二步导入配置文件 第三 ...
- SSH框架整合——基于XML配置文件
SSH框架整合--基于XML配置文件 @(Spring)[spring, struts2, hibernate, 框架整合, ssh, Spring] SSH框架整合基于XML配置文件 SSH框架整合 ...
- 6.SSH框架整合及简单使用示例
6.SSH框架整合 ssh:spring+spring-mvc+hibernate 6.1 整合的场所 :web.xml 跟 5.ssm框架 整合类似,可以对照学习,通过监听器配置hibernate, ...
- SSH框架搭建整合详细步骤及运行流程
准备整合环境 数据库环境 MySQL 数据库中创建一个名称为 ssh 的数据库,并在数据库中创建一个名称为 user 的表 配置 Struts2 环境 1.创建项目并导入 Struts2 框架所需的 ...
最新文章
- CSS3选择器 :read-only选择器 CSS3选择器 :read-write选择器
- Hive数据分析——Spark是一种基于rdd(弹性数据集)的内存分布式并行处理框架,比于Hadoop将大量的中间结果写入HDFS,Spark避免了中间结果的持久化...
- 如何为Apache JMeter开发插件(一)
- 第五章 shell学习之文件的排序、合并和分割
- ETL数据导入/导出工具 HData
- linux ldd命令详解
- 【优化求解】基于布谷鸟算法CS实现单目标求解matlab源码
- 一行Python代码玩转emoji表情,已经玩坏了都...
- 服务器集成显卡性能,主流显卡参数和性能对比
- 4.3.5 导频插入
- ECMAScript6 Proxy和Reflect 对象操作拦截以及自定义
- 央行征信中心:第二代个人征信系统将于1月20日上线
- Ubuntu18.04安装搜狗输入法使用时不能输入中文
- pid file /opt/zbox/tmp/apache/httpd.pid overwritten -- Unclean shutdown of previous Apache run?
- MacBook将大文件分割成很多个小文件split命
- html 拉伸幕,html 对 div 进行 拉伸 拖拽
- 理解RTF和RTX指标
- 【GANs学习笔记】(十六)CGAN、TRIPLEGAN
- 《数据结构》-图的邻接表表示法(四)
- #pragma DATA_SECTION的使用
热门文章
- html页面返回json数据为空,在Html页面中,展示出Json格式数据,且保持缩进格式...
- java spring 面向切面编程_Spring Boot实战系列(3)AOP面向切面编程
- android 通过短信找回密码,我的android手机短信被我加密了,忘记密码了,在不刷机恢复出厂设置的情况下如何解决?急急急!!!!!...
- edge打开pdf不显示印章_SumatraPDF - 免费轻量的 PDF 阅读器
- linux环境c语言课程设计,linux环境下c语言编程课程设计
- python中argsort_浅述python中argsort()函数的实例用法
- hint oracle qbname_从才oracle中找到所有列名为BANK_ACC,且BANK_ACC=000的项,并将BANK_ACC=000000的项修改为BANK_ACC=111...
- javascript内存泄漏调试工具mac_Vuex3.1.1更新:支持jsDelivr,修复内存泄漏
- php恋爱,突然想到php程序和谈恋爱很相似
- 2.7 Inception 网络