最近在使用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注解的更多相关文章

  1. 我为什么放弃使用mybatis3的mapper注解了

    原文链接 最近在使用MyBatis3做项目.在使用注解实现Mapper的时候遇到了比较奇葩的问题:在实现数据的batch insert的时候总是报错.好不容易可以正常插入了,但是又不能返回自增的主键i ...

  2. MyBatis中的@Mapper注解及配套注解使用详解(上)

    前言: 从mybatis3.4.0开始加入了@Mapper注解,目的就是为了不再写mapper映射文件(那个xml写的是真的蛋疼...).很恶心的一个事实是源码中并没有对于这个注解的详细解释 现在我们 ...

  3. MyBatis中的@Mapper注解 @Mappe与@MapperScan关系

    从mybatis3.4.0开始加入了@Mapper注解,目的就是为了不再写mapper映射文件 现在项目中的配置 public interface DemoMapper{ int deleteByPr ...

  4. @Mapper注解在springboot中无法注入

    问题① @Mapper注解报红无法注入 方法 在pom文件中添加依赖

  5. 详解 @MapperScan 注解和 @Mapper 注解

    实际上,这是一个非常简单的问题.我并没有一口回绝他,让他去百度.因为,新人都会经历这个过程.好不容易,问你一次,你直接让他百度,会打击到他的.而且,别人会觉得你摆架子. @Mapper 这个注解的定义 ...

  6. @Repository注解和@Mapper注解区别

    @Reponsitory注解 @Reponsitory使用后,在启动类上需要添加@MapperScan("xxx.xxx.xxx.mapper")注解 @Mapper注解 @Map ...

  7. Mapper注解与MapperScan注解

    1.Mapper注解 在接口类上添加@Mapper,在运行时动态代理生成实现类 @Mapper public interface UserDao { // User getUser(); } 如果想要 ...

  8. springboot整合mybatis完整示例, mapper注解方式和xml配置文件方式实现(我们要优雅地编程)

    一.注解方式 pom <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId& ...

  9. idea dao使用@Mapper注解 业务类使用@Autowired 注入dao 爆红问题

    实际项目跑起来无影响,但是看起来不太爽. 可以在dao类添加org.springframework.stereotype.Repository 注解 或者可以在service类中使用 javax.an ...

随机推荐

  1. Apache Spark Jobs 性能调优

    当你开始编写 Apache Spark 代码或者浏览公开的 API 的时候,你会遇到各种各样术语,比如transformation,action,RDD(resilient distributed d ...

  2. Python3 面向对象编程之程序设计思想发展

    概述 1940年以前:面向机器 1940年以前:面向机器 最早的程序设计都是采用机器语言来编写的,直接使用二进制码来表示机器能够识别和执行的指令和数 据.简单来说,就是直接编写 和 的序列来代表程序语 ...

  3. MSSQL 复制数据 并随机打乱写入

    select * into temp from XX order by newid() -- 复制表结构 truncate table XX -- 清空表 SET IDENTITY_INSERT XX ...

  4. java.lang.Class类

    第一次接触Class类是在学习 jdbc中.Class.forName()是Class类的一个静态方法,用于手动加载一个类,例如数据库驱动. 其实每一个java类都拥有或者说对应一个Class的实例对 ...

  5. HTML笔记05------AJAX

    AJAX初探01 AJAX概念 概念:即"Asynchronous JavaScript And XML" 通过在后台与服务器进行少量数据交换,AJAX可以使网页实现异步更新.这意 ...

  6. [LeetCode] Smallest Range 最小的范围

    You have k lists of sorted integers in ascending order. Find the smallest range that includes at lea ...

  7. [LeetCode] Find Duplicate File in System 在系统中寻找重复文件

    Given a list of directory info including directory path, and all the files with contents in this dir ...

  8. [Codeforces 933A]A Twisty Movement

    Description 题库链接 给你一个长度为 \(n\) 的只含有 \(1,2\) 的序列.你可以选择其中的一段 \([l,r]\) ,将区间翻转,翻转后使得单调不下降序列最长.求最长长度. \( ...

  9. [HAOI 2007]反素数ant

    Description 对于任何正整数x,其约数的个数记作g(x).例如g(1)=1.g(6)=4. 如果某个正整数x满足:g(x)>g(i) 0<i<x,则称x为反质数.例如,整数 ...

  10. 模板 AC自动机

    题目描述 有$N$ 个由小写字母组成的模式串以及一个文本串$T$ .每个模式串可能会在文本串中出现多次.你需要找出哪些模式串在文本串$T$ 中出现的次数最多. 输入输出格式 输入格式: 输入含多组数据 ...