mybatis / mybatis-3

MyBatis SQL mapper framework for Java
http://mybatis.github.io/mybatis-3/
Apache License 2.0
19.75k stars 12.84k forks source link

Reusing JPA annotations to help results auto-mapping #717

Open Verdoso opened 8 years ago

Verdoso commented 8 years ago

MyBatis automapping capabilties as drescribed in the Auto-mapping section of the documentation are fair enough, and you can also customise the mappings columns <-> fields in the mappers using the @Result annotation and friends.

However, the first option means the fields and the column names have to match and the other means repeating the association for each SelectMap in your mapper. So I think it would be nice to have an in-between option that allows one to specify the mapping on the POJO itself, using annotations. Instead of coming out of the blue with new mybatis specific annotations, I thought it might help to reuse the existing ones: JPA.

For example, you would create a POJO like this one:

@Alias("MyPojo")
public class MyPojo {
    @Id
    @Column(name = "poj_id")
    private Integer id;
    @Column(name = "poj_name")
    private String name;
}

and in the mapper, something like this:

<select id="selectMyPojo" resultType="MyPojo">
  SELECT POJ_ID, POJ_NAME
  FROM T_POJO
</select>

And the automapping finds the right columns. I tested the concept and I already have something working simply by tweaking how org.apache.ibatis.reflection.Reflector creates the getters/setters lists, but I wanted to see first if you find the proposal interesting or I'm the only one that would use something like that.

Note that the idea is to add it as a customization option for th auto-mapping, when you would use that but the names do not match, not as a full blown alternative to mybatis annotations.

Thanks

Edit: I see the proposal is similar to Issue #157, but I propose reusing JPA annotations

harawata commented 8 years ago

Thank you for sharing your idea!

It still requires writing the column names in the query and it would not work with JOINs, so it is a little bit weak as a built-in feature, in my opinion. But there is a feature request about making the auto-mapping rule customizable : #332 If the feature is implemented, it should be possible to lookup JPA annotations when building your custom rule.

Also, abel533's Common Mapper can generate CRUD statements and result mappings based on JPA annotations, I think.

Regards, Iwao

Verdoso commented 8 years ago

Thanks for your comments, I understand you still need to write the queries, but skipping that part is a whole new can of worms and I just wanted something simple to save writing the ResultMap. It's might not seem much, but it's 50% of what I usually have to do. Regarding the JOINs do they not work with the automapper? The idea is to simply enhance the automapper so in most of the simple cases, you don't have to write the resultmap unless you need some extra customisation. If JPA annotations cannot carry all the information required, we might try to use the same ones that are used in the mappers. I don't have any special interest in reusing the JPA ones.

I'm afraid I cannot get much from the Common Mapper as I don't speak that language :).

Thanks

jhrabows commented 5 years ago

I like Verdoso's idea but I would prefer to stick to original JPA annotations like @Column rather than introducing @MappedColumn. In this way we would be able to automatically generate or validate the target schema using available JPA implementations and then access it using mybatis. This would guarantee the correctness of the column name at least in the 50% of use cases that Verdoso is talking about.

Doing this with 2 annotations is possible but less convenient. We would have to write something like

public static final String USER_NAME = "user_name";

@Column(name=USER_NAME) @MappedColumn(name=USER_NAME) private String userName;