Jdbi: ResultSetMapper for List<Map<String, Object>> for SqlQuery

Created on 7 Oct 2013  ยท  5Comments  ยท  Source: jdbi/jdbi

Hi,
I'm trying to create mapper for regular SqlQuery in the same manner like handle.select that is returning
List of Map String, Object

Code example:

public interface SomeQueries
{
    @SqlQuery("select * from SOMETABLE")
    @Mapper(MapMappper.class)
    public List<Map<String, Object>> sqlQuery();
}

public class MapMappper implements ResultSetMapper<List<Map<String, Object>>> {

    @Override
    public List<Map<String, Object>> map(int index, ResultSet r, StatementContext ctx) throws SQLException {
        System.out.println(r);
        Map<String, Object> obj = new HashMap<String, Object>();
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

        list.add(obj);
        return list;
    }
}

But I'm getting exception: unable to access mapper

Most helpful comment

Try this:

public class MapMapper implements ResultSetMapper<Map<String, Object>> {
    @Override
    public Map<String, Object> map(int index, ResultSet r, StatementContext ctx) throws SQLException {
        Map<String, Object> map = new HashMap<String, Object>();
        for (int i = 1; i <= r.getMetaData().getColumnCount(); i++) {
            map.put(r.getMetaData().getColumnLabel(), r.getObject(i));
        }
        return map;
    }
}

I should point out that the default mapper used by Query is already available as public API: org.skife.jdbi.v2.DefaultMapper. You can just use it directly if you want.

All 5 comments

Have solved it? I also meet the same problem.

Hi, if you are getting an exception, can you please post the exact exception trace you are seeing? A self contained code example would also be useful if further debugging is necessary.

I believe the problem is your mapper maps List> instead
of just Map. ResultSetMapper maps a single row of a result
set. Putting it into a list is Query's job, not the mapper.

-Matthew

On Jan 2, 2017 04:23, "ะะปะตะบัะตะน ะŸะพะฟั€ัะดัƒั…ะธะฝ" notifications@github.com wrote:

org.skife.jdbi.v2.MappingRegistry$1: No mapper registered for
java.util.Map
at org.skife.jdbi.v2.MappingRegistry.mapperFor(MappingRegistry.java:83)
at org.skife.jdbi.v2.RegisteredMapper.map(RegisteredMapper.java:35)
at org.skife.jdbi.v2.Query$4.munge(Query.java:183)
at org.skife.jdbi.v2.QueryResultSetMunger.munge(
QueryResultSetMunger.java:41)
at org.skife.jdbi.v2.SQLStatement.internalExecute(SQLStatement.java:1344)
at org.skife.jdbi.v2.Query.fold(Query.java:173)
at org.skife.jdbi.v2.Query.list(Query.java:82)
at org.skife.jdbi.v2.sqlobject.ResultReturnThing$
IterableReturningThing.result(ResultReturnThing.java:253)
at org.skife.jdbi.v2.sqlobject.ResultReturnThing.map(
ResultReturnThing.java:46)
at org.skife.jdbi.v2.sqlobject.QueryHandler.invoke(QueryHandler.java:41)
at org.skife.jdbi.v2.sqlobject.SqlObject.invoke(SqlObject.java:220)
at org.skife.jdbi.v2.sqlobject.SqlObject$2.intercept(SqlObject.java:111)
at org.skife.jdbi.v2.sqlobject.CloseInternalDoNotUseThisClass
$$EnhancerByCGLIB$$33103a76.selectTempChatMessages()

โ€”
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/jdbi/jdbi/issues/81#issuecomment-269960388, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AACW5cJdVzRYKloa4Q-kOIfF6rgavit-ks5rON49gaJpZM4BEn1-
.

Try this:

public class MapMapper implements ResultSetMapper<Map<String, Object>> {
    @Override
    public Map<String, Object> map(int index, ResultSet r, StatementContext ctx) throws SQLException {
        Map<String, Object> map = new HashMap<String, Object>();
        for (int i = 1; i <= r.getMetaData().getColumnCount(); i++) {
            map.put(r.getMetaData().getColumnLabel(), r.getObject(i));
        }
        return map;
    }
}

I should point out that the default mapper used by Query is already available as public API: org.skife.jdbi.v2.DefaultMapper. You can just use it directly if you want.

@qualidafial , thanks a lot. This is the real help. I struggled like anything :) ultimately your answer is the correct one.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jimmyhmiller picture jimmyhmiller  ยท  6Comments

qualidafial picture qualidafial  ยท  3Comments

mcarabolante picture mcarabolante  ยท  4Comments

bakstad picture bakstad  ยท  5Comments

keith-miller picture keith-miller  ยท  3Comments