@Controller@RequestMapping("/admin/types")publicclassTypeController{@Autowiredprivate ITypeService iTypeService;@GetMappingpublic String list(@PageableDefault(size =5, sort ={"id"}, direction = Sort.Direction.DESC) Pageable pageable, Model model){
Page<Type> page = iTypeService.listType(pageable);
model.addAttribute("page", page);return"admin/types";}//@{/admin/type/{id}/delete(id=${types.id})}@GetMapping("{id}/delete")public String delete(@PathVariable Long id){
iTypeService.deleteType(id);return"redirect:/admin/types";}@GetMapping("input")public String input(){return"admin/types-input";}@PostMapping("add")public String add(Type type){
iTypeService.addType(type);return"redirect:/admin/types";}@GetMapping("{id}/toUpdate")public String toUpdate(@PathVariable Long id, Model model){
Type type = iTypeService.getTypeById(id);
model.addAttribute("type", type);return"admin/types-input";}@PostMapping("update/{id}")public String update(Type type,@PathVariable Long id){
iTypeService.update(id, type);return"redirect:/admin/types";}}
ITypeService
publicinterfaceITypeService{
Page<Type>listType(Pageable pageable);voiddeleteType(Long id);voidaddType(Type type);
Type getTypeById(Long id);voidupdate(Long id, Type type);}
TypeServiceImpl
@ServicepublicclassTypeServiceImplimplementsITypeService{@Autowiredprivate TypeDao typeDao;@Overridepublic Page<Type>listType(Pageable pageable){return typeDao.findAll(pageable);}@OverridepublicvoiddeleteType(Long id){
typeDao.deleteById(id);}@OverridepublicvoidaddType(Type type){
typeDao.save(type);}@Overridepublic Type getTypeById(Long id){return typeDao.getOne(id);}@Overridepublicvoidupdate(Long id, Type type){
Type type1 = typeDao.getOne(id);
BeanUtils.copyProperties(type, type1);
typeDao.save(type);}}
Type.html 部分代码
<trth:each="type,iterStat : ${page.content}"><tdth:text="${iterStat.count}">1</td><tdth:text="${type.name}">刻意练习清单</td><td><ahref="#"th:href="@{/admin/types/{id}/toUpdate(id=${type.id})}"class="ui mini teal basic button">编辑</a><ahref="#"th:href="@{/admin/types/{id}/delete(id=${type.id})}"class="ui mini red basic button">删除</a></td></tr>