第一步
导入mybatis-plus和mysql依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
创建配置类
@Configuration
public class MyBatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return mybatisPlusInterceptor;
}
}
我在这里封装了翻页的参数,分别是当前页和每页展示的数量, 使用我们的参数类继承该类即可
@Data
public class BaseParam {
/**
* 默认查询第一页
*/
private Long currentPage = 1L;
/**
* 默认每页查询10条数据
*/
private Long pageSize = 10L;
}
参数都是Long类型
mapper层
Page<User> findAll(Page<User> page);
mapper层对应的xml文件
<resultMap id="userresult" type="com.hbl.xiaofei.entity.User">
<id property="id" column="id"/>
<result property="age" column="age"/>
<result property="name" column="name"/>
<result property="createTime" column="create_time"/>
<result property="updateTime" column="update_time"/>
</resultMap>
<sql id="user">
id,age,name,create_time,update_time
</sql>
<select id="findAll" resultMap="userresult">
select <include refid="user"></include>
from user
</select>
service层查询分页后的方法
@Override
public Page<User> findAll(Page<User> page) {
return userMapper.findAll(page);
}
直接在controller层调用
@PostMapping ("select")
public Page<User> select(@RequestBody UserParam userParam){
Page<User> page = new Page<>(userParam.getCurrentPage(),userParam.getPageSize());
Page<User> list = userService.findAll(page);
return list;
}
list可以直接返回给前端