码迷,mamicode.com
首页 > 其他好文 > 详细

反射实现增删改查(DAO层)——查询数据

时间:2018-05-19 23:24:11      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:print   append   count()   for   result   number   objectc   文件   实例   

先贴出代码,后续补充自己的思路、配置文件、使用方式:
/**
     * 
     * 数据查询
     * 
     */
    @Override
    public List<?> queryObject(List<Map<String, Object>> params,
            String tableName) {
        List<Object> objectList = new ArrayList<Object>();
        StringBuilder sql = new StringBuilder("SELECT * FROM " + tableName
                + " WHERE 1=1");
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            connection = DBConnection.getConnection();
            if (params != null && params.size() > 0) {
                for (int i = 0; i < params.size(); i++) {
                    Map<String, Object> map = params.get(i);
                    sql.append(" AND  " + map.get("name") + " "
                            + map.get("rela") + " " + map.get("value") + " ");
                }
            }
            preparedStatement = connection.prepareStatement(sql.toString());

            resultSet = preparedStatement.executeQuery();
            ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
            // 获取数据列数
            int columnsCount = resultSetMetaData.getColumnCount();
            Field field = null;
            Object object = null;
            while (resultSet.next()) {
                /**
                 * 获取实例化对象
                 */
                object = objectClass.newInstance();

                String columnName = null;
                String columnTypeName = null;
                String columnValue = null;

                for (int i = 1; i <= columnsCount; i++) {
                    columnName = resultSetMetaData.getColumnName(i);
                    columnTypeName = resultSetMetaData.getColumnTypeName(i);
                    columnValue = resultSet.getString(i);
                    field = object.getClass().getDeclaredField(columnName);
                    field.setAccessible(true);
                    switch (columnTypeName) {
                    case "INT":
                        field.set(object, Integer.parseInt(columnValue));
                        break;
                    case "VARCHAR":
                        field.set(object, columnValue);
                        break;
                    case "FLOAT":
                        field.set(object, Float.parseFloat(columnValue));
                        break;
                    case "Date":
                        field.set(object,
                                new SimpleDateFormat().parse(columnValue));
                        break;
                    }

                }
                objectList.add(object);

            }

        } catch (SQLException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } finally {
            DBConnection.close(connection, preparedStatement, resultSet);
        }
        return objectList;
    }

反射实现增删改查(DAO层)——查询数据

标签:print   append   count()   for   result   number   objectc   文件   实例   

原文地址:https://www.cnblogs.com/caoleiCoding/p/9061938.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!