zoukankan      html  css  js  c++  java
  • SSH框架和Redis的整合(2)

    5. 添加功能的实现

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

    package com.school.action;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    
    import com.opensymphony.xwork2.ActionSupport;
    import com.school.entity.Clas;
    import com.school.redisclient.RedisService;
    import com.school.redisclient.RedisTool;
    import com.school.service.ClasService;
    
    @SuppressWarnings("serial")
    public class RClasAction extends ActionSupport {
        
        @Autowired
        private ClasService clasService;
        
        RedisService rs = RedisTool.getRedisService();
        List<Clas> claslist = new ArrayList<Clas>();
        
        private Clas clas;
        public Clas getClas() {
            return clas;
        }    
        public void setClas(Clas Clas) {
            this.clas = Clas;
        }
        
        public String execute(){
            saveClas(clas);
            return SUCCESS;
        }
        
        @SuppressWarnings({ "rawtypes", "unchecked" })
        private void saveClas(Clas c){
            List<String> ids = rs.getList("clas:id");
            // clas:id
            int num = ids.size();
            int id = Integer.parseInt(ids.get(num-1)) + 1;
            rs.rightPushList("clas:id", String.valueOf(id));
            // clas:count
            int count = Integer.parseInt(rs.get("clas:count"));
            rs.set("clas:count", String.valueOf(count+1), -1);
            // 增加
            HashMap hashmap = new HashMap();
            hashmap.put("ID", String.valueOf(id));
            hashmap.put("NAME", clas.getName());
            hashmap.put("COMMENT", clas.getComment());
            rs.addHash("clas:" + id, hashmap);
            // 同步到MySQL
            clasService.saveClas(clas);
        }
    
    }

    clas:id是一个List类型的Key-Value,记录了所有的课程ID,取出最后一个ID,再+1,作为增加的课程的ID,同时clas:count的值也要+1。使用addHash()方法向Redis添加了一个Hash类型的Key-Value(也就是一门课程):

            @SuppressWarnings({ "unchecked", "rawtypes" })
            public synchronized void addHash(K key, HashMap map){
                redisTemplate.opsForHash().putAll(key, map);
            }

    同时将该门课程增加到MySQL。

    6. 删除功能的实现

    新建一个Action:RClasDeleteAction,实现删除Redis的课程数据,并同步到MySQL。

    package com.school.action;
    
    import org.springframework.beans.factory.annotation.Autowired;
    
    import com.opensymphony.xwork2.ActionSupport;
    import com.school.redisclient.RedisService;
    import com.school.redisclient.RedisTool;
    import com.school.service.ClasService;
    
    @SuppressWarnings("serial")
    public class RClasDeleteAction extends ActionSupport {
        
        @Autowired
        private ClasService clasService;
        
        RedisService rs = RedisTool.getRedisService();
        
        private int id;
        public int getId(){
            return id;
        }
        public void setId(int id){
            this.id=id;
        }
        
        public String execute(){    
            deleteClas(id);
            // 同步到MySQL
            clasService.deleteClas(id);
            return SUCCESS;
        }
        
        private void deleteClas(int id){
            // 删除
            rs.del("clas:" + id);
            // clas:count
            int count = Integer.parseInt(rs.get("clas:count"));
            rs.set("clas:count", String.valueOf(count-1), -1);
            // clas:id
            rs.delListItem("clas:id", String.valueOf(id));
        }
    
    }

    直接删除clas:id,再将clas:count的值-1,这两步比较简单,复杂的是从clas:id中删除该课程的ID,使用了delListItem()方法来实现:

            @Override
            public synchronized void delListItem(K key, V value){
                redisTemplate.opsForList().remove(key, 1, value);
            }

    redisTemplate.opsForList().remove()方法类似于LREM命令。最后在MySQL中也删除相同的课程。

    7. 修改功能的实现

    新建一个Action:RClasUpdateAction,实现删除Redis的课程数据,并同步到MySQL。

    package com.school.action;
    
    import java.util.HashMap;
    
    import org.springframework.beans.factory.annotation.Autowired;
    
    import com.opensymphony.xwork2.ActionSupport;
    import com.school.entity.Clas;
    import com.school.redisclient.RedisService;
    import com.school.redisclient.RedisTool;
    import com.school.service.ClasService;
    
    @SuppressWarnings("serial")
    public class RClasUpdateAction extends ActionSupport{
        
        @Autowired
        private ClasService clasService;
        
        RedisService rs = RedisTool.getRedisService();
        
        private Clas clas;
        public Clas getClas() {
            return clas;
        }
        public void setClas(Clas clas) {
            this.clas = clas;
        }
        
        @SuppressWarnings({ "unchecked", "rawtypes" })
        public String execute(){
            HashMap hashmap = new HashMap();
            hashmap.put("ID", String.valueOf(clas.getId()));
            hashmap.put("NAME", clas.getName());
            hashmap.put("COMMENT", clas.getComment());
            rs.putHash("clas:" + clas.getId(), hashmap);
            // 同步到MySQL
            clasService.updateClas(clas);
            return SUCCESS;
        }
    
    }

    使用了putHash()方法来更新:

            @SuppressWarnings({ "rawtypes", "unchecked" })
            @Override
            public synchronized void putHash(K key, HashMap map){
                redisTemplate.boundHashOps(key).putAll(map);
            }

    同时在MySQL做相同的更新操作。

  • 相关阅读:
    YAML 语法小结
    小程序之脚本语言
    小程序WXML 使用小结
    微信小程序 js逻辑
    小程序开发1
    联想Y7000安装Ubuntu16.04/Win10双系统,wifi问题,显卡驱动和CUDA10安装
    VS2015中配置Eigen
    联想Y700安装显卡驱动和CUDA8.0
    php微信生成微信公众号二维码扫描进入公众号带参数
    Y7000 (1)安装ubuntu1604遇到的问题
  • 原文地址:https://www.cnblogs.com/mstk/p/6252747.html
Copyright © 2011-2022 走看看