数据库查询工具类,传入对象class返回查询的对象列表

2023-12-15 12:39:20

其中的connect(),连接什么数据库就使用什么连接,注意传入的Class字段名必须和select后面的字段名匹配,可以使用别名进行对应

public static <M> List<M> executeQuery(String sql, Class<M> clazz, Object... params) {
        try (Connection connQuery = connect();
             PreparedStatement ps = connQuery.prepareStatement(sql)
        ) {
            if (null != params && params.length > 0) {
                for (int i = 0; i < params.length; i++) {
                    ps.setObject(i + 1, params[i]);
                }
            }
            try (ResultSet rs = ps.executeQuery()) {
                List<M> list = new ArrayList<>();
                Field[] fields = clazz.getDeclaredFields();
                ResultSetMetaData rsmd = rs.getMetaData();
                int columnCount = rsmd.getColumnCount();
                String columnLabel;
                while (rs.next()) {
                    M object = clazz.getDeclaredConstructor().newInstance();
                    for (int i = 1; i <= columnCount; i++) {
                        // getColumnLabel可以得到as 之后的別名 , getColumnName 不能得到as 之后的別名
                        columnLabel = rsmd.getColumnLabel(i);
                        for (Field field : fields) {
                            if (field.getName().equals(columnLabel)) {
                                judgeType(rs, rsmd, object, i, field);
                                break;
                            }
                        }
                    }
                    list.add(object);
                }
                return list;
            }
        } catch (Exception e) {
            log.error(sql + "查询异常!!", e);
            return Collections.emptyList();
        }
    }

    private static <M> void judgeType(ResultSet rs, ResultSetMetaData rsmd, M m, int i, Field field) throws SQLException, IllegalAccessException, IOException {
        field.setAccessible(true);
        String fieldType = field.getType().getSimpleName();
        switch (fieldType) {
            case "boolean":
                field.set(m, rs.getBoolean(i));
                break;
            case "byte":
                field.set(m, rs.getByte(i));
                break;
            case "short":
                field.set(m, rs.getShort(i));
                break;
            case "int":
                field.set(m, rs.getInt(i));
                break;
            case "long":
                field.set(m, rs.getLong(i));
                break;
            case "float":
                field.set(m, rs.getFloat(i));
                break;
            case "double":
                field.set(m, rs.getDouble(i));
                break;
            case "Date":
                field.set(m, rs.getDate(i));
                break;
            case "Timestamp":
                field.set(m, rs.getTimestamp(i));
                break;
            case "Time":
                field.set(m, rs.getTimestamp(i));
                break;
            case "String":
                field.set(m, rs.getString(i));
                break;
            case "byte[]":
                InputStream binaryStream = rs.getBlob(i).getBinaryStream();
                byte[] bs = new byte[binaryStream.available()];
                binaryStream.read(bs);
                field.set(m, bs);
                break;
        }
    }

文章来源:https://blog.csdn.net/weixin_43736084/article/details/135013952
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。