IDea Maven工程创建的分页查询

本文详细介绍了如何在使用IDEa和Maven构建的Java项目中实现分页查询功能。从配置Maven依赖,到设置数据源、引入MyBatis或JPA,再到编写分页查询的SQL和Service层代码,一步步指导开发者实现高效的数据分页展示。同时,还探讨了分页查询的优化技巧,包括缓存策略和减少数据库负担的方法。
摘要由CSDN通过智能技术生成

idea 中的接口
package cn.bdqn.dao;

import cn.bdqn.entity.employee;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by linlin on 2017/6/16.
 */
public interface empdao {
    public List<employee> selectemp(int pageindex,int pagesize)throws Exception;
    public int getCount() throws SQLException;

}

实现类
package cn.bdqn.service;

import cn.bdqn.dao.empdao;
import cn.bdqn.entity.employee;
import cn.bdqn.util.BaseDAO;
import org.junit.Test;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by linlin on 2017/6/16.
 */
public class empservice extends BaseDAO implements empdao {

    public List<employee> selectemp(int pageindex,int pagesize) throws Exception {
        List<employee> list=new ArrayList<employee>();
        String sql="select * from employee limit ?,?";
        ResultSet rs=executeQuery(sql,pageindex,pagesize);
        if(rs!=null){
            while(rs.next()){
                employee emp=new employee();
                emp.setName(rs.getString("name"));
                emp.setSalary(rs.getDouble("salary"));
                emp.setDepartment_id(rs.getInt("department_id"));
                emp.setPassword(rs.getInt("password"));
                list.add(emp);
            }
        }
        return list;
    }
/*
@Test
public void Tests() throws Exception {
    List<employee> count = selectemp(0,3);
    for (employee item:count) {
        System.out.println(item.getName());
    }
}
*/


    public int getCount() throws SQLException {
        int result=0;
        String sql="select count(*) as num from employee";
        ResultSet rs= null;
        try {
            rs = executeQuery(sql);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(rs!=null){
                    if(rs.next()){
                result=rs.getInt("num");
            }
            closeAll();
        }
        return result;
    }
}

servlet 
package cn.bdqn.servlet;

import cn.bdqn.entity.employee;
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值