MyBatis文件解析报错
背景
新增一个MyBatis的Plugin,添加到配置文件中后,报错:
Caused by: org.xml.sax.SAXParseException; lineNumber: 99; columnNumber: 17; The content of element type “configuration” must match “(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?,objectWrapperFactory?,reflectorFactory?,plugins?,environments?,databaseIdProvider?,mappers?)”.
解决
在mybatis-config.xml配置文件配置节点时,按照如下的顺序配置,即可消除错误:
| 1 | <properties>...</properties> | 
分析
在mybatis-config.xml的声明中,定义了xml的格式:
| 1 | <?xml version="1.0" encoding="UTF-8"?> | 
在MyBatis源码的mybatis-3-config.dtd文件中,定义了mybatis-config.xml的文件结构,内容如下:
| 1 | ... | 
DTD:Document Type Definition
A DTD is a Document Type Definition. A DTD defines the structure and the legal elements and attributes of an XML document.
DTD规则如下,用正则来理解就好:
| 符号 | 用途 | 示例 | 说明 | ||||
|---|---|---|---|---|---|---|---|
| () | 用来给元素分组 | `(A | B | C | D),(E | F),D` | 分成三组 | 
| ` | ` | 在列出的对象中选择一个 | `(A | B)` | 表示A或者B必须出现,两者选一 | ||
| + | 出现1或N次 | (A+) | A必须出现,而且可以出现多个A | ||||
| * | 出现0或N次 | (A*) | A可以出现0到多次 | ||||
| ? | 可以不出现,最多出现一次 | (A?) | A可以不出现,可以出现1次 | ||||
| , | 必须按指定的顺序出现 | (A,B,C,D) | 表示ABCD必须出现,而且需要按照这个顺序出现 | 
结合规则与定义文件来看,则表示,configuration下的的元素都必须按照定义的顺序出现,可以不出现,最多只能出现一次。
所以,当新添加的plugins节点,如果添加在properties,settings,typeAliases,typeHandlers,objectFactory, objectWrapperFactory这些节点之前,则会报错。
