Spring Boot(十四):Spring Boot + mybatis + Thymeleaf 分页示例(纯底层代码,不是pagehelper)

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

一、项目架构

二、实现功能

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

三、代码实例

我一般是从controller层开始写

indexController

package life.majiang.community.controller;

...

@Controller public class IndexController {

@Autowired

QuestionService questionService;

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

}

QuestionService

1package life.majiang.community.service; 2 3... 4 5@Service 6public class QuestionService { 7 8 @Autowired 9 private QuestionMapper questionMapper; 10 11 @Autowired 12 private UserMapper userMapper; 13 14 public PaginationDTO list(Integer page, Integer size) { 15 PaginationDTO paginationDTO = new PaginationDTO(); 16 Integer totalCount = questionMapper.count(); 17 paginationDTO.setPagination(totalCount,page,size); 18 if(page<1){ 19 page = 1; 20 } 21 if(page>paginationDTO.getTotalPage()){ 22 page = paginationDTO.getTotalPage(); 23 } 24 25 Integer offset = size * (page-1); 26 List<Question> questions = questionMapper.list(offset,size); 27 List<QuestionDTO> questionDTOList = new ArrayList<>(); 28 29 for (Question question:questions){ 30 User user = userMapper.getUserBySingleName(question.getCreator()); 31 int comments = questionMapper.getComment_count(question); 32 int views = questionMapper.getView_count(question); 33 question.setComment_count(comments); 34 question.setView_count(views); 35 QuestionDTO questionDTO = new QuestionDTO(); 36 BeanUtils.copyProperties(question,questionDTO); 37 questionDTO.setUser(user); 38 questionDTOList.add(questionDTO); 39 } 40 paginationDTO.setQuestions(questionDTOList); 41 return paginationDTO; 42 } 43}

QuestionMapper

1package life.majiang.community.mapper; 2 3... 4 5@Mapper 6public interface QuestionMapper { 7 public void insertQuestion(Question question); 8 public String selectQuestionTitle(Question question); 9 public void updateQuestion(Question question); 10 @Select("select * from question limit #{offset},#{size}") 11 public List<Question> list(@Param(value = "offset") Integer offset, 12 @Param(value = "size") Integer size); 13 14 public Integer count(); 15 public int getComment_count(Question question); 16 public int getView_count(Question question); 17 public int getCreatorTitleIsExist(Question question); 18 19 @Select("select * from question where creator=#{creator} limit #{offset},#{size}") 20 List<Question> listByCreator(@Param(value = "creator") String creator, @Param(value = "offset") Integer offset,@Param(value = "size") Integer size); 21}

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>

四、实现效果

五、总结

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

每一篇博客都是一种经历,程序猿生涯的痕迹,知识改变命运,命运要由自己掌控,愿你游历半生,归来仍是少年。

欲速则不达,欲达则欲速!

更多精彩内容,首发公众号【素小暖】,欢迎关注。

点赞
收藏

评论区

加载中...

相关推荐

ssm+PageHelper实现分页查询

通过搭建ssm框架,然后通过mybatis的分页插件pagehelp进行分页查询。源码:https://gitee.com/smfx1314/pagehelper看一下项目结构:!(https://oscimg.oschina.net/oscnet/62738252a25e8c7d8d60f9f9bcf7cb51695.png)首先创建

Spring Boot(八):Okhttp实现GitHub第三方登录

最近一直在学习springboot,刚刚看完雷丰阳老师讲的springboot基础、整合视频,相见恨晚的赶脚,顺理成章的看了尚硅谷的springboot谷粒商城项目视频,但是,有种说不出的感觉,看了16节,还是放弃了,在bilibili上又搜索了一番,找到了现在学习的这个视频,码匠社区项目,看了几节,感觉还可以,项目中用到了GitHub第三方登录,第一次接触

SpringBoot 整合Filter

SpringBoot整合Filter一、创建基础SpringBoot项目,集成Web即可<dependency\<groupId\org.springframework.boot</groupId\<a

SpringBoot2.0应用(五):SpringBoot2.0整合MyBatis

如何整合MyBatis1、pom依赖<dependency<groupIdorg.mybatis.spring.boot</groupId<artifactIdmybatisspringbootstarte

MyBatis学习总结(17)——Mybatis分页插件PageHelper

如果你也在用Mybatis,建议尝试该分页插件,这一定是最方便使用的分页插件。分页插件支持任何复杂的单表、多表分页,部分特殊情况请看重要提示(http://git.oschina.net/free/Mybatis_PageHelper/blob/master/wikis/Important.markdown)。想要使用分页插件?请看如何使用分页

SpringBoot使用pagehelper注意事项

Spring和SpringBoot使用pagehelper的方式不太一样,从Spring到SpringBoot的朋友注意了!如果在SpringBoot中使用Spring加载pagehelper的方式,能获取数据,但是分页不准确。SpringBoot加载pagehelper步骤如下:maven搜索springbootpagehelper!