一 现象:
今天在写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文件,将变量前面和后面都用井号(#)包围。
时间: 2024-12-22 23:45:46