双表的增删改查-controller

package com.lzl.controller;

import java.io.IOException;
import java.util.List;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageInfo;
import com.lzl.pojo.Doctor;
import com.lzl.service.DoctorService;


@Controller
public class DoctorController {

@Reference
DoctorService service;

@RequestMapping("list")
public String list(Model m,@RequestParam(defaultValue = "1")Integer pageNum,
@RequestParam(defaultValue = "3")Integer pageSize,Doctor doctor) {

PageInfo<Doctor> info = service.findAll(pageNum, pageSize, doctor);

List<Doctor> list = service.findProvince();

List<Doctor> findDepart = service.findDepart();
m.addAttribute("info", info);
m.addAttribute("province", list);
m.addAttribute("listDepart", findDepart);
m.addAttribute("doctor", doctor);


return "list";
}

/**
* 回显省市区
* @param id
* @return
*/
@RequestMapping("city")
@ResponseBody
public JSONArray city(Integer id) {
List<Doctor> list = service.findTown(id);
return JSONArray.parseArray(JSON.toJSONString(list));
}
@GetMapping("add")
public String getAdd(Model model) {
//查找省
List<Doctor> list = service.findProvince();
//回显科室
List<Doctor> listDepart = service.findDepart();
//查到擅长所有的数据
List<Doctor> shanchang = service.findByShanChang();
model.addAttribute("province", list);
model.addAttribute("listDepart", listDepart);
model.addAttribute("shanchang", shanchang);
return "add";
}
@PostMapping("add")
@ResponseBody
public int postAdd(Doctor doctor,MultipartFile file) {
try {
String processFile = FileUtils.processFile(file);
doctor.setTouxiang(processFile);
} catch (IllegalStateException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int i = service.add(doctor);
return i;

}
@GetMapping("update")
public String getUpdate(Model model,Integer id) {
//查找所有的数据
Doctor doctor = service.findById(id);
//查找省
List<Doctor> list = service.findProvince();
//回显科室
List<Doctor> listDepart = service.findDepart();
//查到擅长所有的数据
List<Doctor> shanchang = service.findByShanChang();
model.addAttribute("province", list);
model.addAttribute("listDepart", listDepart);
model.addAttribute("shanchang", shanchang);
model.addAttribute("d", doctor);
return "update";
}
@RequestMapping("update")
@ResponseBody
public int postUpdate(Doctor doctor,MultipartFile file) {
try {
String processFile = FileUtils.processFile(file);
System.err.println(processFile);
doctor.setTouxiang(processFile);
System.err.println(doctor);

} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int i = service.update(doctor);
return i;
}

}

原文地址:https://www.cnblogs.com/liuzhaolong/p/12874546.html