线程" main"中的例外情况org.apache.ibatis.exceptions.IbatisException

时间:2016-09-05 01:45:42

标签: java mybatis

这是我第一次在Mybatis工作。我正在尝试创建一个连接但是会出现以下错误。

Exception in thread "main" org.apache.ibatis.exceptions.IbatisException:    
    ### Error building SqlSession.
    ### The error may exist in SQL Mapper Configuration
    ### Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance.  Cause: org.xml.sax.SAXParseException; lineNumber: 3; columnNumber: 16; Document root element "configuration", must match DOCTYPE root "null".
        at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:8)
        at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:32)
        at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:15)
        at Database_and_tables.mybatisInsert.main(mybatisInsert.java:19)
    Caused by: org.apache.ibatis.builder.BuilderException: Error creating document instance.  Cause: org.xml.sax.SAXParseException; lineNumber: 3; columnNumber: 16; Document root element "configuration", must match DOCTYPE root "null".
        at org.apache.ibatis.parsing.XPathParser.createDocument(XPathParser.java:136)
        at org.apache.ibatis.parsing.XPathParser.<init>(XPathParser.java:32)
        at org.apache.ibatis.builder.xml.XMLConfigBuilder.<init>(XMLConfigBuilder.java:47)
        at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:28)
        ... 2 more
    Caused by: org.xml.sax.SAXParseException; lineNumber: 3; columnNumber: 16; Document root element "configuration", must match DOCTYPE root "null".
        at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)
        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:437)
        at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:368)
        at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:325)
        at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.rootElementSpecified(XMLDTDValidator.java:1599)
        at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.handleStartElement(XMLDTDValidator.java:1877)
        at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.startElement(XMLDTDValidator.java:742)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(XMLDocumentFragmentScannerImpl.java:1363)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$ContentDriver.scanRootElementHook(XMLDocumentScannerImpl.java:1292)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3138)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:880)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
        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:848)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
        at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
        at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:243)
        at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:348)
        at `enter code here`org.apache.ibatis.parsing.XPathParser.createDocument(XPathParser.java:134)
        ... 5 more

mybatisSelect.java

    package Database_and_tables;

    import java.io.File;
    import java.io.IOException;
    import java.io.Reader;
    import java.util.List;

    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;

    public class mybatisSelect { 

       public static void main(String args[]) throws IOException{

          Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");

          SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);       
          SqlSession session = sqlSessionFactory.openSession();


          List<Borrower> borrower = session.selectList("Borrower.getAll");

          for(Borrower st : borrower ){       
             System.out.println(st.getCUSTOMER_NAME());
             System.out.println(st.getLOAN_NUMBER());

          }  

          System.out.println(" successfully");
          session.commit();
          session.close();
       }   
    }

Borrower.xml

<?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">
<mapper namespace = "resources/Borrower">   
   <resultMap id = "result" type = "Borrower">
      <result property = "CUSTOMER_NAME"/>  
      <result property = "LOAN_NUMBER" />    
   </resultMap>

   <select id = "getAll" resultMap = "result">
      SELECT * FROM Borrower; 
   </select>

   <select id = "getById" parameterType = "int" resultMap = "result">
      SELECT * FROM Borrower WHERE LOAN_NUMBER = !#{LOAN_NUMBER};
   </select>
</mapper>

Borrower.Java

package Database_and_tables;

public class Borrower {
           private String CUSTOMER_NAME;
           private String LOAN_NUMBER;

        /*   public Borrower( String CUSTOMER_NAME, String LOAN_NUMBER) {
                  super();
                  this.CUSTOMER_NAME = CUSTOMER_NAME;
                  this.LOAN_NUMBER = LOAN_NUMBER;

               }*/

           //getters setters
        public String getCUSTOMER_NAME() {
            return CUSTOMER_NAME;
        }
        public void setCUSTOMER_NAME(String cUSTOMER_NAME) {
            CUSTOMER_NAME = cUSTOMER_NAME;
        }
        public String getLOAN_NUMBER() {
            return LOAN_NUMBER;
        }
        public void setLOAN_NUMBER(String lOAN_NUMBER) {
            LOAN_NUMBER = lOAN_NUMBER;
        }
    [enter image description here][1]

SqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
   <environments default = "development">
      <environment id = "development">
         <transactionManager type = "JDBC"/> 

         <dataSource type = "POOLED">
            <property name = "driver" value = "oracle.jdbc.driver.OracleDriver"/>
            <property name = "url" value = "jdbc:oracle:thin:@localhost:1522:pavan2531"/>
            <property name = "username" value = "c##pavan"/>
            <property name = "password" value = "pavan"/>
         </dataSource>   

      </environment>
   </environments>

   <mappers>
      <mapper resource = "Borrower.xml"/>
   </mappers>

</configuration>

截图

enter image description here

有人可以帮我解决这个错误吗?

1 个答案:

答案 0 :(得分:-1)

您的XML映射文件中有错误;它告诉你错误所在的行号,列号和XML元素。

发布您的XML配置文件以帮助您(也可以在将来搜索此问题时参考)。

<强>更新

您是否可以包含此DTD声明并查看会发生什么?

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

另外,请确保您没有任何隐藏字符。