封装数据库查询分页操作

package com.project.dao.impl;


import com.project.bean.CutPageBean;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.*;

import java.util.ArrayList;
import java.util.List;

public class BaseDao {
    
    
    protected Connection con ;
    protected PreparedStatement ps ;
    /**结果集*/
    protected ResultSet rs ;

    public void setConnection(){
    
    
        try {
    
    
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:23456/mydb?characterEncoding=utf-8",
                    "root",
                    "jiang");
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
    }

    public void closeConnection(){
    
    
        try {
    
    
            if(rs!=null){
    
    
                rs.close();
            }
            if(ps!=null){
    
    
                ps.close();
            }
            if(con!=null){
    
    
                con.close();
            }
        } catch (SQLException e) {
    
    
            e.printStackTrace();
        }
    }

    public void updateDB(String sql,Object... valueArray){
    
    
        this.setConnection();
        try {
    
    
            ps = con.prepareStatement(sql);
            for(int i = 0 ; i < valueArray.length ;i++ ){
    
    
                ps.setObject(i+1, valueArray[i]);
            }
            ps.executeUpdate();
        } catch (Exception e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            this.closeConnection();
        }
    }

    /**
     * 查询方法
     * @param sql SQL语句
     * @param beanClass 集合封装实体类类模板
     * @param valueArray 查询值列表
     * @return 集合
     */
    public List findDB(String sql,Class beanClass,Object... valueArray){
    
    

        this.setConnection();
        try {
    
    
            ps = con.prepareStatement(sql);
            if(valueArray != null){
    
    
                for(int i = 0 ; i < valueArray.length ;i++ ){
    
    
                    ps.setObject(i+1, valueArray[i]);
                }
            }
            return this.fullData(beanClass);

//            //得到实体类属性列表
//            Field[] farray = beanClass.getDeclaredFields();
//            rs = ps.executeQuery();
//            while(rs.next()){
    
    
//                //产生实体类的对象
//                Object beanObj = beanClass.newInstance();
//                for(Field f : farray){
    
    
//                    忽略访问修饰符的检查
//                    f.setAccessible(true);
//                    由于属性名和列名一样,从结果集中得到查询列的值
//                    Object value = rs.getObject(f.getName());
//                    f.set(beanObj, value);
//                }
//                list.add(beanObj);
//            }
        } catch (Exception e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            this.closeConnection();
        }
        return null;
    }

    /**
     * 从结果集中封装list集合
     * @param beanClass 实体类的类模板
     * @throws Exception
     */
    private List fullData(Class beanClass) throws Exception {
    
    
        List list = new ArrayList();
        rs = ps.executeQuery();
        //得到结果集合信息的对象
        ResultSetMetaData rm = rs.getMetaData();
        //获取结果集的列数
        int columnCount =  rm.getColumnCount();
        while (rs.next()){
    
    
            //产生实体类对象
            Object beanObj = beanClass.newInstance();
            for(int i = 0; i< columnCount ;i++){
    
    
                //得到指定列的名称
                String columnName = rm.getColumnName(i+1);
                //从结果集中查询指定列的值
                Object valueObj = rs.getObject(columnName);
                //得到需要填值的属性对象
                Field f = beanClass.getDeclaredField(columnName);

                Method method = this.getMethod(beanClass, f);
                method.invoke(beanObj, valueObj);

//                    //去掉修饰符的检查
//                    f.setAccessible(true);
//                    //将指定对象的当前属性,设置为指定的值
//                    f.set(beanObj, valueObj);
            }
            list.add(beanObj);
        }
        return list;
    }
    /**
     * 根据属性对象得到set方法得对象
     * @param field 属性对象
     * @return 方法对象
     */
    private Method getMethod(Class beanClass,Field field){
    
    
        String methodName =  "set" +
                field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1);
        try {
    
    
             return beanClass.getMethod(methodName,field.getType());
        } catch (Exception e) {
    
    
            e.printStackTrace();
    }

        return null;

    }

    public CutPageBean cutPage(int page,String sql,Class beanClass,Object... valueArray){
    
    
        CutPageBean cutBean = new CutPageBean();
        this.setConnection();
        try {
    
    
            ps = con.prepareStatement(sql + " limit ?,?");
            if(valueArray != null){
    
    
                for(int i = 0 ; i < valueArray.length ;i++){
    
    
                    ps.setObject(i+1, valueArray[i]);
                }
            }
            ps.setObject(valueArray.length+1 ,(page-1)*CutPageBean.PAGESIZE);
            ps.setObject(valueArray.length+2, CutPageBean.PAGESIZE);

            cutBean.setList(this.fullData(beanClass));

            ps = con.prepareStatement("select count(*) c from (" + sql + ") temp");
            if(valueArray != null){
    
    
                for(int i = 0 ; i < valueArray.length ;i++){
    
    
                    ps.setObject(i+1, valueArray[i]);
                }
            }
            rs = ps.executeQuery();
            if(rs.next()){
    
    
                cutBean.setCount(rs.getInt("c"));
            }

        } catch (Exception e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            this.closeConnection();
        }
        return cutBean ;
    }
}

猜你喜欢

转载自blog.csdn.net/Mr_Dracy/article/details/109157452