|
Hi,
I am trying to get my result set as a HashMap as <userid, userObject> map, reprseting all the users from the table. Below is my mapper config and selectmethod from Mapper class. It works fine if there is only one row. If there are more than one row returned it throws too many results exception. I am very very new to iBatis/myBatis (started just last week). Can some one please advice what's causing this and how to fix this? ===== usermapper.xml ======== <resultMap id="usersResultMap" type="hashmap"> <id column="userid" property="key" jdbcType="VARCHAR" /> <association property="value" column="last_name" javaType="com.ge.wolc.lighting.db.model.WolcUser"> <id column="userid" property="userid" jdbcType="VARCHAR" /> <result column="password" property="password" jdbcType="VARCHAR" / > <result column="first_name" property="first_name" jdbcType="VARCHAR" /> <result column="last_name" property="last_name" jdbcType="VARCHAR" /> <result column="group_id_rank" property="group_id_rank" jdbcType="INTEGER" /> <result column="max_pwd_attempts" property="max_pwd_attempts" jdbcType="INTEGER" /> <result column="active" property="active" jdbcType="CHAR" /> <result column="locked" property="locked" jdbcType="CHAR" /> <result column="time_add" property="time_add" jdbcType="DATE" /> <result column="time_mod" property="time_mod" jdbcType="DATE" / > </association> </resultMap> <select id="selectAllUsers" resultMap="usersResultMap"> select <include refid="Base_Column_List" /> from wolc_users </select> ==== abstract method from usermapper interface is something like below ============= Map<String, WolcUser> selectAllUsers(); tried below also HashMap<String, WolcUser> selectAllUsers(); |
|
One hashmap holds one object. So If you want to retrive a list you
should use this: List<Map<String, WolcUser>> selectAllUsers(); El día 26 de marzo de 2012 22:36, User_555 <[hidden email]> escribió: > Hi, > > I am trying to get my result set as a HashMap as <userid, userObject> > map, reprseting all the users from the table. Below is my mapper > config and selectmethod from Mapper class. It works fine if there is > only one row. If there are more than one row returned it throws too > many results exception. I am very very new to iBatis/myBatis (started > just last week). > Can some one please advice what's causing this and how to fix this? > > ===== usermapper.xml ======== > <resultMap id="usersResultMap" type="hashmap"> > <id column="userid" property="key" jdbcType="VARCHAR" /> > <association property="value" column="last_name" > javaType="com.ge.wolc.lighting.db.model.WolcUser"> > <id column="userid" property="userid" jdbcType="VARCHAR" /> > <result column="password" property="password" jdbcType="VARCHAR" / >> > <result column="first_name" property="first_name" > jdbcType="VARCHAR" /> > <result column="last_name" property="last_name" > jdbcType="VARCHAR" /> > <result column="group_id_rank" property="group_id_rank" > jdbcType="INTEGER" /> > <result column="max_pwd_attempts" property="max_pwd_attempts" > jdbcType="INTEGER" /> > <result column="active" property="active" jdbcType="CHAR" /> > <result column="locked" property="locked" jdbcType="CHAR" /> > <result column="time_add" property="time_add" jdbcType="DATE" /> > <result column="time_mod" property="time_mod" jdbcType="DATE" / >> > </association> > </resultMap> > > <select id="selectAllUsers" resultMap="usersResultMap"> > select > <include refid="Base_Column_List" /> > from wolc_users > </select> > > ==== abstract method from usermapper interface is something like below > ============= > Map<String, WolcUser> selectAllUsers(); > tried below also > HashMap<String, WolcUser> selectAllUsers(); |
|
I was able to get the desired results by annotating it as below
@MapKey("userid") (an then change the resultmap's userid property name to "userid" from "key"). Now I am able to get a HashMap (with all users NOT just 1 record) as {userdid=(UserObject)} On Mar 26, 5:06 pm, Eduardo Macarron <[hidden email]> wrote: > One hashmap holds one object. So If you want to retrive a list you > should use this: > > List<Map<String, WolcUser>> selectAllUsers(); > > El día 26 de marzo de 2012 22:36, User_555 <[hidden email]> escribió: > > > > > > > > > Hi, > > > I am trying to get my result set as a HashMap as <userid, userObject> > > map, reprseting all the users from the table. Below is my mapper > > config and selectmethod from Mapper class. It works fine if there is > > only one row. If there are more than one row returned it throws too > > many results exception. I am very very new to iBatis/myBatis (started > > just last week). > > Can some one please advice what's causing this and how to fix this? > > > ===== usermapper.xml ======== > > <resultMap id="usersResultMap" type="hashmap"> > > <id column="userid" property="key" jdbcType="VARCHAR" /> > > <association property="value" column="last_name" > > javaType="com.ge.wolc.lighting.db.model.WolcUser"> > > <id column="userid" property="userid" jdbcType="VARCHAR" /> > > <result column="password" property="password" jdbcType="VARCHAR" / > > > <result column="first_name" property="first_name" > > jdbcType="VARCHAR" /> > > <result column="last_name" property="last_name" > > jdbcType="VARCHAR" /> > > <result column="group_id_rank" property="group_id_rank" > > jdbcType="INTEGER" /> > > <result column="max_pwd_attempts" property="max_pwd_attempts" > > jdbcType="INTEGER" /> > > <result column="active" property="active" jdbcType="CHAR" /> > > <result column="locked" property="locked" jdbcType="CHAR" /> > > <result column="time_add" property="time_add" jdbcType="DATE" /> > > <result column="time_mod" property="time_mod" jdbcType="DATE" / > > > </association> > > </resultMap> > > > <select id="selectAllUsers" resultMap="usersResultMap"> > > select > > <include refid="Base_Column_List" /> > > from wolc_users > > </select> > > > ==== abstract method from usermapper interface is something like below > > ============= > > Map<String, WolcUser> selectAllUsers(); > > tried below also > > HashMap<String, WolcUser> selectAllUsers(); |
|
Hi,
Could you post a sample code showing how to do this ? I've always thought I could only get one Map per result. Dridi http://www.zenika.com/ Le mardi 27 mars 2012 17:36:49 UTC+2, User_555 a écrit : I was able to get the desired results by annotating it as below |
|
Morning Dridi,
First of all, nice post in Zenika's blog, I have linked it in google code. Maps are often a bit difficult to understand. MB3 provides three possibilities: - return a record inside a hashmap - return a list of records inside a hashmap - return a map of records inside beans The first two are just using this: public interface MyMapper{ @Select("select * from user where id=#{id}") Map<String, Object> getUser(long id) } or using the xml syntax <select id="getUser" returnType="map"> select * from user where id=#{id} </select> If you want to retrieve a list of maps, then the mapper should look like: public interface MyMapper{ @Select("select * from user where id=#{id}") List<Map> getUser(long id) } The last option is to use the @MapKey annotation. This annotation lets you retrieve multiple objects inside a Map instead of inside a List. For that purpose you must specify what property will be used as a Key (that is @MapKey for) So this will retrieve a map of users public interface MyMapper{ @MapKey("id") @Select("select * from user where id=#{id}") Map<Long, User> getUser(long id) } Hope I managed to explain it! |
|
Hey Eduardo!
What a surprise, that's very cool! I didn't intend to send you this link since it's in french (maybe you should actually mention this is french in the wiki) btw where did you find my post ? can you read french or did you use a translator ? Thx for your explanation. From what I read, the @MapKey is what I'm looking for. Is there a way to do this with xml configuration ? Thanks again, you made my day! Dridi http://www.zenika.com/ Le jeudi 29 mars 2012 06:52:16 UTC+2, Eduardo a écrit : Morning Dridi, |
|
Google translator does a very good job translating french to
spanish! :) > From what I read, the @MapKey is what I'm looking for. Is there a way to do > this with xml configuration ? There is an API for that: <K,V> Map<K,V> selectMap(String statement, Object parameter, String mapKey) |
| Powered by Nabble | Edit this page |
