今天在写一条SQL的时候,需要用到mybatis动态SQL中的bind标签,但是,启动项目的时候却给我抛出了个异常,启动失败org.xml.sax.SAXParseException: Element type "bind" must be declared
这个异常的大致意思就是说我的mybatis中的mapper.xml写得有问题,其中有一个标签未定义,是这个bind标签,我感觉很奇怪,明明代码提示都能出来的一个标签,怎么会是没有定义的标签,然后又看了一下mybatis中的那个mapper.xml中定义的dtd,
1
2
3<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
是通过公网中的http://mybatis.org/dtd/mybatis-3-mapper.dtd这个dtd文件来进行校验的,然后我把这个文件下载下来
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292<?xml version="1.0" encoding="UTF-8" ?> <!-- Copyright 2009-2017 the original author or authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <!ELEMENT mapper (cache-ref | cache | resultMap* | parameterMap* | sql* | insert* | update* | delete* | select* )+> <!ATTLIST mapper namespace CDATA #IMPLIED > <!ELEMENT cache-ref EMPTY> <!ATTLIST cache-ref namespace CDATA #REQUIRED > <!ELEMENT cache (property*)> <!ATTLIST cache type CDATA #IMPLIED eviction CDATA #IMPLIED flushInterval CDATA #IMPLIED size CDATA #IMPLIED readOnly CDATA #IMPLIED blocking CDATA #IMPLIED > <!ELEMENT parameterMap (parameter+)?> <!ATTLIST parameterMap id CDATA #REQUIRED type CDATA #REQUIRED > <!ELEMENT parameter EMPTY> <!ATTLIST parameter property CDATA #REQUIRED javaType CDATA #IMPLIED jdbcType CDATA #IMPLIED mode (IN | OUT | INOUT) #IMPLIED resultMap CDATA #IMPLIED scale CDATA #IMPLIED typeHandler CDATA #IMPLIED > <!ELEMENT resultMap (constructor?,id*,result*,association*,collection*, discriminator?)> <!ATTLIST resultMap id CDATA #REQUIRED type CDATA #REQUIRED extends CDATA #IMPLIED autoMapping (true|false) #IMPLIED > <!ELEMENT constructor (idArg*,arg*)> <!ELEMENT id EMPTY> <!ATTLIST id property CDATA #IMPLIED javaType CDATA #IMPLIED column CDATA #IMPLIED jdbcType CDATA #IMPLIED typeHandler CDATA #IMPLIED > <!ELEMENT result EMPTY> <!ATTLIST result property CDATA #IMPLIED javaType CDATA #IMPLIED column CDATA #IMPLIED jdbcType CDATA #IMPLIED typeHandler CDATA #IMPLIED > <!ELEMENT idArg EMPTY> <!ATTLIST idArg javaType CDATA #IMPLIED column CDATA #IMPLIED jdbcType CDATA #IMPLIED typeHandler CDATA #IMPLIED select CDATA #IMPLIED resultMap CDATA #IMPLIED name CDATA #IMPLIED > <!ELEMENT arg EMPTY> <!ATTLIST arg javaType CDATA #IMPLIED column CDATA #IMPLIED jdbcType CDATA #IMPLIED typeHandler CDATA #IMPLIED select CDATA #IMPLIED resultMap CDATA #IMPLIED name CDATA #IMPLIED > <!ELEMENT collection (constructor?,id*,result*,association*,collection*, discriminator?)> <!ATTLIST collection property CDATA #REQUIRED column CDATA #IMPLIED javaType CDATA #IMPLIED ofType CDATA #IMPLIED jdbcType CDATA #IMPLIED select CDATA #IMPLIED resultMap CDATA #IMPLIED typeHandler CDATA #IMPLIED notNullColumn CDATA #IMPLIED columnPrefix CDATA #IMPLIED resultSet CDATA #IMPLIED foreignColumn CDATA #IMPLIED autoMapping (true|false) #IMPLIED fetchType (lazy|eager) #IMPLIED > <!ELEMENT association (constructor?,id*,result*,association*,collection*, discriminator?)> <!ATTLIST association property CDATA #REQUIRED column CDATA #IMPLIED javaType CDATA #IMPLIED jdbcType CDATA #IMPLIED select CDATA #IMPLIED resultMap CDATA #IMPLIED typeHandler CDATA #IMPLIED notNullColumn CDATA #IMPLIED columnPrefix CDATA #IMPLIED resultSet CDATA #IMPLIED foreignColumn CDATA #IMPLIED autoMapping (true|false) #IMPLIED fetchType (lazy|eager) #IMPLIED > <!ELEMENT discriminator (case+)> <!ATTLIST discriminator column CDATA #IMPLIED javaType CDATA #REQUIRED jdbcType CDATA #IMPLIED typeHandler CDATA #IMPLIED > <!ELEMENT case (constructor?,id*,result*,association*,collection*, discriminator?)> <!ATTLIST case value CDATA #REQUIRED resultMap CDATA #IMPLIED resultType CDATA #IMPLIED > <!ELEMENT property EMPTY> <!ATTLIST property name CDATA #REQUIRED value CDATA #REQUIRED > <!ELEMENT typeAlias EMPTY> <!ATTLIST typeAlias alias CDATA #REQUIRED type CDATA #REQUIRED > <!ELEMENT select (#PCDATA | include | trim | where | set | foreach | choose | if | bind)*> <!ATTLIST select id CDATA #REQUIRED parameterMap CDATA #IMPLIED parameterType CDATA #IMPLIED resultMap CDATA #IMPLIED resultType CDATA #IMPLIED resultSetType (FORWARD_ONLY | SCROLL_INSENSITIVE | SCROLL_SENSITIVE) #IMPLIED statementType (STATEMENT|PREPARED|CALLABLE) #IMPLIED fetchSize CDATA #IMPLIED timeout CDATA #IMPLIED flushCache (true|false) #IMPLIED useCache (true|false) #IMPLIED databaseId CDATA #IMPLIED lang CDATA #IMPLIED resultOrdered (true|false) #IMPLIED resultSets CDATA #IMPLIED > <!ELEMENT insert (#PCDATA | selectKey | include | trim | where | set | foreach | choose | if | bind)*> <!ATTLIST insert id CDATA #REQUIRED parameterMap CDATA #IMPLIED parameterType CDATA #IMPLIED timeout CDATA #IMPLIED flushCache (true|false) #IMPLIED statementType (STATEMENT|PREPARED|CALLABLE) #IMPLIED keyProperty CDATA #IMPLIED useGeneratedKeys (true|false) #IMPLIED keyColumn CDATA #IMPLIED databaseId CDATA #IMPLIED lang CDATA #IMPLIED > <!ELEMENT selectKey (#PCDATA | include | trim | where | set | foreach | choose | if | bind)*> <!ATTLIST selectKey resultType CDATA #IMPLIED statementType (STATEMENT|PREPARED|CALLABLE) #IMPLIED keyProperty CDATA #IMPLIED keyColumn CDATA #IMPLIED order (BEFORE|AFTER) #IMPLIED databaseId CDATA #IMPLIED > <!ELEMENT update (#PCDATA | selectKey | include | trim | where | set | foreach | choose | if | bind)*> <!ATTLIST update id CDATA #REQUIRED parameterMap CDATA #IMPLIED parameterType CDATA #IMPLIED timeout CDATA #IMPLIED flushCache (true|false) #IMPLIED statementType (STATEMENT|PREPARED|CALLABLE) #IMPLIED keyProperty CDATA #IMPLIED useGeneratedKeys (true|false) #IMPLIED keyColumn CDATA #IMPLIED databaseId CDATA #IMPLIED lang CDATA #IMPLIED > <!ELEMENT delete (#PCDATA | include | trim | where | set | foreach | choose | if | bind)*> <!ATTLIST delete id CDATA #REQUIRED parameterMap CDATA #IMPLIED parameterType CDATA #IMPLIED timeout CDATA #IMPLIED flushCache (true|false) #IMPLIED statementType (STATEMENT|PREPARED|CALLABLE) #IMPLIED databaseId CDATA #IMPLIED lang CDATA #IMPLIED > <!-- Dynamic --> <!ELEMENT include (property+)?> <!ATTLIST include refid CDATA #REQUIRED > <!ELEMENT bind EMPTY> <!ATTLIST bind name CDATA #REQUIRED value CDATA #REQUIRED > <!ELEMENT sql (#PCDATA | include | trim | where | set | foreach | choose | if | bind)*> <!ATTLIST sql id CDATA #REQUIRED lang CDATA #IMPLIED databaseId CDATA #IMPLIED > <!ELEMENT trim (#PCDATA | include | trim | where | set | foreach | choose | if | bind)*> <!ATTLIST trim prefix CDATA #IMPLIED prefixOverrides CDATA #IMPLIED suffix CDATA #IMPLIED suffixOverrides CDATA #IMPLIED > <!ELEMENT where (#PCDATA | include | trim | where | set | foreach | choose | if | bind)*> <!ELEMENT set (#PCDATA | include | trim | where | set | foreach | choose | if | bind)*> <!ELEMENT foreach (#PCDATA | include | trim | where | set | foreach | choose | if | bind)*> <!ATTLIST foreach collection CDATA #REQUIRED item CDATA #IMPLIED index CDATA #IMPLIED open CDATA #IMPLIED close CDATA #IMPLIED separator CDATA #IMPLIED > <!ELEMENT choose (when* , otherwise?)> <!ELEMENT when (#PCDATA | include | trim | where | set | foreach | choose | if | bind)*> <!ATTLIST when test CDATA #REQUIRED > <!ELEMENT otherwise (#PCDATA | include | trim | where | set | foreach | choose | if | bind)*> <!ELEMENT if (#PCDATA | include | trim | where | set | foreach | choose | if | bind)*> <!ATTLIST if test CDATA #REQUIRED >
发现里面明明是有定义了这个bind标签的,感觉有点邪门。
然后就一步一步找mybatis源码查看
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31private Document createDocument(InputSource inputSource) { // important: this must only be called AFTER common constructor try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(validation); factory.setNamespaceAware(false); factory.setIgnoringComments(true); factory.setIgnoringElementContentWhitespace(false); factory.setCoalescing(false); factory.setExpandEntityReferences(true); DocumentBuilder builder = factory.newDocumentBuilder(); builder.setEntityResolver(entityResolver); builder.setErrorHandler(new ErrorHandler() { public void error(SAXParseException exception) throws SAXException { throw exception; } public void fatalError(SAXParseException exception) throws SAXException { throw exception; } public void warning(SAXParseException exception) throws SAXException { } }); return builder.parse(inputSource); } catch (Exception e) { throw new BuilderException("Error creating document instance. Cause: " + e, e); } }
然后就找到这里,是这个地方抛出来的异常,然后在这个方法打了断点调试,看到
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setEntityResolver(entityResolver);
这两行代码,setEntityResolver()这个方法是设置校验的,然后我通过调试,找到这个方法中的参数对应的对象是org.apache.ibatis.builder.xml.XMLMapperEntityResolver这个类的一个对象,然后我打开这个类源码进行查看,发现这个类竟然是将xml对应的通过公网上的dtd文件进行校验改成了通过本地jar包中的dtd文件进行校验,感觉已经找到原因了,然后我再找到这个本地jar包中的dtd文件org/apache/ibatis/builder/xml/mybatis-3-mapper.dtd 果然,这个jar包中是没有定义bind标签的,现在就好办了,找到了问题原因是jar引起的,那么升级这个jar的版本就解决了。
这个就是之前用的jar
最后
以上就是机智小笼包最近收集整理的关于org.xml.sax.SAXParseException: Element type "bind" must be declared问题的全部内容,更多相关org.xml.sax.SAXParseException:内容请搜索靠谱客的其他文章。
发表评论 取消回复