我为什么放弃使用MyBatis3的Mapper注解

最近在使用MyBatis3做项目。在使用注解实现Mapper的时候遇到了比较奇葩的问题:在实现数据的batch insert的时候总是报错。好不容易可以正常插入了,但是又不能返回自增的主键id到实体bean中了。而这些问题在XML配置中都不存在。一方面可能是我能力有限,没有找到合适的解决方案;另一方面可能与MyBatis自身对Java中注解的支持不够给力有关系。

以下是MyBatis官网对Mapper Annotations的解释:

Mapper Annotations

Since the very beginning, MyBatis has been an XML driven framework. The configuration is XML based, and the Mapped Statements are defined in XML. With MyBatis 3, there are new options available. MyBatis 3 builds on top of a comprehensive and powerful Java based Configuration API. This Configuration API is the foundation for the XML based MyBatis configuration, as well as the new Annotation based configuration. Annotations offer a simple way to implement simple mapped statements without introducing a lot of overhead.

NOTE : Java Annotations are unfortunately limited in their expressiveness and flexibility. Despite a lot of time spent in investigation, design and trials, the most powerful MyBatis mappings simply cannot be built with Annotations – without getting ridiculous that is. C# Attributes (for example) do not suffer from these limitations, and thus MyBatis.NET will enjoy a much richer alternative to XML. That said, the Java Annotation based configuration is not without its benefits.

翻译:
(最初MyBatis是基于XML驱动的框架。MyBatis的配置是基于XML的,语句映射也是用XML定义的。对于MyBatis3,有了新的可选方案。MyBatis3 是建立在全面且强大的Java配置API之上的。 该配置API是MyBatis基于XML配置的基础,也是基于注解配置的基础。注解提供了简单的方式去实现简单的映射语句,不需要花费大量的开销。
注意:很不幸的是,java注解在表现和灵活性上存在限制。虽然在调研、设计和测试上花费了很多时间,但是最强大的MyBatis映射功能却无法用注解实现。这没有什么可笑的。举例来说,C#的特性就没有这个限制,所以MyBatis.NET 能拥有一个功能丰富的多的XML替代方案。所以,Java基于注解的配置是依赖于其自身特性的。)
http://www.mybatis.org/mybatis-3/java-api.html
所以,我最终决定不使用注解来实现Mapper了,换成XML。虽然麻烦一点,但起码会少一些奇葩问题出现。问题少也是提高效率的一个方面啊(伤心)。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值