Butterfly Persistence Map DAO

Jakob Jenkov
Last update: 2021-06-30

From the IDaos instance you have access to a IMapDao instance. This DAO can be used to read records and columns of a ResultSet into a Map. Reading into Map's rather than objects can be handy when you do not know at development time what columns the ResultSet will contain, for instance in dynamic reports. It can also be useful if the columns in the ResultSet doesn't match any objects in your domain, but rather is a composite view of data in the database.

Here is how the IMapDao interface looks:

public Map readMap(String sql)
public Map readMap(String sql, Object ... parameters)
public Map readMap(String sql,
        IPreparedStatementManager statementManager)

public List readMapList(String sql)
public List readMapList(String sql, Object ... parameters)
public List readMapList(String sql,
        IPreparedStatementManager statementManager)

If you have read how the IJdbcDao works, these methods are almost self explanatory. If not, please read the text about the IJdbcDao.

Map DAO Examples

This text contains a few examples of how to use the IMapDao. Here is a list of the examples:

All examples assume a configuration of the PersistenceManager similar to this:

protected static final DataSource dataSource = new SimpleDataSource(
    "org.h2.Driver", "jdbc:h2:tcp://localhost/theDb", "sa", "");

public static final PersistenceManager persistenceManager =
    new PersistenceManager(dataSource);

Map DAO Examples

Below are some examples of how the Map DAO, IMapDao, is used so you can get an idea about what using the IMapDao looks like.

Reading a Map

Reading the columns of a ResultSet into a Map can be done like this:

String sql = "select * from persons, posts " +
             "where person.id = posts.person_id " +
             " and person.id = ?";

IDaos daos = persistenceManager.createDaos();

Map map = daos.getJdbcDao().readMap(sql, 666);


Reading a List of Map's

Reading a list of Map's can be done like this:

String sql = "select * from persons, posts " +
             "where person.id = posts.person_id " +
             " and person.birth_year > ?";

IDaos daos = persistenceManager.createDaos();

List maps = daos.getJdbcDao().readMapList(sql, 1975);


Jakob Jenkov

Featured Videos

P2P Networks Introduction


Java Persistence
Close TOC
All Tutorial Trails
All Trails
Table of contents (TOC) for this tutorial trail
Trail TOC
Table of contents (TOC) for this tutorial
Page TOC
Previous tutorial in this tutorial trail
Next tutorial in this tutorial trail