• ssm整合redis&kafka&es


    package com.bw.controller;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.List;
    import java.util.UUID;
    
    import javax.servlet.http.HttpServletRequest;
    
    import org.aspectj.apache.bcel.classfile.Field;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.kafka.core.KafkaTemplate;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.multipart.MultipartFile;
    
    import com.alibaba.fastjson.JSON;
    import com.bw.bean.Sedan;
    import com.bw.service.SedanService;
    import com.bw.utils.HLUtils;
    import com.github.pagehelper.PageInfo;
    
    @Controller
    public class MyController {
    
        @Autowired
        SedanService sedanService;
        
        @Autowired
        RedisTemplate redisTemplate;
        
        @Autowired
        ElasticsearchTemplate elasticsearchTemplate;
        
        @Autowired
        KafkaTemplate<String, String> kafkaTemplate;
        
        @RequestMapping("sousuo")
        public Object sousuo(Model model,String keyword,@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "3")Integer pageSize) {
            
            PageInfo<Sedan> info = (PageInfo<Sedan>) HLUtils.findByHighLight(elasticsearchTemplate, Sedan.class, pageNum, pageSize, new String[] {"name","typea"}, "id", keyword);
            
            model.addAttribute("info", info);
            model.addAttribute("key", keyword);
            
            return "sousuo";
        }
        
        @SuppressWarnings("unchecked")
        @RequestMapping("list")
        public String list(Model model,@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "3")Integer pageSize) {
            
            if (pageNum>1) {
                PageInfo<Sedan> info = sedanService.info(pageNum, pageSize);
                
                model.addAttribute("info", info);
            }
            if (pageNum==1) {
                
                //先看redis中有没有
                List<Sedan> list = redisTemplate.opsForList().range("sedan", 0, -1);
                if (list==null||list.size()==0) {
                    PageInfo<Sedan> info = sedanService.info(pageNum, pageSize);
                    System.err.println("从mysql中查询了数据!!!!!!!!!!!!!");
                    
                    redisTemplate.opsForList().leftPushAll("sedan", info.getList().toArray());
                    model.addAttribute("info", info);
                }else {
                    //redis数据不为空
                    System.err.println("从redis中查出了数据");
                    PageInfo<Sedan> info = sedanService.info(pageNum, pageSize);
                    model.addAttribute("info", info);
                }
            }
            
            
            return "list";
        }
        
        @RequestMapping("toadd")
        public Object toadd() {
            
            return "add";
        }
        
        @RequestMapping("save")
        public Object add(Sedan sedan,HttpServletRequest request,MultipartFile myFile) throws IllegalStateException, IOException {
            
            String startName = UUID.randomUUID().toString();
            
            String url=request.getRealPath("/load/");
            
            File file = new File(url+startName);
            
            myFile.transferTo(file);
            
            sedan.setPic(startName);
            
            sedanService.save(sedan);
            
            String jsonString = JSON.toJSONString(sedan);
            
            kafkaTemplate.send("sedan","add="+jsonString);
            System.err.println("发送卡夫卡消息!!!!!");
            
            return "redirect:list";
        }
        
        @RequestMapping("toUpdate")
        public Object toUpdate(int id,Model model) {
            
            Sedan sedan=sedanService.selectOne(id);
            
            model.addAttribute("sedan", sedan);
            
            return "update";
        }
        
        @RequestMapping("update")
        public Object update(Sedan sedan,HttpServletRequest request,MultipartFile myFile) throws IllegalStateException, IOException {
            
            String startName = UUID.randomUUID().toString();
            
            String url=request.getRealPath("/load/");
            
            File file = new File(url+startName);
            
            myFile.transferTo(file);
            
            sedan.setPic(startName);
            
            sedanService.update(sedan);
            
            String jsonString = JSON.toJSONString(sedan);
            kafkaTemplate.send("sedan","update="+jsonString);
            
            return "redirect:list";
        }
        
        @RequestMapping("seare")
        public Object seare(Model model,int id) {
            
            Sedan sedan = sedanService.selectOne(id);
            model.addAttribute("sedan", sedan);
            return "show";
        }
    }
  • 相关阅读:
    error C4430: missing type specifier int assumed. Note: C++ does not support defaultint 解决方法
    A performance evaluation of local descriptors——局部描述子评估译文(1,2,...)
    global mapper裁剪DEM文件的方法
    直方图学习笔记
    vc++如何将客户区存为bmp【zz】
    处理tiff格式dem数据的方法——输出xyz坐标
    处理tiff格式dem数据的方法——输出txt形式高程信息
    我的社区成立了
    Closed socket connection for client /39.103.162.230:56100 (no session established for client)
    Will not attempt to authenticate using SASL (unknown error) (org.apache.zookeeper.ClientCnxn)
  • 原文地址:https://www.cnblogs.com/tang0125/p/12693694.html
Copyright © 2020-2023  润新知