mybatis-3 icon indicating copy to clipboard operation
mybatis-3 copied to clipboard

Add `@StatementId` to support overloaded mapper methods

Open harawata opened this issue 5 years ago • 3 comments

This PR introduces a new annotation @StatementId to let users assign arbitrary statement ID [1].

@StatementId("selectAll")
@Select("select * from users")
List<User> select();

@StatementId("selectById")
User select(Integer id);

When the value is omitted, MyBatis will calculate the ID from the method signature. e.g.

@StatementId // -> select#User
User select(User user);

@StatementId // -> select#String[]_Date
User select(String[] names, Date dateOfBirth);

There is no documentation yet. I haven't decided if this should be merged or not and there could be changes in the spec. Should fix #1482 . Related to #1312 .

Any feedback is appreciated! 🙏

[1] I plan to apply more strict validation to the user-specified ID. e.g. valid characters are alphanumeric, underscore, brackets [] and pound sign #.

harawata avatar Apr 13 '19 16:04 harawata

This is a great idea, the error message like 'invlid bound statement(not found):' will not occur often.

heshengbang avatar Jul 03 '19 03:07 heshengbang

Coverage Status

Coverage increased (+0.03%) to 87.276% when pulling 0fae53f6e0f8035f8adc157d20060baf784ed8e8 on harawata:gh/1482-overloaded-methods into 717bba5d1494d0c044dd397107b29f994dc7aa2b on mybatis:master.

coveralls avatar May 03 '21 18:05 coveralls

+1. Having explicit assigned id will bring more consistence between XML and annotation based configuration, avoiding the shortcoming on usage of convention. By the way I assume even without @StatementId it is still a good idea to have the framework still generate the internal statementId according the method name plus signature when there is overloading happens, and notify the programmer during framework startup, instead of raise error like today's framework. For backward compatible in a non-overloaded case, we can keep the id as simple as the method name.

grantsunny avatar Mar 19 '24 03:03 grantsunny