博客标签功能管理
博客标签的类型管理,实现了标签的增删改查
同样每张页面上最多显示五条类型,在编辑、新增、删除中实现功能。
新增test项
删除test
修改第一条信心为ajaxxx
##核心代码
TagController
@Controller
@RequestMapping("admin/tags")
public class TagController {
@Autowired
private ITagService tagService;
@GetMapping
public String list(@PageableDefault(size = 5,sort = {"id"},direction = Sort.Direction.DESC) Pageable pageable, Model model){
Page<Tag> page = tagService.listType(pageable);
model.addAttribute("page",page);
return "admin/tags";
}
@GetMapping("{id}/delete")
public String delete(@PathVariable Long id){
tagService.deleteType(id);
return "redirect:/admin/tags";
}
@GetMapping("input")
public String input(Model model){
model.addAttribute("tag",new Tag());
return "admin/tags-input";
}
@PostMapping("add")
public String add(Tag tag){
tagService.addTag(tag);
return "redirect:/admin/tags";
}
@GetMapping("{id}/toUpdate")
public String toUpdate(@PathVariable Long id, Model model, RedirectAttributes attributes){
Tag tag=tagService.getTag(id);
model.addAttribute("tag",tag);
return "admin/tags-input";
}
@PostMapping("update/{id}")
public String update(Tag type,@PathVariable Long id){
tagService.update(id,type);
return "redirect:/admin/tags";
}
}
ITagService接口
public interface ITagService {
Page<Tag> listType(Pageable pageable);
void deleteType(Long id);
void addTag(Tag tag);
Tag getTag(Long id);
void update(Long id, Tag type);
List<Tag> listType();
List<Tag> getTagByIds(String tagIds);
List<Tag> listTypeTop(int i);
}
TagServiceImpl
@Service
public class TagServiceImpl implements ITagService {
@Autowired
private TagDao tagDao;
@Override
public Page<Tag> listType(Pageable pageable) {
Page<Tag> page = tagDao.findAll(pageable);
return page;
}
@Override
public void deleteType(Long id) {
tagDao.deleteById(id);
}
@Override
public void addTag(Tag tag) {
tagDao.save(tag);
}
@Override
public Tag getTag(Long id) {
return tagDao.getOne(id);
}
@Override
public void update(Long id, Tag tag) {
Tag tag1 = tagDao.getOne(id);
BeanUtils.copyProperties(tag,tag1);
tagDao.save(tag1);
}
@Override
public List<Tag> listType() {
return tagDao.findAll();
}
@Override//用于调用发布博客里的标签
public List<Tag> getTagByIds(String tagIds) {
List<Long> ids=new ArrayList<>();
if(tagIds !=null && tagIds != ""){
String[] s = tagIds.split(",");
for(int i=0;i<s.length;i++){
if(!StringUtils.isEmpty(s[i])){
ids.add(new Long(s[i]));
}
}
}
List<Tag> tags = tagDao.findAllById(ids);
return tags;
}
@Override//用于首页查找博客中使用最多的标签tag
public List<Tag> listTypeTop(int i) {
Sort sort= Sort.by(Sort.Direction.DESC,"blogs.size");
Pageable pageable= PageRequest.of(0,i,sort);
List<Tag> tags=tagDao.findTop(pageable);
return tags;
}
}
TagDao
同样继承JpaRepository类,用于调用findAll等方法
public interface TagDao extends JpaRepository<Tag,Long> {
@Query("select t from Tag t")
List<Tag> findTop(Pageable pageable);//用于查找首页上使用最多的tag标签
}