大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE稳定放心使用
在java项目中不使用mybatis的pageHelper进行数据分页:
1. 分页工具类编写:
import java.util.List;
public class PageModel<E> {
//结果集
private List<E> list;
//查询记录数
private int totalRecords;
//每页多少条数据
private int pageSize;
//第几页
private int pageNo;
/**
* 总页数
* @return
*/
public int getTotalPages() {
return (totalRecords + pageSize - 1) / pageSize;
}
/**
* 取得首页
* @return
*/
public int getTopPageNo() {
return 1;
}
/**
* 上一页
* @return
*/
public int getPreviousPageNo() {
if (pageNo <= 1) {
return 1;
}
return pageNo - 1;
}
/**
* 下一页
* @return
*/
public int getNextPageNo() {
if (pageNo >= getBottomPageNo()) {
return getBottomPageNo();
}
return pageNo + 1;
}
/**
* 取得尾页
* @return
*/
public int getBottomPageNo() {
return getTotalPages();
}
public List<E> getList() {
return list;
}
public void setList(List<E> list) {
this.list = list;
}
public int getTotalRecords() {
return totalRecords;
}
public void setTotalRecords(int totalRecords) {
this.totalRecords = totalRecords;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPageNo() {
return pageNo;
}
public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}
}
2.数据库实体类
public class Demo {
private int id;
private String name;
public Demo(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
3.数据分页查询(下面为一个方法,类名为:JdbcService)
/**
*
* @param pageNo 当前第几页
* @param pageSize 每页显示数据量
* @return
* @throws SQLException
*/
public PageModel getDate(int pageNo, int pageSize) throws SQLException {
String sql = "select * from demo limit ?,?";
PageModel pageModel = null;
PreparedStatement pstm = null;
ResultSet rs = null;
Demo demo = null;
Connection connection = null;
List<Demo> list = new ArrayList<>();
try {
//分页查询的数据
connection = getDataSource().getConnection();
pstm = connection.prepareStatement(sql);
pstm.setInt(1, (pageNo - 1) * pageSize);
pstm.setInt(2, pageSize);
rs = pstm.executeQuery();
while (rs.next()) {
list.add(new Demo(rs.getInt("id"),rs.getString("name")));
}
//查询总条数
ResultSet resultSet1 = pstm.executeQuery("select count(*) from demo");
int total = 0;
if (resultSet1.next()) {
total = resultSet1.getInt(1);
}
resultSet1.close();
//将数据放到分页工具类中
pageModel = new PageModel<>();
pageModel.setPageNo(pageNo);
pageModel.setPageSize(pageSize);
pageModel.setTotalRecords(total);
pageModel.setList(list);
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null) {
rs.close();
}
if (pstm != null) {
pstm.close();
}
if (connection != null) {
connection.close();
}
}
if (connection==null){
System.out.println("close");
}
if (connection!=null){
System.out.println(connection);
}
return pageModel;
}
4.controller层调用数据(一个接口方法)
//注解注入
Autowired
private JdbcService jdbcService;
@GetMapping("/getDemo")
public PageModel getDemo(int pageNo, int pageSize){
PageModel date = null;
try {
date = jdbcService.getDate(pageNo, pageSize);
} catch (SQLException e) {
e.printStackTrace();
}
return date;
}
5.显示结果解析
参考:https://www.open-open.com/lib/view/open1346772322162.html
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/186465.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...