已知一个已有的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的缓存。

java三大框架项目和Redis组合使用的更多相关文章

  1. Java三大框架 介绍

    三大框架:Struts+hibernate+spring Java三大框架主要用来做WEN应用. Struts主要负责表示层的显示 Spring利用它的IOC和AOP来处理控制业务(负责对数据库的操作 ...

  2. java 三大框架 介绍

    三大框架:Struts+Hibernate+Spring Java三大框架主要用来做WEN应用. Struts主要负责表示层的显示 Spring利用它的IOC和AOP来处理控制业务(负责对数据库的操作 ...

  3. [转]JAVA三大框架SSH和MVC

    Java—SSH(MVC) JAVA三大框架的各自作用  hibernate是底层基于jdbc的orm(对象关系映射)持久化框架,即:表与类的映射,字段与属性的映射,记录与对象的映射 数据库模型 也就 ...

  4. JAVA三大框架SSH的各自作用

        一.Spring Spring是一个解决了许多在J2EE开发中常见的问题的强大框架. Spring提供了管理业务对象的一致方法并且鼓励了注入对接口编程而不是对类编程的良好习惯. Spring的 ...

  5. java三大框架介绍

    常听人提起三大框架,关于三大框架,做了如下了解: 三大框架:Struts+Hibernate+Spring java三大框架主要用来做WEN应用. Struts主要负责表示层的显示 Spring利用它 ...

  6. JAVA三大框架SSH和MVC

    Java—SSH(MVC) JAVA三大框架的各自作用    hibernate是底层基于jdbc的orm(对象关系映射)持久化框架,即:表与类的映射,字段与属性的映射,记录与对象的映射 数据库模型 ...

  7. JAVA三大框架的各自作用

    http://christhb.blog.163.com/blog/static/98982492011727114936239/ 一.Spring Spring是一个解决了许多在J2EE开发中常见的 ...

  8. java 搭建新项目,最佳组合:spring boot + mybatis generator

    java 搭建新项目,最佳组合:spring boot + mybatis generator

  9. 浅谈Java三大框架与应用

    前言:对于一个程序员来说,尤其是在java web端开发的程序员,三大框架:Struts+Hibernate+Spring是必须要掌握熟透的,因此,下面谈谈java三大框架的基本概念和原理. JAVA ...

随机推荐

  1. Judge Route Circle

    Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot m ...

  2. 我的three.js学习记录(一)

    在之前因为项目需要使用WebGL技术做网页应用,但是苦于自己没有接触,只是使用过OpenGL.然后接触到了thre.js这个第三方库之后我突然心情很愉快,这将节省我很多时间. 过了这个项目之后,就再也 ...

  3. 【笔记】如何查看HTTP请求头&&【实验吧】天下武功唯快不破

    打开Chrome浏览器,点击右上角“三”按钮. 点击工具-----再点击开发者工具   找到Network选项框.以百度经验页面为例,点击任务选框来查看网络请求流   在Network框内会有所有的请 ...

  4. 使用VLC创建组播流

    vlc既是一个播放器,又可以成为一个流媒体服务器.最近需要做udp组播播放相关的东西,需要先在本地搭建一个udp组播服务器,因为机器上本来就装有vlc,所以就用它了. 第一步: 点击媒体->流 ...

  5. es6零基础学习之构建脚本(二)

    编译器打开你的es6项目 首先:创建我们的第一个脚本,tasks/util/args.js      在文件里面要先引入一个包,处理命令行参数 import yargs from 'yargs'; / ...

  6. 【转载】quickLayout.css-快速构建结构兼容的web页面

    文章转载自 张鑫旭-鑫空间-鑫生活 http://www.zhangxinxu.com/wordpress/ 原文链接:http://www.zhangxinxu.com/wordpress/?p=4 ...

  7. db2备份还原

    还原步骤:创建好数据库后进入该数据库 .restore db TSMESDB from D:\ICSS\dbData on D:\ICSS\dbData  into TSMESDB redirect. ...

  8. 笨鸟先飞之ASP.NET MVC系列之过滤器(05结果过滤器)

    概念介绍 结果过滤器看名字就知道这个过滤器是针对方法所产生结果的,结果过滤器,主要在我们的动作方法结果返回前后执行. 如果我们需要创建结果过滤器需要实现IResultFilter接口. namespa ...

  9. ArcGis for flex查询FeatureLayer数据

    1. 首先实例化一个FeatureLayer对象 private var featureLayer:FeatureLayer=new FeatureLayer(); 2.指定FeatureLayer对 ...

  10. IDEA搭建SpringMVC+Mybatis+Mysql+Maven框架

    相关环境 Intellij IDEA Ultimate Tomcat JDK MySql 5.6(win32/win64) Maven (可使用Intellij IDEA自带的) 搭建步骤 创建项目工 ...