随笔-1  评论-1  文章-2  trackbacks-0

      简介:该框架吸取IBatis设计思想和方法映射机制,结合模式匹配,将查询结果直接映射成域对象模型,并支持分页。
      代码如下:
package heroking.db;

import java.util.List;
import java.util.ArrayList;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.sql.*;


public class GeneralDao {
    private Connection connection = null;

    public Connection getConnection() {
        return connection;
    }

    public void setConnection(Connection connection) {
        this.connection = connection;
    }

    /**
     * 执行插入,更新,删除操作,返回值为影响记录数。
     * @param sql
     * @param parameter
     */
    public int executeUpdate(String sql, Object parameter) {
        List result = new ArrayList();
        //按模式匹配转换sql语句
        Pattern pattern = Pattern.compile("#[a-zA-Z]+#");   //模式匹配
        Matcher matcher = pattern.matcher(sql);
        String formatSql = matcher.replaceAll("?");
        //连接数据源
        PreparedStatement st = null;
        try {
            st = connection.prepareStatement(formatSql);
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        //添加预处理参数
        int i = 1;
        while (matcher.find()) {
            String field = matcher.group();
            String formatField = field.substring(1, field.length() - 1);
            try {
                st.setObject(i++, DataBaseUtil.excuteGetMethod(formatField, parameter));
            } catch (SQLException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
        try {
            return st.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return 0;
    }
   

    /**
     * 查询第pageCount页面(每个页面的记录数为pageSize)的结果
     * @param sql   sql查询
     * @param parameter 参数对象
     * @param pageSize 页面结果集大小
     * @param pageCount 页面数
     * @param resultClass 结果类
     * @return List
     */
    public List executeQuerys(String sql, Object parameter, int pageSize, int pageCount, Class resultClass){
        List result = new ArrayList();
        //按模式匹配转换sql语句
        Pattern pattern = Pattern.compile("#[a-zA-Z]+#");   //模式匹配
        Matcher matcher = pattern.matcher(sql);
        String formatSql = matcher.replaceAll("?");
        //连接数据源
        PreparedStatement st = null;
        try {
            //设置预处理状态参数,滚动,只读。
            st = connection.prepareStatement(formatSql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        //添加预处理参数
        int i = 1;
        while (matcher.find()) {
            String field = matcher.group();
            String formatField = field.substring(1, field.length() - 1);
            try {
                st.setObject(i++, DataBaseUtil.excuteGetMethod(formatField, parameter));
            } catch (SQLException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
        ResultSet rs = null;
        try {
            rs = st.executeQuery();
            //获取总记录数
            rs.last();
            int totalCount = rs.getRow();

            //当前页面第一条记录位置
            int curPagePosition = (pageCount -1)*pageSize + 1;
            if (totalCount < curPagePosition) {
                return null;
            }
            rs.beforeFirst();
            rs.absolute(curPagePosition);

            //封装值对象
            int k = 0;
            ResultSetMetaData rsmd = rs.getMetaData();
            int cols = rsmd.getColumnCount();
            while (rs.next() && k < pageSize) {
                Object o = resultClass.newInstance();
                for (int j = 1; j <= cols; j++) {
                    String name = rsmd.getColumnName(j);
                    Object value = rs.getObject(j);//作通用类型处理,这样row中的类型都是Object型的。
                    String voName = DataBaseUtil.toInValueName(name);
                    DataBaseUtil.executeSetMethod(voName, value, o);
                }
                result.add(o);
                k++;    //获取数加1
            }
        } catch (SQLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (IllegalAccessException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (InstantiationException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return result;
    }
    /**
     * 查询所有纪录
     * @param sql
     * @param parameter
     * @param resultClass
     * @return List
     */
    public List executeQuerys(String sql, Object parameter, Class resultClass){
        //默认为所有记录
        return this.executeQuerys(sql, parameter, Integer.MAX_VALUE, 1, resultClass);
    }

    /**
     * 查询单个记录
     * @param sql
     * @param parameter
     * @param resultClass
     * @return Object
     */
    public Object executeQuery(String sql, Object parameter, Class resultClass){
        List list = executeQuerys(sql, parameter,resultClass);
        if (list == null || list.size() == 0) {
            return null;
        }
        return list.get(0);
    }
}

posted on 2006-01-04 11:15 Hero King 阅读(151) 评论(0)  编辑  收藏 所属分类: 实践

只有注册用户登录后才能发表评论。


网站导航:
 
<2025年4月>
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

常用链接

留言簿(1)

随笔档案

文章分类

文章档案

博客

搜索

  •  

最新评论