SpringBoot自我整理学习3(springboot集成Mybatis+PageHelp分页插件)

来源:互联网 发布:淘宝网上怎么买药 编辑:程序博客网 时间:2024/05/18 14:28

1)首先需要引入依赖包

<dependency>        <groupId>org.mybatis.spring.boot</groupId>        <artifactId>mybatis-spring-boot-starter</artifactId>        <version>1.3.1</version></dependency> <!-- mybatis 分页插件 --> <dependency>         <groupId>com.github.pagehelper</groupId>          <artifactId>pagehelper</artifactId>          <version>4.1.6</version>  </dependency>

1)首先先说明集成mybtais,配置挺简单的

##################################################            整合Mybatis                  #######                                       #################  加载Mybatis的相关配置 ################mybatis:  ###########  扫描mapper的配置 #########  mapper-locations: classpath:mapper/*Mapper.xml  ######### mybatis额外的一些配置信息  #######  config-location: classpath:mybatis/mybatis-config.xml  #### 扫描实体  可以直接用类名   不需要加全路径###  type-aliases-package: com.sxs.entity

这里写图片描述

然后启动项目,访问一些地址,以我的案例说明:
这里写图片描述

访问后,我的控制台打印出了3,这个表示有3条数据(我的数据库里面就3条记录)

这里写图片描述

2)介绍使用PageHelp的分页插件
2.1)配置个分页插件的配置
这里写图片描述
相关代码如下,具体什么意思,找度娘。

    @Configurationpublic class MybatisConfigurationConfig {        @Bean        public PageHelper  pageHelper(){            PageHelper pageHelper=new PageHelper();            Properties properties=new Properties();            properties.setProperty("offsetPageNum","true");            properties.setProperty("rowBoundsWithCount", "true");            properties.setProperty("reasonable", "true");            pageHelper.setProperties(properties);            return pageHelper;        }}

然后就可以在原来的查询地方之前使用了,参考如下:
这里写图片描述
启动项目,再次执行这个请求,发现控制打印的是2,说明分页有效。

说明:关于mybatis和分页的配置,网上也有很多,配置的方式很多种,根据自己的业务进行配置即可。

阅读全文
0 0
原创粉丝点击