es+redis+kafka 文件上传等应用

1,controller层

@Controller
public class PlanController {
@Autowired
private PlanService planService;

@Autowired
private RedisTemplate redisTemplate;

@Autowired
private KafkaTemplate<String, String> kafkaTemplate;

@Autowired
private ElasticsearchTemplate elasticsearchTemplate;

@RequestMapping("list")
public String list(MultipartFile myFile,Model m,@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "5")Integer pageSize,String key) {

if (pageNum>1) {
PageInfo<Plan> info = planService.findAll(pageNum, pageSize);
m.addAttribute("info", info);
}

if (pageNum==1) {
List<Plan> redisPlans = redisTemplate.opsForList().range("zhunneng", 0, -1);


if (redisPlans==null || redisPlans.size()==0) {
PageInfo<Plan> info = planService.findAll(pageNum, pageSize);
System.out.println("从mysql中查询了数据");

redisTemplate.opsForList().rightPushAll("zhunneng", info.getList().toArray());
m.addAttribute("info", info);
}else {
System.out.println("从redis中查询了数据");
m.addAttribute("info", new PageInfo<>(redisPlans));
}

}

return "list";
}

@RequestMapping("toAdd")
public String toAdd() {
return "add";
}

@RequestMapping("search")
public String search(Model m,@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "5")Integer pageSize,String key) {
@SuppressWarnings("unchecked")
PageInfo<Plan> info = (PageInfo<Plan>) HLUtils.findByHighLight(elasticsearchTemplate, Plan.class, pageNum, pageSize, new String[]{"manager"},"id", key);
m.addAttribute("info", info);
m.addAttribute("key", key);
return "list";
}

@RequestMapping("add")
public String add(Plan plan) {
if (planService.add(plan)) {
String jsonString = JSON.toJSONString(plan);
kafkaTemplate.send("1711F","save="+jsonString);
return "redirect:list";
}
return "add";
}

@RequestMapping("del")
public String del(String ids,Model m) {
if (planService.del(ids)) {
kafkaTemplate.send("1711F","del="+ids);
try {
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
return "redirect:list";
}
return "list";
}

@RequestMapping("toUpd")
public String toUpd(Integer id,Model m) {
Plan plan = planService.selectPlan(id);
m.addAttribute("plan", plan);

return "upd";
}

@RequestMapping("upd")
public String upd(Plan plan) {
if (planService.upd(plan)) {
String jsonString = JSON.toJSONString(plan);
kafkaTemplate.send("1711F","upd="+jsonString);
try {
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
return "redirect:list";
}
return "upd";
}
}

 

2,listen监听器

public class MsgListener implements MessageListener<String , String> {

@Autowired
private RedisTemplate redisTemplate;

@Autowired
private PlanRepository planRepository;

@Override
public void onMessage(ConsumerRecord<String, String> data) {
String msg = data.value();
System.err.println("收到了消息");
if (msg.startsWith("save")) {
String[] split = msg.split("=");
String jsonString = split[1];
Plan plan = JSON.parseObject(jsonString, Plan.class);
planRepository.save(plan);
System.err.println("同步es索引库成功!!!");
}

if (msg.startsWith("del")) {
String[] split = msg.split("=");
String ids = split[1];
String[] id = ids.split(",");
for (String string : id) {
int id1 = Integer.parseInt(string);
planRepository.deleteById(id1);
redisTemplate.delete("zhunneng");
}
System.err.println("删除es索引库成功");
}

if (msg.startsWith("upd")) {
String[] split = msg.split("=");
Plan plan = JSON.parseObject(split[1],Plan.class);
planRepository.save(plan);
redisTemplate.delete("zhunneng");
System.err.println("修改es索引库成功");
}

}

}

 

3,es高亮搜索 只需继承ElasticsearchCrudRepository类  其他查询可自己定义方法

public interface PlanRepository extends ElasticsearchCrudRepository<Plan, Integer>{

}

 

4.文件上传  前台form表单  加上enctype="multipart/form-data"

@RequestMapping("add")
public String add(MultipartFile myFile,@Validated@ModelAttribute Ce ce,BindingResult br,HttpServletRequest request,Model m)throws Exception {

if (br.hasErrors()) {
List<Type> types = ceService.findByType();

m.addAttribute("types", types);
m.addAttribute("ce", ce);
return "add";
}

String path = "d:/pic";

String filename = myFile.getOriginalFilename();
String start = UUID.randomUUID().toString();
String end = filename.substring(filename.lastIndexOf("."));

String newName = start + end;

File file = new File(path,newName);

myFile.transferTo(file);
ce.setPic(newName);

}

 

全选全不选

function quan(){
for(i in $("[name=id]")){
$("[name=id]")[i].checked = $("[name=q]")[0].checked;
}
}

<input type="checkbox" name="q" onclick="quan()">

<input type="checkbox" name="id">

<input type="checkbox" name="id">

<input type="checkbox" name="id">

 

上一篇:牛客网The Flee Plan of Groundhog


下一篇:SAP PM 入门系列之19 - IP31 Maintenance Plan Costing