2014-11-04 72 views
0

請問此設置有什麼明顯的錯誤。在應用程序啓動時,我必須聲明屬性「名稱空間」的元素類型「映射器」,當它明確聲明。myBatis xml映射導致命名空間必須聲明元素類型映射器

彈簧xml資源dao.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:util="http://www.springframework.org/schema/util" 
xmlns:jms="http://www.springframework.org/schema/jms" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
        http://www.springframework.org/schema/util 
        http://www.springframework.org/schema/util/spring-util-3.2.xsd 
        http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-2.5.xsd"> 

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> 
    <property name="jndiName" value="java:comp/env/JNDINAME"/> 
    <property name="lookupOnStartup" value="true"/> 
    <property name="cache" value="true"/> 
    <property name="proxyInterface" value="javax.sql.DataSource"/>  
</bean> 

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
</bean> 

<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 
    <property name="basePackage" value="com.mycompany.dao"/> 
    <property name="sqlSessionFactory" ref="sqlSessionFactory"/> 
</bean> 
</beans> 

接口:

public interface MyDAO {  
    public void save(@Param("id") String id); 
} 

映射器的xml:

<?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> 

<mapper namespace="com.mycompany.dao"> 

<typeAlias alias="xxxVO" type="com.xxx" /> 

<insert id="save" parameterType="string"> 
    SOME SQL etc 
</insert> 

</mapper> 

</configuration> 

接口處於src/main/java/com/mycompany/dao 映射器XML在src/main/resources/com/mycompany/dao

我POM具有

<dependency> 
     <groupId>org.mybatis</groupId> 
     <artifactId>mybatis-spring</artifactId> 
     <version>1.0.1</version> 
    </dependency> 

實際的錯誤:事先

Caused by: org.xml.sax.SAXParseException: Attribute "namespace" must be declared for element type "mapper". 
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195) 
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131) 
    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.addDTDDefaultAttrsAndValidate(XMLDTDValidator.java:1275) 
    at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.handleStartElement(XMLDTDValidator.java:1940) 
    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:1363) 
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2756) 
    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:511) 
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808) 
    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:119) 
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:235) 
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:284) 
    at org.apache.ibatis.parsing.XPathParser.createDocument(XPathParser.java:236) 

感謝

回答

4

是從哪裏複製知道,但你混淆了這兩個mybatis-config.xmlmapper.xml

在mybatis-config.xml定義了框架配置,如緩存,定義類型,進口映射器(不定義映射器)

一個例子

<?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> 

    <settings> 
     <setting name="cacheEnabled" value="true" /> 
     <setting name="lazyLoadingEnabled" value="true" /> 
     <setting name="multipleResultSetsEnabled" value="true" /> 
     <setting name="useColumnLabel" value="true" /> 
     <setting name="useGeneratedKeys" value="false" /> 
     <setting name="defaultExecutorType" value="SIMPLE" /> 
     <setting name="defaultStatementTimeout" value="100" /> 
     <setting name="safeRowBoundsEnabled" value="false" /> 
     <setting name="mapUnderscoreToCamelCase" value="false" /> 
     <setting name="localCacheScope" value="SESSION" /> 
     <setting name="jdbcTypeForNull" value="OTHER" /> 
     <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString" /> 
    </settings> 

    <typeAliases> 
     <package name="com.kp.swasthik.domain" /> 
    </typeAliases> 

</configuration> 

你在另一個XML定義映射器(注意DTD差)

<?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.mycompany.dao.MyDao"> 

    <insert id="save" parameterType="string"> 
    SOME SQL etc 
    </insert> 

</mapper> 

而且在SessionFactory的您設置的MyBatis-配置。 xml和mapper文件的位置(在下面的例子中,我將所有的sql文件放在sqlpath目錄中的classpath中,或者你可以在mybatis-config.xml中使用映射器來定義,如here

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="configLocation" value="classpath:mybatis-config.xml"/> 
     <property name="mapperLocations" value="classpath*:sqlmap/*.xml" /> 
    </bean> 

而且在mapperscaner設置映射接口封裝

+0

感謝KARTHIK的路徑,這個固定我的,我花了幾個小時嘗試不同的解決方案的所有方式的諸多問題。 – 2014-11-04 17:52:28