【Spring Boot】初识Spring Boot之开发博客项目(三)

博客分类功能管理

博客文章的类型管理,实现了类型的增删改查,每张页面上最多显示五条类型,在编辑、新增、删除中实现功能。
在这里插入图片描述
删除第一条
在这里插入图片描述
第二页
在这里插入图片描述
修改第一条信心为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最多的类型

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值