• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • MsSql
  • Mysql
  • oracle
  • MariaDB
  • DB2
  • SQLite
  • PostgreSQL
  • MongoDB
  • Redis
  • Access
  • 数据库其它
  • sybase
  • HBase
您的位置:首页 > 数据库 >Redis > 详解SSH框架和Redis的整合

详解SSH框架和Redis的整合

作者:MSTK 字体:[增加 减小] 来源:互联网 时间:2017-05-11

MSTK通过本文主要向大家介绍了ssh框架详解,ssh框架配置详解,ssh框架配置文件详解,ssh命令详解,ssh详解等相关知识,希望本文的分享对您有所帮助

一个已有的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 
</div>

再新建一个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>

</div>

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);   
} 

</div>

抽象类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); 
   } 
  } 
  
 } 

</div>

实现类RedisService:

@Service("redisService") 
public class RedisService extends AbstractRedisService<String, String> { 
 
}
</div>

工具类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;
 }

}
</div>

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);
  }
 }

}
</div>

Struts的设置和jsp文件就不详细讲了。

5. Redis数据库

Redis数据库里面的内容(使用的是Redis Desktop Manager):

最后是运行结果:

当然,这只是实现了从Redis查询数据,还没有实现将Redis作为MySQL的缓存。

5. 添加功能的实现

新建一个Action:RClasAction,实现向Redis添加课程数据,并同步到MySQL。

package com.school.action;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.springframework.
  


 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • 详解SSH框架和Redis的整合

相关文章

  • 2017-05-11phpredis提高消息队列的实时性方法(推荐)
  • 2017-05-11NoSQL和Redis简介及Redis在Windows下的安装和使用教程
  • 2017-05-11Redis配置文件详解
  • 2017-05-11windows环境下Redis+Spring缓存实例讲解
  • 2017-05-11解锁redis锁的正确姿势
  • 2017-05-11Redis教程(六):Sorted-Sets数据类型
  • 2017-05-11Redis教程(七):Key操作命令详解
  • 2017-05-11CentOS 6.6下Redis安装配置记录
  • 2017-05-11在CentOS 7环境下安装Redis数据库详解
  • 2017-05-11Redis教程(十三):管线详解

文章分类

  • MsSql
  • Mysql
  • oracle
  • MariaDB
  • DB2
  • SQLite
  • PostgreSQL
  • MongoDB
  • Redis
  • Access
  • 数据库其它
  • sybase
  • HBase

最近更新的内容

    • Redis中5种数据结构的使用场景介绍
    • Redis数据库的使用场景介绍(避免误用Redis)
    • Redis String 类型和 Hash 类型学习笔记与总结
    • Redis总结笔记(二):C#连接Redis简单例子
    • Redis 集群搭建和简单使用教程
    • 浅谈redis采用不同内存分配器tcmalloc和jemalloc
    • 详解用Redis实现Session功能
    • Redis 命令整理并说明如何使用
    • Redis的Python客户端redis-py安装使用说明文档
    • Redis Sentinel服务配置流程(详解)

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有