2015-06-29 4 views
0

Может ли кто-нибудь сказать мне, в чем дело? Я потратил более двух дней на отладку, но я все еще не могу. Есть ли что-то явно неправильное в этой настройке, пожалуйста. Я последовал за учебником в Интернете.mybatis: Атрибут «ресурс» должен быть объявлен для типа элемента «mapper»

Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: 
    ### Error building SqlSession. 
    ### The error may exist in com/test/ProductMapper.xml 
    ### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 7; columnNumber: 49; Attribute "resource" must be declared for element type "mapper". 
     at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:23) 
     at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:51) 
     at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:35) 
     at com.test.TestMybatis.delete(TestMybatis.java:44) 
     at com.test.Calltest.main(Calltest.java:18) 
    Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 7; columnNumber: 49; Attribute "resource" must be declared for element type "mapper". 
     at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:105) 
     at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:88) 
     at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:49) 
     ... 3 more 
    Caused by: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 7; columnNumber: 49; Attribute "resource" must be declared for element type "mapper". 
     at org.apache.ibatis.parsing.XPathParser.createDocument(XPathParser.java:253) 
     at org.apache.ibatis.parsing.XPathParser.<init>(XPathParser.java:122) 
     at org.apache.ibatis.builder.xml.XMLMapperBuilder.<init>(XMLMapperBuilder.java:74) 
     at org.apache.ibatis.builder.xml.XMLConfigBuilder.mapperElement(XMLConfigBuilder.java:310) 
     at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:103) 
     ... 5 more 
    Caused by: org.xml.sax.SAXParseException; lineNumber: 7; columnNumber: 49; Attribute "resource" must be declared for element type "mapper". 
     at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198) 
     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.addDTDDefaultAttrsAndValidate(XMLDTDValidator.java:1253) 
     at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.handleStartElement(XMLDTDValidator.java:1917) 
     at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.emptyElement(XMLDTDValidator.java:763) 
     at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(XMLDocumentFragmentScannerImpl.java:1342) 
     at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2770) 
     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:347) 
     at org.apache.ibatis.parsing.XPathParser.createDocument(XPathParser.java:251) 
     ... 9 more 

Mapper Продукт

<?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="com.test.ProductMapper"> 
<mapper resource="com/test/ProductMapper.xml"/> 
<typeAlias alias="Product" type="com.test.Product"/> 

    <insert id="insertProduct" parameterClass="Product"> 
     insert into product (brand,model,name) values (#{brand},#{model},#{name}); 
    </insert> 

    <delete id="deleteFilm" parameterClass="Product"> 
     delete from product where id = #{id} 
    </delete> 
</mapper> 

POJO Продукт

package com.test; 

public class Product { 

    private int id; 
    private String brand; 
    private String model; 
    private String name; 

    public int getId() { 
     return id; 
    } 
    public void setId(int id) { 
     this.id = id; 
    } 
    public String getBrand() { 
     return brand; 
    } 
    public void setBrand(String brand) { 
     this.brand = brand; 
    } 
    public String getModel() { 
     return model; 
    } 
    public void setModel(String model) { 
     this.model = model; 
    } 
    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 

} 

испытаний Класс

package com.test; 

import java.io.IOException; 
import java.io.Reader; 

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 TestMybatis { 

    /** 
    * @param args 
    */ 

    private static final String RESOURCE = "com/test/SqlMapConfig.xml"; 


    public static void test() throws IOException{ 
     Reader reader = Resources.getResourceAsReader(RESOURCE); 
     SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); 
     SqlSession session = null; 
     try{ 
      session = sessionFactory.openSession(); 
      //DAO mean Interface class 
      ProductMapper mapper=session.getMapper(ProductMapper.class); 
      //CRUD 
      Product p=new Product(); 
      p.setName("kee"); 
      mapper.insertProduct(p); 
      session.commit(); 

     }catch (Exception e) { 
      // TODO: handle exception 
      e.printStackTrace(); 
     } 


    } 

    public void delete() throws IOException{ 
     Reader reader = Resources.getResourceAsReader(RESOURCE); 
     SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); 
     SqlSession session = null; 
     try{ 
      session = sessionFactory.openSession(); 
      //DAO mean Interface class 
      ProductMapper mapper=session.getMapper(ProductMapper.class); 
      //CRUD 
      mapper.deleteProduct(1); 
      session.commit(); 

     }catch (Exception e) { 
      // TODO: handle exception 
      e.printStackTrace(); 
     } 


     } 
} 
+0

Никто? пожалуйста, мне нужно исправить это как можно скорее –

ответ

0

Ваш mapper элемент содержит другой mapper. Это не верно. Вы должны удалить строку <mapper resource="com/test/ProductMapper.xml"/>. Вероятно, он был скопирован из файла конфигурации MyBatis в файл конфигурации mapper.

См. Соответствующие разделы о configuration и mapper xml files в документации.

 Смежные вопросы

  • Нет связанных вопросов^_^