Java 中 Controller 层增删改查代码
此文件会随着时间不断更新
package com.hyd.YKenan.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.YKenan.YKenan.pojo.YHide;
import com.YKenan.YKenan.service.YHideService;
@Controller
public class YHideController {
@Autowired
private YHideService yHideService;
@RequestMapping("/list")
public String list(Model model) {
try {
List<YHide> all = yHideService.getAll();
model.addAttribute("yHideList", all);
} catch (Exception e) {
e.printStackTrace();
}
return "list";
}
@RequestMapping(value = "/list/{yId}")
public String delect(@PathVariable(value = "yId") String yId) {
try {
yHideService.deleteByPrimaryKey(yId);
} catch (Exception e) {
e.printStackTrace();
}
return "redirect:/list";
}
@RequestMapping(value = "/update/{yId}", method = RequestMethod.GET)
public String update(@PathVariable(value = "yId") String yId, HttpServletRequest request, Model model) {
try {
YHide selectByPrimaryKey = yHideService.selectByPrimaryKey(yId);
String getyAge = selectByPrimaryKey.getyAge();
String getyHoppy = selectByPrimaryKey.getyHoppy();
request.getSession().setAttribute("update_selectByPrimaryKey", selectByPrimaryKey);
model.addAttribute("getyAge", getyAge);
model.addAttribute("getyHoppy", getyHoppy);
} catch (Exception e) {
e.printStackTrace();
}
return "update";
}
@RequestMapping(value = "update/add/", method = RequestMethod.POST)
public String add(@RequestParam String age, @RequestParam String hoppy, HttpServletRequest request, Model model) {
try {
if ("".equals(age) || age.isEmpty() || "".equals(hoppy) || hoppy.isEmpty()) {
} else {
YHide attribute = (YHide) request.getSession().getAttribute("update_selectByPrimaryKey");
attribute.setyAge(age);
attribute.setyHoppy(hoppy);
yHideService.update(attribute);
model.addAttribute("attribute" + "用户修改成功", attribute.getyId());
}
} catch (Exception e) {
e.printStackTrace();
}
return "redirect:/list";
}
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String yHideAdd(@RequestParam String age, @RequestParam String hoppy, Model model) {
try {
if ("".equals(age) || age.isEmpty() || "".equals(hoppy) || hoppy.isEmpty()) {
} else {
String insertAndReturnID = yHideService.insertAndReturnID(age, hoppy);
if (insertAndReturnID.equals("") || insertAndReturnID.isEmpty()) {
model.addAttribute("用户添加失败", insertAndReturnID);
}
model.addAttribute("insertAndReturnID" + "用户添加成功", insertAndReturnID);
}
} catch (Exception e) {
e.printStackTrace();
}
return "redirect:/list";
}
@RequestMapping("/delect")
public String yHideDelect(@RequestParam String id, Model model) {
try {
if ("".equals(id) || id.isEmpty()) {
} else {
yHideService.deleteByPrimaryKey(id);
}
} catch (Exception e) {
e.printStackTrace();
}
return "redirect:/list";
}
}