简体   繁体   中英

Custom mybatis mapper for 2 columns

I have a requirement where in I will be getting back data from 2 columns like day and month but I want to convert it in to a Date object nd set it into my bean class.

Is this possible without adding the property to the java class?

I tried checking for custom result handler but the examples were not clear enough.Are there hooks to run some kind of custom handler before returning back from the select method?

What you are talking about in MyBatis terms is called a Result Map Which is used to map fields withing a type to columns withing a database table.

It can be set up in two ways. XML Config :

<resultMap id = "result" type = "Date">
   <result property = "month" column = "monthcolumn"/>
   <result property = "day" column = "daycolumn"/>
   <result property = "year" column = "yearcolumn"/>
</resultMap>

<select id = "getDate" resultMap = "result">
   SELECT * FROM yourtable.dates
</select>

Or if you are using Java Annotations:

@Select("SELECT * FROM yourtable.dates")
@Results(value = {
@Result(property = "day", column = "daycolumn"),
@Result(property = "month", column = "monthcolumn"),
@Result(property = "year", column = "yearcolumn")})     
public Date getDate();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM