ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Pascal DeMilly <list.iba...@newgenesys.com>
Subject java.util.HashMap as resultClass
Date Mon, 24 Jan 2005 16:42:25 GMT

I would like to retrieve a Map with whose key is the 1st column of my
query and the value is the 2nd column. For example right now I do:

<select id="getItemNameMap" resultClass="java.util.HashMap">
	select SKU, Description from Items

However this returns a list of maps with the key being the column name
and the value the column value which seems wasteful in term of space (I
am using remoting to retrieve that info).

I currently solve it by using a custom rowHandler in my DAO as follow:

    public Map getItemNames () throws DataAccessException {
	final KeyValueHandler rowHandler = new KeyValueHandler ();
        getSqlMapClientTemplate().queryWithRowHandler("getItemNameMap", null, rowHandler);
        return rowHandler.getMap();
    private class KeyValueHandler implements RowHandler {
        final Map map = new HashMap ();

        public void handleRow(Object valueObject) {
            final Map row = (Map) valueObject; 
            map.put (row.get("SKU"), row.get("Description"));
        public Map getMap () {
            return map;
But I would like to move possibly that code out of my DAO code and into
iBatis SqlMap file.

How could I do that



View raw message