发表于: 2020-01-04 20:36:43

1 1061


今天完成的事情:

使用ssm实现增删改查

如图:

由于之后还要完成分页操作就不粘全部代码,下面是controller层代码:

package com.wp.sm.controller;

import com.wp.sm.beans.User;
import com.wp.sm.service.UserService;
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.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
@Resource(name = "userService")
private UserService userService;

   /**
    * 根据id查询
    */
   @RequestMapping(value = "/selectUserById")
public ModelAndView selectUserById(HttpServletRequest request) {
ModelAndView mv = new ModelAndView();
       int id = Integer.parseInt(request.getParameter("id"));
       try {
User user = userService.selectUserById(id);
           mv.setViewName("selectbyid");
           mv.addObject("user", user);
       } catch (Exception e) {
e.printStackTrace();
       }
return mv;
   }

@RequestMapping(value = "/show")
public String selectByAll(HttpServletRequest req) throws Exception {
List<User> list = userService.selectAllUser();
       req.setAttribute("list", list);
       return "show";
   }

@RequestMapping("/delete")
public String deleteById(int id) throws Exception {
userService.deleteUser(id);
       return "redirect:show";
   }

@RequestMapping(value = "/insert",method = RequestMethod.POST)
public String insertUser(User user) throws Exception {
userService.insertUser(user);
       return "redirect:show";
   }
@RequestMapping("/update/{id}")
public String update(@PathVariable("id") int id, Model model) throws Exception {
model.addAttribute("user",userService.selectUserById(id));
       return "update";
   }

@RequestMapping(value = "/update",method = RequestMethod.POST)
public String update(User user) throws Exception {
userService.updateUser(user);
       return "redirect:show";
   }
}

明天计划的事情: 

尝试添加分页,熟悉controller层的逻辑


遇到的问题: 

昨天找的案例的操作逻辑不符合正常标准,今天又重新找了一个,重新改了一遍。


收获:

对jsp页面的简单实现有了一些了解,对controller层的业务逻辑也有了一些了解,但是有部分代码没遇到过,还需要进行深入理解。

暂时没什么其他的收获。


返回列表 返回列表
评论

    分享到