mybatis-3
mybatis-3 copied to clipboard
Add `@StatementId` to support overloaded mapper methods
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 #
.
This is a great idea, the error message like 'invlid bound statement(not found):' will not occur often.
Coverage increased (+0.03%) to 87.276% when pulling 0fae53f6e0f8035f8adc157d20060baf784ed8e8 on harawata:gh/1482-overloaded-methods into 717bba5d1494d0c044dd397107b29f994dc7aa2b on mybatis:master.
+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.