首页 > 代码库 > IBatis项目中com.ibatis.common.xml.NodeletException的解决方案
IBatis项目中com.ibatis.common.xml.NodeletException的解决方案
一 现象:
今天在写IBatis项目是总是提示我有如下异常:
com.ibatis.common.xml.NodeletException
java.util.NoSuchElementException
Exception in thread "main" java.lang.RuntimeException: Error occurred. Cause: com.ibatis.common.xml.NodeletException: Error parsing XML. Cause: java.lang.RuntimeException: Error parsing XPath ‘/sqlMapConfig/sqlMap‘. Cause: com.ibatis.common.xml.NodeletException: Error parsing XML. Cause: java.lang.RuntimeException: Error parsing XPath ‘/sqlMap/insert‘. Cause: java.util.NoSuchElementException at com.ibatis.sqlmap.engine.builder.xml.SqlMapConfigParser.parse(SqlMapConfigParser.java:89) at com.ibatis.sqlmap.client.SqlMapClientBuilder.buildSqlMapClient(SqlMapClientBuilder.java:63) at org.lyk.main.Hello.main(Hello.java:25) Caused by: com.ibatis.common.xml.NodeletException: Error parsing XML. Cause: java.lang.RuntimeException: Error parsing XPath ‘/sqlMapConfig/sqlMap‘. Cause: com.ibatis.common.xml.NodeletException: Error parsing XML. Cause: java.lang.RuntimeException: Error parsing XPath ‘/sqlMap/insert‘. Cause: java.util.NoSuchElementException at com.ibatis.common.xml.NodeletParser.parse(NodeletParser.java:53) at com.ibatis.sqlmap.engine.builder.xml.SqlMapConfigParser.parse(SqlMapConfigParser.java:86) ... 2 more Caused by: java.lang.RuntimeException: Error parsing XPath ‘/sqlMapConfig/sqlMap‘. Cause: com.ibatis.common.xml.NodeletException: Error parsing XML. Cause: java.lang.RuntimeException: Error parsing XPath ‘/sqlMap/insert‘. Cause: java.util.NoSuchElementException at com.ibatis.common.xml.NodeletParser.processNodelet(NodeletParser.java:123) at com.ibatis.common.xml.NodeletParser.process(NodeletParser.java:84) at com.ibatis.common.xml.NodeletParser.process(NodeletParser.java:102) at com.ibatis.common.xml.NodeletParser.parse(NodeletParser.java:72) at com.ibatis.common.xml.NodeletParser.parse(NodeletParser.java:51) ... 3 more Caused by: com.ibatis.common.xml.NodeletException: Error parsing XML. Cause: java.lang.RuntimeException: Error parsing XPath ‘/sqlMap/insert‘. Cause: java.util.NoSuchElementException at com.ibatis.common.xml.NodeletParser.parse(NodeletParser.java:53) at com.ibatis.sqlmap.engine.builder.xml.SqlMapParser.parse(SqlMapParser.java:51) at com.ibatis.sqlmap.engine.builder.xml.SqlMapConfigParser$11.process(SqlMapConfigParser.java:410) at com.ibatis.common.xml.NodeletParser.processNodelet(NodeletParser.java:121) ... 7 more Caused by: java.lang.RuntimeException: Error parsing XPath ‘/sqlMap/insert‘. Cause: java.util.NoSuchElementException at com.ibatis.common.xml.NodeletParser.processNodelet(NodeletParser.java:123) at com.ibatis.common.xml.NodeletParser.process(NodeletParser.java:84) at com.ibatis.common.xml.NodeletParser.process(NodeletParser.java:102) at com.ibatis.common.xml.NodeletParser.parse(NodeletParser.java:72) at com.ibatis.common.xml.NodeletParser.parse(NodeletParser.java:51) ... 10 more Caused by: java.util.NoSuchElementException at java.util.StringTokenizer.nextToken(StringTokenizer.java:349) at com.ibatis.sqlmap.engine.mapping.parameter.InlineParameterMapParser.parseInlineParameterMap(InlineParameterMapParser.java:55) at com.ibatis.sqlmap.engine.builder.xml.SqlStatementParser.parseDynamicTags(SqlStatementParser.java:240) at com.ibatis.sqlmap.engine.builder.xml.SqlStatementParser.processSqlStatement(SqlStatementParser.java:204) at com.ibatis.sqlmap.engine.builder.xml.SqlStatementParser.parseGeneralStatement(SqlStatementParser.java:120) at com.ibatis.sqlmap.engine.builder.xml.SqlMapParser$19.process(SqlMapParser.java:563) at com.ibatis.common.xml.NodeletParser.processNodelet(NodeletParser.java:121) ... 14 more
二 原因
该问题是由于我的mapping文件语法错误导致的:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd"> <sqlMap namespace="NewsNS"> <typeAlias alias="News" type="org.lyk.vo.News" /> <insert id="doCreate" parameterClass="News"> INSERT INTO news(title,pub_date,content) VALUES (#title,#pubdate,#content) <selectKey resultClass="Integer" keyProperty="nid"> SELECT LAST_INSERT_ID() </selectKey> </insert> </sqlMap>
注意mapping文件的红色部分,语法是错误的,正确的语法是在title、pubdate、content的前面和后面都要用井号包裹:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd"> <sqlMap namespace="NewsNS"> <typeAlias alias="News" type="org.lyk.vo.News" /> <insert id="doCreate" parameterClass="News"> INSERT INTO news(title,pub_date,content) VALUES (#title#,#pubdate#,#content#) <selectKey resultClass="Integer" keyProperty="nid"> SELECT LAST_INSERT_ID() </selectKey> </insert> </sqlMap>
三 解决方案
修改mapping文件,将变量前面和后面都用井号(#)包围。
IBatis项目中com.ibatis.common.xml.NodeletException的解决方案
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。