在Java中使用PageHelper進行分頁操作的方法如下:
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.2.1</version>
</dependency>
import com.github.pagehelper.Page;
public interface YourMapper {
List<YourEntity> selectByPage(Page<YourEntity> page);
}
<select id="selectByPage" resultType="YourEntity">
SELECT * FROM your_table
</select>
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
public class YourService {
@Autowired
private YourMapper yourMapper;
public List<YourEntity> selectByPage(int pageNum, int pageSize) {
Page<YourEntity> page = PageHelper.startPage(pageNum, pageSize);
yourMapper.selectByPage(page);
return page.getResult();
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class YourController {
@Autowired
private YourService yourService;
@GetMapping("/yourData")
public List<YourEntity> getYourData(@RequestParam int pageNum, @RequestParam int pageSize) {
return yourService.selectByPage(pageNum, pageSize);
}
}
通過以上步驟,你就可以在Java中使用PageHelper進行分頁操作了。希望對你有幫助!