我为什么放弃使用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。虽然麻烦一点,但起码会少一些奇葩问题出现。问题少也是提高效率的一个方面啊(伤心)。
我为什么放弃使用MyBatis3的Mapper注解的更多相关文章
- 我为什么放弃使用mybatis3的mapper注解了
原文链接 最近在使用MyBatis3做项目.在使用注解实现Mapper的时候遇到了比较奇葩的问题:在实现数据的batch insert的时候总是报错.好不容易可以正常插入了,但是又不能返回自增的主键i ...
- MyBatis中的@Mapper注解及配套注解使用详解(上)
前言: 从mybatis3.4.0开始加入了@Mapper注解,目的就是为了不再写mapper映射文件(那个xml写的是真的蛋疼...).很恶心的一个事实是源码中并没有对于这个注解的详细解释 现在我们 ...
- MyBatis中的@Mapper注解 @Mappe与@MapperScan关系
从mybatis3.4.0开始加入了@Mapper注解,目的就是为了不再写mapper映射文件 现在项目中的配置 public interface DemoMapper{ int deleteByPr ...
- @Mapper注解在springboot中无法注入
问题① @Mapper注解报红无法注入 方法 在pom文件中添加依赖
- 详解 @MapperScan 注解和 @Mapper 注解
实际上,这是一个非常简单的问题.我并没有一口回绝他,让他去百度.因为,新人都会经历这个过程.好不容易,问你一次,你直接让他百度,会打击到他的.而且,别人会觉得你摆架子. @Mapper 这个注解的定义 ...
- @Repository注解和@Mapper注解区别
@Reponsitory注解 @Reponsitory使用后,在启动类上需要添加@MapperScan("xxx.xxx.xxx.mapper")注解 @Mapper注解 @Map ...
- Mapper注解与MapperScan注解
1.Mapper注解 在接口类上添加@Mapper,在运行时动态代理生成实现类 @Mapper public interface UserDao { // User getUser(); } 如果想要 ...
- springboot整合mybatis完整示例, mapper注解方式和xml配置文件方式实现(我们要优雅地编程)
一.注解方式 pom <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId& ...
- idea dao使用@Mapper注解 业务类使用@Autowired 注入dao 爆红问题
实际项目跑起来无影响,但是看起来不太爽. 可以在dao类添加org.springframework.stereotype.Repository 注解 或者可以在service类中使用 javax.an ...
随机推荐
- Go-GRPC 初体验
grpc 跟常见的client-server模型相似(doubbo)grpc 编码之前需要准备以下环境: 安装protobuf,grpc的client与server之间消息传递使用的protoc格式消 ...
- pyspider爬取TripAdvisor
#!/usr/bin/env python # -*- encoding: utf-8 -*- # Created on 2017-06-11 10:10:53 # Project: london f ...
- AngularJS 全局scope与指令 scope通信
在项目开发时,全局scope 和 directive本地scope使用范围不够清晰,全局scope与directive本地scope通信掌握的不够透彻,这里对全局scope 和 directive本地 ...
- 算法题丨Next Permutation
描述 Implement next permutation, which rearranges numbers into the lexicographically next greater perm ...
- 两个css之间的切换
需求: 头部两个按钮 两种样式之间的切换 解决办法: 结合JQ 三目运算 来处理 第一步: 把需要切换的样式设置为样式里背景,这样做的目的为了避免 js里出现过多 css代码 二来这样会显得更加的清 ...
- Java基础知识回顾之四 ----- 集合List、Map和Set
前言 在上一篇中回顾了Java的三大特性:封装.继承和多态.本篇则来介绍下集合. 集合介绍 我们在进行Java程序开发的时候,除了最常用的基础数据类型和String对象外,也经常会用到集合相关类. 集 ...
- Java8-Collect收集Stream
Collection, Collections, collect, Collector, Collectos Collection是Java集合的祖先接口. Collections是java.util ...
- [JLOI2015]装备购买
题目描述 脸哥最近在玩一款神奇的游戏,这个游戏里有 n 件装备,每件装备有 m 个属性,用向量zi(aj ,.....,am) 表示 (1 <= i <= n; 1 <= j < ...
- HDU 5723 Abandoned country 最小生成树+搜索
Abandoned country Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- 2015 多校联赛 ——HDU5371(manacher + 枚举)
Sample Input 1 10 2 3 4 4 3 2 2 3 4 4 Sample Output Case #1: 9 要求找出一段数字. 将其分成3部分,第①和第②部分成回文字串,第②和第 ...