Example usage for org.apache.ibatis.type TypeHandler getResult

List of usage examples for org.apache.ibatis.type TypeHandler getResult

Introduction

In this page you can find the example usage for org.apache.ibatis.type TypeHandler getResult.

Prototype

T getResult(CallableStatement cs, int columnIndex) throws SQLException;

Source Link

Usage

From source file:com.glaf.core.jdbc.QueryHelper.java

License:Apache License

public List<Map<String, Object>> getResults(ResultSet rs) {
    logger.debug("--------------use mybatis results----------------");
    try {//  w  w w  . jav  a2 s .  co  m
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        List<String> columns = new ArrayList<String>();
        List<TypeHandler<?>> typeHandlers = new ArrayList<TypeHandler<?>>();
        ResultSetMetaData rsmd = rs.getMetaData();
        for (int i = 0, n = rsmd.getColumnCount(); i < n; i++) {
            columns.add(rsmd.getColumnLabel(i + 1));
            try {
                Class<?> type = Resources.classForName(rsmd.getColumnClassName(i + 1));
                TypeHandler<?> typeHandler = typeHandlerRegistry.getTypeHandler(type);
                if (typeHandler == null) {
                    typeHandler = typeHandlerRegistry.getTypeHandler(Object.class);
                }
                typeHandlers.add(typeHandler);
            } catch (Exception ex) {
                ex.printStackTrace();
                typeHandlers.add(typeHandlerRegistry.getTypeHandler(Object.class));
            }
        }
        while (rs.next()) {
            Map<String, Object> row = new HashMap<String, Object>();
            for (int i = 0, n = columns.size(); i < n; i++) {
                String name = columns.get(i);
                TypeHandler<?> handler = typeHandlers.get(i);
                Object value = handler.getResult(rs, name);
                row.put(name, value);
                if (value != null && value instanceof java.util.Date) {
                    java.util.Date date = (java.util.Date) value;
                    row.put(name + "_date", DateUtils.getDate(date));
                    row.put(name + "_datetime", DateUtils.getDateTime(date));
                }
            }
            list.add(row);
        }
        return list;
    } catch (SQLException ex) {
        logger.error(ex);
        ex.printStackTrace();
        throw new RuntimeException(ex);
    } finally {
        try {
            if (rs != null) {
                rs.close();
                rs = null;
            }
        } catch (SQLException e) {
        }
    }
}

From source file:com.hand.hap.mybatis.mapperhelper.MultipleJdbc3KeyGenerator.java

License:Open Source License

private void populateKeys(ResultSet rs, MetaObject metaParam, String[] keyProperties,
        TypeHandler<?>[] typeHandlers) throws SQLException {
    for (int i = 0; i < keyProperties.length; i++) {
        TypeHandler<?> th = typeHandlers[i];
        if (th != null) {
            Object value = th.getResult(rs, i + 1);
            metaParam.setValue(keyProperties[i], value);
        }/*from  w  w  w.  j  a v a  2  s.  c  o  m*/
    }
}