【Spring Boot 10】Spring Boot + mybatis + Thymeleaf 分页示例(纯底层代码,不是pagehelper)

最近放假在家,终于有时间学习springboot了,当下最流行的java框架,我还没有接触过,有点遗憾,看过尚硅谷雷丰阳老师的springboot基础整合篇,现在轮到项目整合了,在B站发现一个码匠社区项目挺好的,最近刚完成了分页部分的练习,居然没用插件,直接原生代码,有点牛笔。

一、项目架构

【Spring Boot 10】Spring Boot + mybatis + Thymeleaf 分页示例(纯底层代码,不是pagehelper)_第1张图片

二、实现功能

目前我实现了登录,简单的springboot+mybatis的增删改查,使用bootstrap的前台布局,问题列表展示,分页展示等功能。

三、代码实例

我一般是从controller层开始写

1、indexController

package life.majiang.community.controller;

...

@Controller
public class IndexController {

    @Autowired
    QuestionService questionService;

    @GetMapping("/")
    public String index(@RequestParam(name="page",defaultValue = "1") Integer page,
                        @RequestParam(name="size",defaultValue = "5") Integer size,
                        Model model) {
        PaginationDTO pagination = questionService.list(page,size);
        model.addAttribute("pagination",pagination);
        return "index";
    }
}

2、QuestionService

package life.majiang.community.service;

...

@Service
public class QuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private UserMapper userMapper;

    public PaginationDTO list(Integer page, Integer size) {
        PaginationDTO paginationDTO = new PaginationDTO();
        Integer totalCount = questionMapper.count();
        paginationDTO.setPagination(totalCount,page,size);
        if(page<1){
            page = 1;
        }
        if(page>paginationDTO.getTotalPage()){
            page = paginationDTO.getTotalPage();
        }

        Integer offset = size * (page-1);
        List questions = questionMapper.list(offset,size);
        List questionDTOList = new ArrayList<>();

        for (Question question:questions){
            User user = userMapper.getUserBySingleName(question.getCreator());
            int comments = questionMapper.getComment_count(question);
            int views = questionMapper.getView_count(question);
            question.setComment_count(comments);
            question.setView_count(views);
            QuestionDTO questionDTO = new QuestionDTO();
            BeanUtils.copyProperties(question,questionDTO);
            questionDTO.setUser(user);
            questionDTOList.add(questionDTO);
        }
        paginationDTO.setQuestions(questionDTOList);
        return paginationDTO;
    }
}

3、QuestionMapper

package life.majiang.community.mapper;

...

@Mapper
public interface QuestionMapper {
    public void insertQuestion(Question question);
    public String selectQuestionTitle(Question question);
    public void updateQuestion(Question question);
    @Select("select * from question limit #{offset},#{size}")
    public List list(@Param(value = "offset") Integer offset,
                               @Param(value = "size") Integer size);

    public Integer count();
    public int getComment_count(Question question);
    public int getView_count(Question question);
    public int getCreatorTitleIsExist(Question question);

    @Select("select * from question where creator=#{creator} limit #{offset},#{size}")
    List listByCreator(@Param(value = "creator") String creator, @Param(value = "offset") Integer offset,@Param(value = "size") Integer size);
}

4、QuestionMapper.xml




    
        insert into question(creator,title,description,tag,updateTime,comment_count,view_count,like_count)
        values (#{creator},#{title},#{description},#{tag},now(),1,1,1);
    
    
    
        
    
    
    
    
    

四、实现效果

【Spring Boot 10】Spring Boot + mybatis + Thymeleaf 分页示例(纯底层代码,不是pagehelper)_第2张图片

 

【Spring Boot 10】Spring Boot + mybatis + Thymeleaf 分页示例(纯底层代码,不是pagehelper)_第3张图片

五、总结

样式有点low,知识点也有点陈旧,但也算是一种学习吧,小白就应该更努力一些,2月15日开始看的,今天19号,看到了P28,又是一个74分钟的视频,真的是有点长,有点烦躁,希望自己能坚持下去。

你可能感兴趣的:(Spring,Boot)