增部分
<a href="#" th:href="@{/admin/types/input}" class="ui mini right floated teal basic button">新增</a>
<form action="#" method="post" th:action="*{id}==null ? @{/admin/types/add} : @{/admin/types/update/{id}({id=*id})}" class="ui form">
<input type="hidden" name="id" th:value="*{id}">
@PostMapping("add")
public String add(Type type){
typeService.addType(type);
return "redirect:/admin/types";
}
@Override
public void addType(Type type) {
typeDao.save(type);
}
删部分
<a href="#" th:href="@{/admin/types/{id}/delete(id=${type.id})}" class="ui mini red basic button">删除</a>
@GetMapping("{id}/delete")
public String delete(@PathVariable Long id){
typeService.deleteType(id);
return "redirect:/admin/types";
}
@Override
public void deleteType(Long id) {
typeDao.deleteById(id);
}
改
<a href="#" th:href="@{/admin/types/{id}/toUpdate(id=${type.id})}" class="ui mini teal basic button">编辑</a>
@PostMapping("{id}/toUpdate")
public String toUpdate(@PathVariable Long id,Model model){
Type type = typeService.getType(id);
model.addAttribute("type",type);
return "admin/types-input";
}
@Override
public Type getType(Long id) {
return typeDao.getOne(id);
}
<form action="#" method="post" th:action="*{id}==null ? @{/admin/types/add} : @{/admin/types/update/{id}({id=*id})}" class="ui form">
<input type="hidden" name="id" th:value="*{id}">
@PostMapping("update/{id}")
public String update(Type type,@PathVariable Long id){
typeService.update(id,type);
return "redirect:/admin/types";
}
@Override
public void update(Long id, Type type) {
Type type1 = typeDao.getOne(id);
BeanUtils.copyProperties(type,type1);
typeDao.save(type1);
}
查
<tr th:each="type,iterStat : ${page.content}">
<td th:text="${iterStat.count}">1</td>
<td th:text="${type.name}">刻意练习清单</td>
@GetMapping("")
public String list(@PageableDefault(size=5,sort = {"id"},direction = Sort.Direction.DESC)Pageable pageable, Model model){
Page<Type> page = typeService.ListType(pageable);
model.addAttribute("page",page);
return "admin/types";
}
@Override
public Page<Type> ListType(Pageable pageable) {
Page<Type> page = typeDao.findAll(pageable);
return page;
}