首先依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<!--PageHelper依赖-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.24</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<!-- 使用Spring Validation,对接口的参数进行合法性校验-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<!-- JWT令牌-->
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>4.4.0</version>
</dependency>
<!-- JSON -->
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.18</version>
</dependency>
</dependencies>
yml:
mybatis:
type-aliases-package: usx.xwt.taotao.domain //实体类包
mapper-locations: classpath:/mapper/*.xml //mybatis xml(数据库语句文件)
pagehelper:
helper - dialect: mysql # 设置数据库方言,根据实际数据库修改
reasonable: true
这里的PageHelper依赖一定要看清(很重要),因为我之前就是用的不是springboot的(那种好像挺麻烦的,还要自己配置mybatis)所以出问题了,而且这两个还不能兼容(两个依赖只能选着一个),plus用法也不同,记得辨别清楚!
Page<Thread> page = PageHelper.startPage(pageNo, pageSize);//Thread是你的实体类,两个参数分别是 当前页 和 当前页数据的数量
if(idName.equals("threadId")){
mapper.getRepliesWithUserInfoByThreadId(id);
}
else {
mapper.getRepliesWithUserInfoByReplyId(id);
} //查询数据库
ResponsePage pageResult = new ResponsePage(page.getPageNum(), (int) page.getTotal(), page.getPages()); //自己封装的
System.out.println(pageResult);
System.out.println(page);
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ResponsePage {
private int current; //当前页数
private int total; //总的数据量
private int pages; //总的页数
}
当然page还能返回其他数据,这个网上很好找,其实这三个应该够用了!