分类管理与标签管理
程序员文章站
2024-03-22 17:51:16
...
分类与标签
分类和标签是博客系统的两项功能。分类指用户定义类别并给博文归类。这是很自然地整理文章的方法,所以也成为博客系统的基本功能。后来,互联网上的文章又发展出一种方便友好的特色——标签(Label或Tag)。标签相当于传统文章的关键字和书本的索引,指明的是文章包含的某一点内容或者特色。分类往往是从某一个角度将所有文章讨论的范围分成几个子集。标签则只是反映文章的侧面和特色。两者相互补充,就像一本书既有目录也有索引。比如,我们可以把车辆分类为自行车、摩托车和汽车,同时又可以给不同的车贴上进口、永久牌、女式、我骑过的这些五花八门的标签。在一个网站上,如果标签设置得规整,保持一致的角度,也可以替代分类。用户操作上,类别需要单独先添加再选择,标签则可以即时输入或选择。互联网上灵活*的风格,两者若只选其一,标签比分类更适合。
Type
@OneToMany意即关联News表,返回某Type时也返回所有对应的News。
@Entity
@Table(name = "t_type")
public class Type {
@Id //主键标识
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank(message = "分类名称不能为空")
private String name;
@OneToMany(mappedBy = "type")
private List<News> newsList = new ArrayList<>();
public Type(){
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<News> getNewsList() {
return newsList;
}
public void setNewsList(List<News> newsList) {
this.newsList = newsList;
}
@Override
public String toString() {
return "Type{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
@Service
public class TypeServiceImpl implements TypeService {
@Autowired
private TypeRepository typeRepository;
@Override
public Page<Type> listType(Pageable pageable) {
return typeRepository.findAll(pageable);
}
@Override
public Type saveType(Type type) {
return typeRepository.save(type);
}
@Override
public Type getTypeByName(String name) {
return typeRepository.findByName(name);
}
@Override
public void delete(Long id) {
typeRepository.deleteById(id);
}
@Override
public Type getType(Long id) {
return typeRepository.findById(id).orElse(null);
}
@Override
public Type updateType(Long id, Type type) {
Type type1=typeRepository.findById(id).orElse(null);
if(type1==null){
return null;
}
BeanUtils.copyProperties(type,type1);
return typeRepository.save(type1);
}
@Override
public List<Type> listType() {
return typeRepository.findAll();
}
}
Spring Data JPA内置分页,用法是在参数中加入Pageable pageable,和参数注解@PageableDefault()
@Controller
@RequestMapping("/admin")
public class TypeController {
@Autowired
private TypeService typeService;
@RequestMapping("/types")
public String type(@PageableDefault(size = 3,sort = {"id"},direction = Sort.Direction.DESC)
Pageable pageable, Model model){
model.addAttribute("page",typeService.listType(pageable));
return "admin/types";
}
@GetMapping("/types/input")
public String input(Model model){
model.addAttribute("type",new Type());
return "admin/types-input";
}
@PostMapping("/types/add")
public String add(@Valid Type type, BindingResult result, RedirectAttributes attributes){
Type type1=typeService.getTypeByName(type.getName());
if(type1!=null){
result.rejectValue("name","nameError","不能添加重复分类");
}
if(result.hasErrors()){
return "admin/types-input";
}
Type type2=typeService.saveType(type);
if(type2==null){
attributes.addFlashAttribute("message","新增失败");
}else{
attributes.addFlashAttribute("message","新增成功");
}
return "redirect:/admin/types";
}
@RequestMapping("/types/{id}/delete")
public String delete(@PathVariable long id,RedirectAttributes attributes){
typeService.delete(id);
attributes.addFlashAttribute("message","删除成功");
return "redirect:/admin/types";
}
@RequestMapping("/types/{id}/toUpdate")
public String toUpdate(@PathVariable Long id,Model model){
model.addAttribute("type",typeService.getType(id));
return "admin/types-input";
}
@RequestMapping("/types/update/{id}")
public String update(@Valid Type type,BindingResult result,@PathVariable Long id,RedirectAttributes attributes) {
Type type1 = typeService.getTypeByName(type.getName());
if (type1 != null) {
result.rejectValue("name", "nameError", "不能添加重复分类");
}
if (result.hasErrors()) {
return "admin/types-input";
}
Type type2 = typeService.updateType(id, type);
if (type2 != null) {
attributes.addFlashAttribute("message", "更新成功");
} else {
attributes.addFlashAttribute("message", "更新失败");
}
return "redirect:/admin/types";
}
}
Tag
@Entity
@Table(name="t_tag")
public class Tag {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank(message = "标求名不能为空")
private String name;
@ManyToMany(mappedBy = "tags")
private List<News> newsList=new ArrayList<>();
public Tag(){
}
public List<News> getNewsList() {
return newsList;
}
public void setNewsList(List<News> newsList) {
this.newsList = newsList;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Tag{" +
"id=" + id +
", name='" + 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);
}
@Override
public List<Tag> listTag() {
return tagRepository.findAll();
}
@Override
public List<Tag> listTag(String ids) {
return tagRepository.findAllById(convertToList(ids));
}
private List<Long> convertToList(String ids){
System.out.println("service接收TagId为"+ids);
List<Long> list=new ArrayList<>();
if(!"".equals(ids) && ids!=null){
String[] idArray=ids.split(",");
for(int i=0;i<idArray.length;i++){
list.add(new Long(idArray[i]));
}
}
System.out.println("service处理完后TagId为"+list);
return list;
}
}
@Controller
@RequestMapping("/admin")
public class TagController {
@Autowired
private TagService tagService;
@RequestMapping("/tags")
private 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","不能添加重复tag");
}
if(result.hasErrors()){
return "admin/tags-input";
}
Tag type2=tagService.saveTag(tag);
if(type2==null){
attributes.addFlashAttribute("message","新增失败");
}else{
attributes.addFlashAttribute("message","新增成功");
}
return "redirect:/admin/tags";
}
@RequestMapping("/tags/{id}/toUpdate")
public String toUpdate(@PathVariable Long id, Model model){
model.addAttribute("tags",tagService.getTag(id));
return "admin/tags-input";
}
@RequestMapping("/tags/update/{id}")
public String update(@Valid Tag tag,BindingResult result,@PathVariable Long id,RedirectAttributes attributes) {
Tag tag1 = tagService.getTagByName(tag.getName());
if (tag1 != null) {
result.rejectValue("name", "nameError", "不能添加重复tag");
}
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";
}
}