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的分頁功能。