spring boot项目标签管理+分类管理
程序员文章站
2024-03-04 11:34:23
...
1 分类管理
表现层设计如下
@Controller
@RequestMapping("/admin")
public class TypeController {
@Autowired
TypeService typeService;
@GetMapping("/types")
public String types(@PageableDefault(size = 5, sort = {"id"}, direction = Sort.Direction.DESC) Pageable pageable, Model model){
model.addAttribute("page",typeService.listType(pageable));
return "/admin/types";
}
}
业务层中listType()实现如下所示
public interface TypeService {
// 分页查询
List<Type> listType(Pageable pageable);
}
@Service
public class TypeServiceImpl implements TypeService {
@Autowired
TypeRepository typeRepository;
@Transactional
@Override
public Page<Type> listType(Pageable pageable) {
return typeRepository.findAll(pageable);
}
}
编辑操作对应的后端逻辑为
@PostMapping("/types/{id}")
public String editPost(@Valid Type type, BindingResult result,@PathVariable Long id, RedirectAttributes attributes) {
// 根据前端传来的Type对象的name字段来验证是否是当前已有的类别
Type type1 = typeService.getTypeByName(type.getName());
if (type1 != null) {
result.rejectValue("name","nameError","不能添加重复的分类");
}
// 如果输入不合法,重新跳转到输入页
if (result.hasErrors()) {
return "admin/types-input";
}
// 否则更新数据库中给定id的类别信息
Type t = typeService.updateType(id,type);
// 根据更新操作的结果给出提示信息
if (t == null ) {
attributes.addFlashAttribute("message", "更新失败");
} else {
attributes.addFlashAttribute("message", "更新成功");
}
// 最后重定向回类别管理页,显示最新的类别列表
return "redirect:/admin/types";
更新操作的业务层实现为
public interface TypeService {
// 更新类别
Type updateType(Long id, Type type);
}
@Transactional
@Override
public Type updateType(Long id, Type type) {
// 首先验证给定id对应的类别是否存在,如果不存在直接抛异常
Type type1 = typeRepository.getOne(id);
if (type1 == null){
throw new NotFoundException("不存在该类型");
}
// 否则执行更新操作
BeanUtils.copyProperties(type, type1);
return typeRepository.save(type1);
}
删除操作的实现
@GetMapping("/types/{id}/delete")
public String delete(@PathVariable Long id,RedirectAttributes attributes) {
typeService.deleteType(id);
attributes.addFlashAttribute("message", "删除成功");
return "redirect:/admin/types";
public interface TypeService {
// 删除类别
void deleteType(Long id);
}
@Service
public class TypeServiceImpl implements TypeService {
@Autowired
TypeRepository typeRepository;
@Transactional
@Override
public void deleteType(Long id) {
typeRepository.deleteById(id);
}
}
2 标签管理
表现层:
@Controller
@RequestMapping("/admin")
public class TagController {
@Autowired
private TagService tagService;
@GetMapping("/tags")
public String tags(@PageableDefault(size = 3,sort = {"id"},direction = Sort.Direction.DESC)
Pageable pageable, Model model) {
model.addAttribute("page",tagService.listTag(pageable));
return "admin/tags";
}
@GetMapping("/tags/input")
public String input(Model model) {
model.addAttribute("tag", new Tag());
return "admin/tags-input";
}
@GetMapping("/tags/{id}/input")
public String editInput(@PathVariable Long id, Model model) {
model.addAttribute("tag", tagService.getTag(id));
return "admin/tags-input";
}
@PostMapping("/tags")
public String post(@Valid Tag tag,BindingResult result, RedirectAttributes attributes) {
Tag tag1 = tagService.getTagByName(tag.getName());
if (tag1 != null) {
result.rejectValue("name","nameError","不能添加重复的标签");
}
if (result.hasErrors()) {
return "admin/tags-input";
}
Tag t = tagService.saveTag(tag);
if (t == null ) {
attributes.addFlashAttribute("message", "新增失败");
} else {
attributes.addFlashAttribute("message", "新增成功");
}
return "redirect:/admin/tags";
}
@PostMapping("/tags/{id}")
public String editPost(@Valid Tag tag, BindingResult result,@PathVariable Long id, RedirectAttributes attributes) {
Tag tag1 = tagService.getTagByName(tag.getName());
if (tag1 != null) {
result.rejectValue("name","nameError","不能添加重复标签");
}
if (result.hasErrors()) {
return "admin/tags-input";
}
Tag t = tagService.updateTag(id,tag);
if (t == null ) {
attributes.addFlashAttribute("message", "更新失败");
} else {
attributes.addFlashAttribute("message", "更新成功");
}
return "redirect:/admin/tags";
}
@GetMapping("/tags/{id}/delete")
public String delete(@PathVariable Long id,RedirectAttributes attributes) {
tagService.deleteTag(id);
attributes.addFlashAttribute("message", "删除成功");
return "redirect:/admin/tags";
}
}
业务层:
@Service
public class TagServiceImpl implements TagService {
@Autowired
private TagRepository tagRepository;
@Transactional
@Override
public Tag saveTag(Tag tag) {
return tagRepository.save(tag);
}
@Transactional
@Override
public Tag getTag(Long id) {
return tagRepository.getOne(id);
}
@Transactional
@Override
public Page<Tag> listTag(Pageable pageable) {
return tagRepository.findAll(pageable);
}
@Transactional
@Override
public Tag updateTag(Long id, Tag tag) {
Tag t = tagRepository.getOne(id);
if (t == null) {
throw new NotFoundException("不存在该标签");
}
BeanUtils.copyProperties(tag,t);
return tagRepository.save(t);
}
@Transactional
@Override
public void deleteTag(Long id) {
tagRepository.deleteById(id);
}
}
效果展示
上一篇: TeX使用范本
推荐阅读