开发者

MyBatis mapping properties to database columns when inserting & updating (using annotations)

开发者 https://www.devze.com 2023-02-18 09:58 出处:网络
I\'m just starting to learn MyBatis and I\'m wondering, when I\'m creating insert or update queries, is there a way that I can make property names a bit more friendly to refactoring?I\'ll explain in m

I'm just starting to learn MyBatis and I'm wondering, when I'm creating insert or update queries, is there a way that I can make property names a bit more friendly to refactoring? I'll explain in more detail:

I have constants in all of my domain classes that can be used to reference property names. In my opinion, it cuts down on typos and makes refactoring a bit easier.

public static final String FIRST_NAME = "firstName";
private String firstName = 开发者_JAVA百科"";

When I create a MyBatis select statement using annotations, I can do something like this:

@Select("SELECT ID, FIRST_NAME, LAST_NAME FROM CUSTOMERS WHERE ID = #{id}")
@Results({
    @Result(property = CustomerDetail.ID, column = "ID"),
    @Result(property = CustomerDetail.FIRST_NAME, column = "FIRST_NAME"),
    @Result(property = CustomerDetail.LAST_NAME, column = "LAST_NAME")
})
CustomerDetail selectById(final int id);

If I refactor my domain object (CustomerDetail) and change property names, it ends up being fairly simple.

However, when I create a MyBatis insert statement using annotations, I have to do something like this:

@Insert("INSERT INTO CUSTOMERS (ID, FIRST_NAME, LAST_NAME) VALUES (#{id}, #{firstName}, #{lastName})")
void insertCustomerDetail(final CustomerDetail customerDetail);

In this case, if I refactor my domain object (CustomerDetail) and change property names, it's much more error prone. Is there a way I can use my constants without resorting to a bunch of string concatenation? Is it something I should even concern myself with?

As a total newbie, I was expecting the @Insert and @Update annotations to mimic the structure of the @Select annotation. For example (please note, the below are NOT valid MyBatis annotations):

@Insert("INSERT INTO CUSTOMERS (ID, FIRST_NAME, LAST_NAME)")
@Params({
    @Param(property = CustomerDetail.ID, column = "ID"),
    @Param(property = CustomerDetail.FIRST_NAME, column = "FIRST_NAME"),
    @Param(property = CustomerDetail.LAST_NAME, column = "LAST_NAME")
})
void insertCustomerDetail(final CustomerDetail customerDetail);

Have I missed any options that would have the same effect as my above sample? Alternatively, is it possible for me to unit test MyBatis mappings to ensure no one is using property names that don't exist in my domain objects? Testing may be a better solution since it would also apply to XML based mappings. Thoughts?


Is it something I should even concern myself with?

I don't think so. I understand your concern, and I see how such a feature could be beneficial to you, especially early in development when POJO's tend to change so often.

I don't think your objects fields will be refactored enough to justify the investment needed to provide this functionality.

I suggest you read about the @Param tag in the mybatis user guide. It's not exactly what your looking for here, but it is a way to decouple object field names to mybatis sql map variables. In my mind, I would take your approach over passing in individual feilds with a @Param.

In regards to unit testing your sql maps, i'm fairly certain that if you use an ognl expression that doesn't have a corresponding get method in the object you'll get an exceptions. i.e if you use #{someField}, and the object your passing in doesn't have a getSomeField() method, then you get exception.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号