java - JDBI, retrieve data with sql query into customized object(constructor) instead of Map -


so when use jdbi query database, getting map<string, object> type.

i want customized object (constructor) instead of map<string, object>.

dbi dbi = establishconnection(url, username, password); handle handle = dbi.open(); list<map<string, object>> rs = handle.select("select * sometable"); 

instead want use:

list<customizedobject> rs = handle.select("select * sometable"); 

where customizedobject class object contains column properties it.

is there way this? found relative documentation, cannot understand implementation.

http://jdbi.org/sql_object_api_queries/

please see previous page in documentation shows how link handle or dbi mappers.

essentially, need mapper convert resultset desired object , interface refer mapper.

let's assume minimal example. first mapper needs provided:

public class customizedobjectmapper implements resultsetmapper<customizedobject> {      @override     public customizedobject map(int index, resultset r, statementcontext ctx)             throws sqlexception {         return new customizedobject(r.getstring("uuid"), r.getstring("other_column"));     }  } 

then need interface define query provides data passed mapper class. 1 result row leads 1 invocation of customizedobjectmapper.map(...):

@registermapper(customizeobjectmapper.class) public interface customizeobjectquery {      @sqlquery("select uuid, other_column schema.relation")     list<customizedobject> get(); } 

finally, objects can retrieved: list<customizedobject> test = dbi.open(customizeobjectquery.class).get().

your can put components on individual basis , omit interface: dbi.open().createquery("select uuid, other_colum schema.relation").map(new eventmapper()).list()


Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -