hbwf / mybatis

Automatically exported from code.google.com/p/mybatis
0 stars 0 forks source link

Add ability of @ResultMap to take a class #774

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
MyBatis Version: 3.1.1

I currently have xml resultmaps and use the @ResultMap annotaion in my mapper 
interfaces. I would like to request the ability to inject a class into 
@ResultMap. I would also like to request annotaions that can be added to the 
fields of a class to denote what coloumns go with that field, and an annotaion 
for complex feilds that basically defined them as an association.

This would be great and completely elimiate my mapper xml files.

Example:
Mapper Interface...

@Select("SELECT * FROM LOG)
@ResultMap(Log.class)
List<Log> selectAllLogs();

...End Mapper Interface

Log Class...

public class Log{
  @ResultColumn("DATE")
  private String date;

  @ResultColumn("TIME")
  privte String time;

  @ResultAssociation()
  private User user;

}

User Class...

public class User {

  @ResultColumn("FIRST_NAME")
  private String firstName;

  @ResultCoulmn("LAST_NAME")
  private String lastName;
}

I hope my example helps to clarify my request.

Thanks,
Patrick Wiltrout

Original issue reported on code.google.com by patrick....@gmail.com on 14 Feb 2013 at 3:14

GoogleCodeExporter commented 9 years ago
You can accomplish this now without XML:

@Select("SELECT * FROM LOG)
@Results ({
  @Result(column="DATE", property="date"),
  @Result(column="TIME", property="time"),
  @Result(column="FIRST_NAME", property="user.firstName"),
  @Result(column="LAST_NAME", property="user.lastName")
})
List<Log> selectAllLogs();

Original comment by jeffgbut...@gmail.com on 22 Feb 2013 at 7:45

GoogleCodeExporter commented 9 years ago
Yes, I agree that using the @Results annotation is a solution, but it is not 
reusable. I have to copy and paste it above every method that uses that 
mapping. By having the mapping defined inside the class, the mapping then 
becomes reusable. All we would beed to do is pass the class into an annotation 
above each method. It makes the code cleaner and easy to maintain. That way if 
something in my mapping were to change, i would just go into the class and 
change the rules, verses changes every @Results annotation.  

Original comment by patrick....@gmail.com on 22 Feb 2013 at 8:26

GoogleCodeExporter commented 9 years ago
OK.  I understand the concept.

I think there might be an easier way to deal with this if we could name the 
result map created with annotations on one method and then refer to it with the 
existing @ResultMap annotation.  This would be equivalent function to the XML 
and would avoid putting MyBatis annotations in your domain objects - which I 
think would be a good goal.

WDYT?

Original comment by jeffgbut...@gmail.com on 23 Feb 2013 at 2:21

GoogleCodeExporter commented 9 years ago
That sounds good to me. I would consider that a good solution to my problem.

Original comment by patrick....@gmail.com on 23 Feb 2013 at 11:22

GoogleCodeExporter commented 9 years ago
yes, i agree with you patrick, Iam new in mybatis. that ide look good and less 
code in xml

Original comment by abion...@gmail.com on 3 May 2014 at 11:14

GoogleCodeExporter commented 9 years ago
Any traction on this? 

Original comment by pe...@club-os.com on 10 Jul 2014 at 3:27