Spring Boot(十四):Spring Boot + mybatis + Thymeleaf 分頁示例(純底層代碼,不是pagehelper)

最近放假在家,終於有時間學習springboot了,當下最流行的java框架,我尚未接觸過,有點遺憾,看過尚硅谷雷豐陽老師的springboot基礎整合篇,如今輪到項目整合了,在B站發現一個碼匠社區項目挺好的,最近剛完成了分頁部分的練習,竟然沒用插件,直接原生代碼,有點牛筆。java

1、項目架構spring

2、實現功能bootstrap

目前我實現了登陸,簡單的springboot+mybatis的增刪改查,使用bootstrap的前臺佈局,問題列表展現,分頁展現等功能。springboot

3、代碼實例mybatis

我通常是從controller層開始寫架構

indexControllerapp

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";
    }
}

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<Question> questions = questionMapper.list(offset,size);
        List<QuestionDTO> 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;
    }
}

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<Question> 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<Question> listByCreator(@Param(value = "creator") String creator, @Param(value = "offset") Integer offset,@Param(value = "size") Integer size);
}

QuestionMapper,xml學習

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="life.majiang.community.mapper.QuestionMapper">
    <insert id="insertQuestion">
        insert into question(creator,title,description,tag,updateTime,comment_count,view_count,like_count)
        values (#{creator},#{title},#{description},#{tag},now(),1,1,1);
    </insert>
    <select id="selectQuestionTitle" resultType="String">
        select title from question where creator = #{creator};
    </select>
    <update id="updateQuestion">
        <!--UPDATE question SET title=#{title} and description=#{description} and tag=#{tag} where creator=#{creator}-->
    </update>
    <select id="count" resultType="Integer">
        select count(*) from question;
    </select>
    <select id="getComment_count" resultType="Integer">
        select comment_count from question WHERE creator = #{creator} and title=#{title};
    </select>
    <select id="getView_count" resultType="Integer">
        select view_count from question WHERE creator = #{creator} and title=#{title};
    </select>
    <select id="getCreatorTitleIsExist" resultType="Integer">
        select count(*) from question WHERE creator = #{creator} and title=#{title};
    </select>
</mapper>

4、實現效果

5、總結

樣式有點low,知識點也有點陳舊,但也算是一種學習吧,小白就應該更努力一些,2月15日開始看的,今天19號,看到了P28,又是一個74分鐘的視頻,真的是有點長,有點煩躁,但願本身能堅持下去。

 

每一篇博客都是一種經歷,程序猿生涯的痕跡,知識改變命運,命運要由本身掌控,願你遊歷半生,歸來還是少年。

欲速則不達,欲達則欲速!

更多精彩內容,首發公衆號【素小暖】,歡迎關注。

相關文章
相關標籤/搜索