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

}

  • 11
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在SSM框架Controller负责处理前端请求,并将处理结果返回给前端。下面是一个简单的Controller处理请求的示例: ```java @Controller @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @RequestMapping(value = "/get/{id}", method = RequestMethod.GET) @ResponseBody public User getUserById(@PathVariable("id") int id) { User user = userService.getUserById(id); return user; } @RequestMapping(value = "/add", method = RequestMethod.POST) @ResponseBody public String addUser(@RequestBody User user) { userService.addUser(user); return "success"; } @RequestMapping(value = "/update", method = RequestMethod.PUT) @ResponseBody public String updateUser(@RequestBody User user) { userService.updateUser(user); return "success"; } @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE) @ResponseBody public String deleteUser(@PathVariable("id") int id) { userService.deleteUser(id); return "success"; } } ``` 在上面的代码,UserController类使用了@Controller注解来声明一个控制器,使用@RequestMapping注解来映射请求路径。在请求处理方法上,使用@RequestMapping注解来声明请求的HTTP方法和路径,使用@PathVariable注解来获取请求路径的参数,使用@ResponseBody注解来将处理结果转换为JSON格式的数据返回给前端。 其,getUserById方法处理GET请求,从数据库获取指定id的用户信息并返回给前端;addUser方法处理POST请求,将前端提交的用户信息添加到数据库;updateUser方法处理PUT请求,更新数据库指定id的用户信息;deleteUser方法处理DELETE请求,删除数据库指定id的用户信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值