1.分类管理
1.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";
}
}
1.2 业务层
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);
}
2.标签
2.1 实体类tag
@Entity
@Table(name = "t_tag")
public class Tag {
@Id
@GeneratedValue
private Long id;
@NotBlank(message = "标签名称不能为空")
private String name;
}
JPA接口创建
public interface TagRepository extends JpaRepository<Tag,Long> {
Tag findByName(String name);
}
业务逻辑
@Service
public class TagServiceImpl implements TagService {
@Autowired
private TagRepository tagRepository;
@Override
public Page<Tag> listTag(Pageable pageable) {
return tagRepository.findAll(pageable);
}
@Override
public Tag saveTag(Tag tag) {
return tagRepository.save(tag);
}
@Override
public void deleteTag(Long id) {
tagRepository.deleteById(id);
}
@Override
public Tag getTagByName(String name) {
return tagRepository.findByName(name);
}
@Override
public Tag getTag(Long id) {
return tagRepository.findById(id).orElse(null);
}
@Override
public Tag updateTag(Long id, Tag tag) {
Tag tag1 = tagRepository.findById(id).orElse(null);
if(tag1==null){
System.out.println("未获得更新对象");
return null;
}
BeanUtils.copyProperties(tag,tag1);
return tagRepository.save(tag1);
}
}
增删改查业务逻辑
@RequestMapping("/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";
}
@PostMapping("/tags/add")
public String add(@Valid Tag tag, BindingResult result, RedirectAttributes attributes){
Tag tag1 = tagService.getTagByName(tag.getName());
if(tag1!=null){
result.rejectValue("name","nameError","不能添加重复的标签");
System.out.println(result);
}
if(result.hasErrors()){
return "admin/tags-input";
}
Tag tag2 = tagService.saveTag(tag);
if(tag2==null)
{
attributes.addFlashAttribute("message","新增失败");
}else{
attributes.addFlashAttribute("message","新增成功");
}
return "redirect:/admin/tags";
}
@RequestMapping("tags/{id}/delete")
public String delete(@PathVariable Long id, RedirectAttributes attributes){
tagService.deleteTag(id);
attributes.addFlashAttribute("message","删除成功");
return "redirect:/admin/tags";
}
@RequestMapping("/tags/{id}/toUpdate")
public String toUpdate(@PathVariable Long id,Model model){
System.out.println("id"+id);
model.addAttribute("tag",tagService.getTag(id));
System.out.println("根据id查得数据为"+tagService.getTag(id));
return "admin/tags-input";
}
@RequestMapping("/tags/update/{id}")
public String update(@Valid Tag tag, BindingResult result, @PathVariable Long id, RedirectAttributes attributes){
System.out.println("传入tag"+tag);
Tag tag1 = tagService.getTagByName(tag.getName());
if(tag1!=null) {
result.rejectValue("name","nameError","不能添加重复的标签");
}
if(result.hasErrors()){
return "admin/tags-input";
}
Tag tag2 = tagService.updateTag(id,tag);
if(tag2!=null){
attributes.addFlashAttribute("message","更新成功");
}
else{
attributes.addFlashAttribute("message","更新失败");
}
return "redirect:/admin/tags";
}
}
3.结果展示