JavaでPageHelperを使う方法は何ですか?
JavaでPageHelperを使用してページング操作を行う方法は以下の通りです:
- 最初に、PageHelperの依存関係をあなたのプロジェクトに導入するために、MavenやGradleを使用して依存関係を追加することができます。
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.2.1</version>
</dependency>
- あなたのMapperインターフェースに、PageHelperのメソッドを追加して、ページのパラメータを設定します。
import com.github.pagehelper.Page;
public interface YourMapper {
List<YourEntity> selectByPage(Page<YourEntity> page);
}
- マッパーインターフェースのXMLファイルに適切なSQL文を記述してください。
<select id="selectByPage" resultType="YourEntity">
SELECT * FROM your_table
</select>
- Service層でのページヘルパーのメソッドを使用してページング検索を行います。
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();
}
}
- 最後に、Controller層でService層のメソッドを呼び出してページデータを取得します。
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を使用してページ分割を行うことができます。お役に立てれば幸いです!