Spring JPA CURD 增删改查

控制器类

/**
 * 这是一个控制器类,相当于Servlet
 * @author wsh
 *
 */
@Controller //表示这是控制器类
public class PhoneController {
	
	@Autowired
	private PhoneRepository phoneService;	
	
	/**
	 * GET请求
	 * @return
	 */
	@RequestMapping("/") //请求路径是根路径
	public String root() {
		///return "redirect:/index"; //redirect表示重定向到/index
		return "admin/index";
	}

	@RequestMapping("/mb") //请求路径是根路径
	public String mb() {
		///return "redirect:/index"; //redirect表示重定向到/index
		return "admin/mb";
	}

	@RequestMapping("/add") //请求路径是根路径
	public String add() {
		///return "redirect:/index"; //redirect表示重定向到/index
		return "admin/add";
	}
	
	@RequestMapping("/list") //请求路径是根路径
	public String list(Model model) {
		model.addAttribute("phones", phoneService.findAll());
		return "admin/list";
	}
	
	@PostMapping("/phones/save")
	public String save(@Valid Phone phone,BindingResult result, Model model,RedirectAttributes redirectAttributes) {
		
		if(result.hasErrors()) {
			return "admin/add";
		}
		
		phoneService.save(phone);
		redirectAttributes.addFlashAttribute("msg",ConstantUtil.MSG_SAVED);
		return "redirect:/list";
	}
	
	@GetMapping("/edit/{id}") // {id}是占位符
	public String showUpdateForm(@PathVariable("id") long id, Model model) { // @PathVariable 路径变量
		Optional<Phone> phone = phoneService.findById((int) id);
		model.addAttribute("phone",phone.get());
		return "admin/edit";
	}
	
	@PostMapping("/update/{id}")
	public String updateUser(@PathVariable("id") long id, @Valid Phone phone, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return "update-user";
		}
		phoneService.save(phone);
		return "redirect:/list";
	}
	
	@GetMapping("/delete/{id}")
	public String deleteUser(@PathVariable("id") long id, Model model) {
		phoneService.deleteById((int) id);
		return "redirect:/list";
	}
	
}

新增页面表单

<form class="form-horizontal" th:action="@{/phones/save}" method="post" >
                <div class="card-body">
                  <div class="form-group row">
                    <label for="inputEmail3" class="col-sm-2 col-form-label">品牌</label>
                    <div class="col-sm-10">
                      <input type="text" class="form-control" name="brand" placeholder="品牌">
                    </div>
                  </div>
                  <div class="form-group row">
                    <label for="inputPassword3" class="col-sm-2 col-form-label">型号</label>
                    <div class="col-sm-10">
                      <input type="text" class="form-control" name="model" placeholder="型号">
                    </div>
                  </div>
                 <div class="form-group row">
                    <label for="inputPassword3" class="col-sm-2 col-form-label">颜色</label>
                    <div class="col-sm-10">
                      <input type="text" class="form-control" name="color" placeholder="颜色">
                    </div>
                  </div>
                </div>
                <!-- /.card-body -->
                <div class="card-footer">
                  <div style="250px;margin: 0 auto">
					<button type="submit" class="btn btn-info" style=" 250px">提交</button>         
                  </div>
                </div>
                <!-- /.card-footer -->
              </form>

查询所有 列表页面

<table class="table table-hover">
                  <thead>
                    <tr>
                      <th>ID</th>
                      <th>品牌</th>
                      <th>型号</th>
                      <th>颜色</th>
                      <th>操作</th>
                      <th>操作</th>
                    </tr>
                  </thead>
                  <tbody>
                    <tr th:each="phone : ${phones}">
                      <td th:text="${phone.id}"></td>
                      <td th:text="${phone.brand}"></td>
                      <td th:text="${phone.model}"></td>
                      <td th:text="${phone.color}"></td>
                      <td><a th:href="@{/edit/{id}(id=${phone.id})}">修改</a></td>
                      <td><a th:href="@{/delete/{id}(id=${phone.id})}">删除</a></td>
                    </tr>
                  </tbody>
                </table>

修改单个页面

              <form class="form-horizontal" th:action="@{'/update/'+${phone.id}}" th:object="${phone}" method="post" >
                <div class="card-body">
                  <div class="form-group row">
                    <label for="inputEmail3" class="col-sm-2 col-form-label">品牌</label>
                    <div class="col-sm-10">
                      <input type="text" class="form-control" name="brand" placeholder="品牌"  th:value="*{brand}"> 
                    </div>
                  </div>
                  <div class="form-group row">
                    <label for="inputPassword3" class="col-sm-2 col-form-label">型号</label>
                    <div class="col-sm-10">
                      <input type="text" class="form-control" name="model" placeholder="型号" th:value="*{model}">
                    </div>
                  </div>
                 <div class="form-group row">
                    <label for="inputPassword3" class="col-sm-2 col-form-label">颜色</label>
                    <div class="col-sm-10">
                      <input type="text" class="form-control" name="color" placeholder="颜色" th:value="*{color}">
                    </div>
                  </div>
                </div>
                <!-- /.card-body -->
                <div class="card-footer">
                  <div style="250px;margin: 0 auto">
					<button type="submit" class="btn btn-info" style=" 250px">提交</button>         
                  </div>
                </div>
                <!-- /.card-footer -->
              </form>
原文地址:https://www.cnblogs.com/max-hou/p/13496844.html