刚学习ibatis遇到java.lang.ExceptionInInitializerError,不能创建SqlMap实例,求高手指点
SqlMapConfigure.xml文件:
<?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">
<sqlMapConfig>
<!--定义了ibatis的事务管理器,有3种(JDBC,JTA,EXTERNAL) -->
<transactionManager type="JDBC">
<!--type属性制定了数据源的链接类型,也有3种类型(SIMPLE,DBCP,JNDI) -->
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="com.mysql.jdbc.Driver" />
<property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost:3306/client" />
<property name="JDBC.Username" value="root" />
<property name="JDBC.Password" value="123456" />
</dataSource>
</transactionManager>
<sqlMap resource="com/ibatis/bean/Child.xml" />
</sqlMapConfig>
Child.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
"http://www.iBATIS.com/dtd/sql-map-2.dtd">
<sqlMap>
<typeAlias alias="Child" type="com.ibatis.bean.Child"/>
<select id="selectAllChild" resultClass="Child">
select * from Child
</select>
<select id="selectChildById" parameterClass="java.lang.Integer" resultClass="Child">
select * from Child where id=#id#
</select>
<insert id="insertChild" parameterClass="Child">
insert into Child(id,name,age,sex) values(#id#,#name#,#age#,#sex#)
</insert>
<update id="updateChild" parameterClass="Child">
update Child set name=#name#,age=#age#,sex=#sex# where id=#id#
</update>
<delete id="deleteChildById" parameterClass="java.lang.Integer">
delele from Child where id=#id#
</delete>
<delete id="deleteChildByName" parameterClass="java.lang.String">
delete from Child where name=#name#
</delete>
</sqlMap>
ChildDao.java文件:
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;
import com.ibatis.bean.Child;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
public class ChildDao {
private static SqlMapClient sqlMapClient=null;
//初始化SqlMapClient对象
static{
try {
Reader reader = Resources.getResourceAsReader("SqlMapConfigure.xml");
sqlMapClient=SqlMapClientBuilder.buildSqlMapClient(reader);
} catch (IOException e) {
e.printStackTrace();
}
}
//查询所有数据
@SuppressWarnings("unchecked")
public List<Child> queryChildAll(){
List<Child> list=null;
try {
list = sqlMapClient.queryForList("selectAllChild",null);
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
//通过id查询相应数据
public Child queryChildById(Integer id){
Child child=null;
try {
child=(Child) sqlMapClient.queryForObject("selectChildById", id);
} catch (SQLException e) {
e.printStackTrace();
}
return child;
}
//插入数据
public void insertChild(Child child){
try {
Object obj=sqlMapClient.insert("insertChild", child);
System.out.println(obj);
} catch (SQLException e) {
e.printStackTrace();
}
}
//更新数据
public void updateChild(Child child){
try {
int count=sqlMapClient.update("updateChild", child);
System.out.println(count);
} catch (SQLException e) {
e.printStackTrace();
}
}
//通过id删除数据
public void deleteChild(Integer id){
try {
int count=sqlMapClient.delete("deleteChildById", id);
System.out.println(count);
} catch (SQLException e) {
e.printStackTrace();
}
}
//通过name删除数据
public void deleteChild(String name){
try {
int count=sqlMapClient.delete("deleteChildByName", name);
System.out.println(count);
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
//Child child=new Child();
ChildDao childDao=new ChildDao();
System.out.println(childDao.queryChildAll());
}
}
Child.java文件就不贴了,就四个属性:
private Integer id;
private String name;
private Integer age;
private String sex;
运行ChildDao.java文件报错:
java.lang.ExceptionInInitializerError
Caused by: com.ibatis.sqlmap.client.SqlMapException: There was an error while building the SqlMap instance.
--- The error occurred in the SQL Map Configuration file.
--- Cause: com.ibatis.sqlmap.client.SqlMapException: XML Parser Error. Cause: org.xml.sax.SAXException: Error: URI=null Line=5: Document root element "sqlMapConfig", must match DOCTYPE root "sqlMap".
Caused by: org.xml.sax.SAXException: Error: URI=null Line=5: Document root element "sqlMapConfig", must match DOCTYPE root "sqlMap".
Caused by: com.ibatis.sqlmap.client.SqlMapException: XML Parser Error. Cause: org.xml.sax.SAXException: Error: URI=null Line=5: Document root element "sqlMapConfig", must match DOCTYPE root "sqlMap".
Caused by: org.xml.sax.SAXException: Error: URI=null Line=5: Document root element "sqlMapConfig", must match DOCTYPE root "sqlMap".
at com.ibatis.sqlmap.engine.builder.xml.XmlSqlMapClientBuilder.buildSqlMap(XmlSqlMapClientBuilder.java:243)
at com.ibatis.sqlmap.client.SqlMapClientBuilder.buildSqlMapClient(SqlMapClientBuilder.java:61)
at com.ibatis.dao.ChildDao.<clinit>(ChildDao.java:19)
Caused by: com.ibatis.sqlmap.client.SqlMapException: XML Parser Error. Cause: org.xml.sax.SAXException: Error: URI=null Line=5: Document root element "sqlMapConfig", must match DOCTYPE root "sqlMap".
Caused by: org.xml.sax.SAXException: Error: URI=null Line=5: Document root element "sqlMapConfig", must match DOCTYPE root "sqlMap".
at com.ibatis.sqlmap.engine.builder.xml.XmlSqlMapClientBuilder.getDoc(XmlSqlMapClientBuilder.java:1211)
at com.ibatis.sqlmap.engine.builder.xml.XmlSqlMapClientBuilder.buildSqlMap(XmlSqlMapClientBuilder.java:235)
... 2 more
Caused by: org.xml.sax.SAXException: Error: URI=null Line=5: Document root element "sqlMapConfig", must match DOCTYPE root "sqlMap".
at com.ibatis.sqlmap.engine.builder.xml.XmlSqlMapClientBuilder$SimpleErrorHandler.error(XmlSqlMapClientBuilder.java:1359)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.rootElementSpecified(XMLDTDValidator.java:1621)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.handleStartElement(XMLDTDValidator.java:1900)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.startElement(XMLDTDValidator.java:764)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(XMLDocumentFragmentScannerImpl.java:1359)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$ContentDriver.scanRootElementHook(XMLDocumentScannerImpl.java:1316)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3095)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:921)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:807)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:225)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:283)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:124)
at com.ibatis.sqlmap.engine.builder.xml.XmlSqlMapClientBuilder.getDoc(XmlSqlMapClientBuilder.java:1208)
... 3 more
Exception in thread "main"
代码几乎全贴出来了,上面报错的大概说的是在SqlMapConfigure.xml里面,但是仔细看了一下,就那几句实在没发现错在哪儿?怎么错的?请哪位高手指点一下到底哪里写错了 ibatis配置文件创建SqlMap实例
[解决办法]
我用的是mybatis
[解决办法]
人品大爆发,谢了
[解决办法]
<?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">
<sqlMapConfig>
<!--定义了ibatis的事务管理器,有3种(JDBC,JTA,EXTERNAL) -->
<transactionManager type="JDBC">
<!--type属性制定了数据源的链接类型,也有3种类型(SIMPLE,DBCP,JNDI) -->
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="com.mysql.jdbc.Driver" />
<property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost:3306/client" />
<property name="JDBC.Username" value="root" />
<property name="JDBC.Password" value="123456" />
</dataSource>
</transactionManager>
<sqlMap resource="com/ibatis/bean/Child.xml" />
</sqlMapConfig>