Spring Boot整合MyBatis-Plus实现分页功能的步骤如下:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus-version}</version>
</dependency>
#MyBatis-Plus配置
mybatis-plus.mapper-locations=classpath*:/mapper/**/*.xml
mybatis-plus.type-aliases-package=com.example.domain
public interface UserMapper extends BaseMapper<User> {
IPage<User> selectUserPage(Page<User> page, @Param("name") String name);
}
<select id="selectUserPage" resultType="com.example.domain.User">
SELECT *
FROM user
WHERE name = #{name}
</select>
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public IPage<User> getUserPage(Page<User> page, String name) {
return userMapper.selectUserPage(page, name);
}
}
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/users")
public IPage<User> getUsers(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, @RequestParam String name) {
Page<User> page = new Page<>(pageNum, pageSize);
return userService.getUserPage(page, name);
}
}
通过以上步骤,就可以在Spring Boot项目中实现MyBatis-Plus的分页功能。