该实例在:https://github.com/gholly/Mybatis-PageHelper-SpringBoot
1.首先添加项目依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.0.1</version>
</dependency>
<!--分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!--作者的mybatis插件-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>1.1.3</version>
</dependency>
2.yml配置
server:
port: 8383
spring:
datasource:
url: jdbc:mysql://127.0.0.1/test?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&autoReconnect=true
username: upgrade
password: upgrade
driverClassName: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
#有关插件的配置情况
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
logging:
level.com.test.data: debug
3.实体类model层:
BaseModel.java
package com.test.data.model;
import javax.persistence.*;
/**
* Created by guanguan on 2017/7/31.
*/
public class BaseModel {
@Id //标注用于声明一个实体类的属性映射为数据库的主键列,也可置于属性的getter方法之前
@Column(name="Id")
@GeneratedValue(strategy= GenerationType.AUTO) //用于标注主键的生成策略,通过strategy属性指定,默认情况下,JPA 自动选择一个最适合底层数据库的主键生成策略:SqlServer对应identity,MySQL 对应 auto increment。
private Integer id;
@Transient //声明一个实例变量,当对象存储时,它的值不需要维持
private Integer page=1;
@Transient
private Integer rows=10;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getPage() {
return page;
}
public void setPage(Integer page) {
this.page = page;
}
public Integer getRows() {
return rows;
}
public void setRows(Integer rows) {
this.rows = rows;
}
}
User.java
package com.test.data.model;
/**
* Created by guanguan on 2017/7/20.
*/
public class User extends BaseModel{
private Integer id;
private String userName;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
4.Mybatis的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="com.test.data.dao.UserMapper">
<resultMap id="userResultMap" type="com.test.data.model.User">
<id property="id" column="id"/>
<result property="userName" column="user_name"/>
</resultMap>
<sql id="sql1">
`id`,`user_name`
</sql>
<select id="findBy" resultMap="userResultMap" parameterType="HashMap">
select
<include refid="sql1"/>
from `user`
<trim prefixOverrides="and" prefix="where">
<if test="id!=null">
and `id`=#{id}
</if>
<if test="userName!=null">
and `user_name`=#{userName}
</if>
</trim>
</select>
</mapper>
5.controller文件
package com.test.data.controller;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.test.data.model.User;
import com.test.data.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.List;
/**
* Created by guanguan on 2017/7/20.
*/
@Controller
public class controller {
@Autowired
UserService userService;
@RequestMapping("/")
@ResponseBody
public String index() {
Integer page=2;
Integer rows=3;
PageHelper.startPage(page, rows);
List<User> userList = userService.findBy(new HashMap<Object, Object>());
PageInfo pageInfo = new PageInfo(userList);
return JSON.toJSONString(userList);
}
}
运行结果:
打断点获取pageInfo的值,其中查询的结果在pageInfo的list中: