博客分类功能管理
博客文章的类型管理,实现了类型的增删改查,每张页面上最多显示五条类型,在编辑、新增、删除中实现功能。
删除第一条
第二页
修改第一条信心为javasee
##核心代码
TypeController
分页技术
@PageableDefault(size = 5,sort = {“id”},direction = Sort.Direction.DESC) Pageable pageable
每页size为5,通过id排序,用desc倒序方式进行排序
@Controller
@RequestMapping("/admin/types")
public class TypeController {
@Autowired
private ITypeService typeService;
@GetMapping
public String list(@PageableDefault(size = 5,sort = {"id"},direction = Sort.Direction.DESC) Pageable pageable, Model model){
Page<Type> page = typeService.listType(pageable);
model.addAttribute("page",page);
return "admin/types";
}
@GetMapping("{id}/delete")
public String delete(@PathVariable Long id){
typeService.deleteType(id);
return "redirect:/admin/types";
}
@GetMapping("input")
public String input(Model model){
model.addAttribute("type",new Type());
return "admin/types-input";
}
@PostMapping("add")
public String add(Type type){
typeService.addType(type);
return "redirect:/admin/types";
}
@GetMapping("{id}/toUpdate")//修改前查询出信息
public String toUpdate(@PathVariable Long id, Model model, RedirectAttributes attributes){
Type type=typeService.getType(id);
model.addAttribute("type",type);
return "admin/types-input";
}
@PostMapping("update/{id}")//修改
public String update(Type type,@PathVariable Long id){
typeService.update(id,type);
return "redirect:/admin/types";
}
}
ITypeService接口
public interface ITypeService {
Page<Type> listType(Pageable pageable);
void deleteType(Long id);
void addType(Type type);
Type getType(Long id);
void update(Long id, Type type);
List<Type> listType();
List<Type> listTypeTop(int i);
}
TypeServiceImpl
@Service
public class TypeServiceImpl implements ITypeService {
@Autowired
private TypeDao typeDao;
@Override//和下面的listtype进行方法的重载
public Page<Type> listType(Pageable pageable) {
Page<Type> page = typeDao.findAll(pageable);
return page;
}
@Override
public void deleteType(Long id) {
typeDao.deleteById(id);
}
@Override
public void addType(Type type) {
typeDao.save(type);
}
@Override
public Type getType(Long id) {
return typeDao.getOne(id);
}
@Override//如果有进行修改,将修改内容进行替换
public void update(Long id, Type type) {
Type type1 = typeDao.getOne(id);
BeanUtils.copyProperties(type,type1);
typeDao.save(type1);
}
@Override//查询所有type
public List<Type> listType() {
return typeDao.findAll();
}
@Override//用于首页查找博客中使用最多的分类type
public List<Type> listTypeTop(int i) {
Sort sort= Sort.by(Sort.Direction.DESC,"blogs.size");
Pageable pageable=PageRequest.of(0,i,sort);
List<Type> types=typeDao.findTop(pageable);
return types;
}
}
TypeDao
同样继承JpaRepository类
public interface TypeDao extends JpaRepository<Type,Long> {
@Query("select t from Type t")
List<Type> findTop(Pageable pageable);//用于首页查询使用type最多的类型
}