Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ResultSetMapper for List<Map<String, Object>> for SqlQuery #81

Closed
nonameplum opened this issue Oct 7, 2013 · 6 comments
Closed

ResultSetMapper for List<Map<String, Object>> for SqlQuery #81

nonameplum opened this issue Oct 7, 2013 · 6 comments

Comments

@nonameplum
Copy link

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

@lixinyilalala
Copy link

Have solved it? I also meet the same problem.

@stevenschlansker
Copy link
Member

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.

@qualidafial
Copy link
Member

qualidafial commented Jan 2, 2017 via email

@qualidafial
Copy link
Member

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.

@sisingh
Copy link

sisingh commented Jul 7, 2017

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

@michael-newsrx
Copy link

How to do this in JDBI3?

@Override
	@SqlQuery("select * from "+table+" where articleId = :id")
	@RegisterRowMapper(MapMapper.class)
	Map<String, Object> rowAsMap(@Bind("id") int id);

@Override
	@SqlQuery("select * from "+table+" where articleId>=:from AND articleId<:before")
	@RegisterRowMapper(MapMapper.class)
	List<Map<String, Object>> rowsAsMapList(@Bind("from") int from, @Bind("before") int before);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

6 participants