Java 中 Controller 层增删改查代码

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";
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值