解析Mapper XML时出错。原因:java.lang.NullPointerException

时间:2014-01-14 21:53:25

标签: java xml spring nullpointerexception mybatis

我一直在尝试从mybatis2转换为mybatis3,它给我带来了各种各样的问题。从堆栈跟踪我得到Error Parsing Mapper XML sqlMapConfig.xml我认为它意味着有一些错误,但我看不到它。我非常感谢任何帮助。

sqlMapConfig.xml     

<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
    <settings> 
        <setting name="cacheModelsEnabled" value="true" />
        <setting name="enhancementEnabled" value="true"/>
        <setting name="lazyLoadingEnabled" value="true"/> 
        <setting name="maxRequests" value="128"/> 
        <setting name="maxSessions" value="10"/>
        <setting name="maxTransactions" value="5"/> 
        <setting name="defaultStatementTimeout" value="0"/> 
        <setting name="statementCachingEnabled" value="true"/>
        <setting name="classInfoCacheEnabled" value="true"/> 
    </settings>

    <mappers>
        <mapper resource="com/fidelity/cmplnr/datasource/search_sql.xml"/>
    </mappers>


</configuration>

的beans.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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <context:annotation-config />

    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:jdbc.properties</value>
            </list>
        </property>
    </bean>

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="url" value="${jdbc.url}" />
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>

    <bean id="sqlMapClient" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="mapperLocations" value="classpath:sqlMapConfig.xml" />
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
    </bean>


    <bean id="ServiceImpl" class="com.fidelity.cmplnr.integration.ServiceImpl">
        <property name="sqlMapClient" ref="sqlMapClient" />
    </bean>

    <bean id="AuthenticationServiceImpl" class="com.fidelity.cmplnr.core.AuthenticationServiceImpl">
        <property name="sqlMapClient" ref="sqlMapClient" />
    </bean>


</beans>

search_sql.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="searchService">

    <resultMap id="getEmpScrCombinedMap" type="com.fidelity.cmplnr.core.EMP_SCR">
        <result property="name" column="PERSON_FULL_NAME" />
        <result property="Payroll_ID" column="ASSIGNMENT_NUMBER" />
        <result property="corp_id" column="CORP_ID" />
        <result property="Manager" column="SUPERVISOR_NAME" />
        <result property="cost_center" column="SCOPING_6" />
    </resultMap>
    <select id="getEmpScrCombined" resultMap="getEmpScrCombinedMap" parameterType="java.util.Map">
        SELECT person_full_name, assignment_number, corp_id, supervisor_name, scoping_6

        FROM $table_name$

        WHERE plan_id = $plan_id$

         <if test= "$table_name$ = SSO_CMPLNR_COMBINED">

            <dynamic prepend="AND">
                <isNotEqual prepend="AND" property="emp_lname" compareValue="0">

                    LOWER(last_name) like '$emp_lname$&#37;'

                </isNotEqual>

                <isNotEqual prepend="AND" property="emp_fname" compareValue="0">

                    LOWER(first_name) like '$emp_fname$&#37;'

                </isNotEqual>

                <isNotEqual prepend="AND" property="payId" compareValue="0">

                    LOWER(assignment_number) like '$payId$&#37;'

                </isNotEqual>

                <isNotEqual prepend="AND" property="corp_id" compareValue="0">

                    LOWER(corp_id) like '$corp_id$&#37;'

                </isNotEqual>

                <isNotEqual prepend="AND" property="manager_lname" compareValue="0">

                    LOWER(supervisor_name) like '$manager_lname$&#37;'

                </isNotEqual>

                <isNotEqual prepend="AND" property="manager_fname" compareValue="0">

                    LOWER(supervisor_name) like '%, $manager_fname$&#37;'

                </isNotEqual>

            </dynamic>

         </if>


    </select>

</mapper>

栈跟踪

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlMapClient' defined in class path resource [beans.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'class path resource [sqlMapConfig.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: java.lang.NullPointerException
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1412)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:546)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:872)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:423)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at com.fidelity.cmplnr.test.TestService.setUp(TestService.java:17)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.springframework.core.NestedIOException: Failed to parse mapping resource: 'class path resource [sqlMapConfig.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: java.lang.NullPointerException
    at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:466)
    at org.mybatis.spring.SqlSessionFactoryBean.afterPropertiesSet(SqlSessionFactoryBean.java:340)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1469)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1409)
    ... 34 more
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: java.lang.NullPointerException
    at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:117)
    at org.apache.ibatis.builder.xml.XMLMapperBuilder.parse(XMLMapperBuilder.java:89)
    at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:464)
    ... 37 more
Caused by: java.lang.NullPointerException
    at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:105)
    ... 39 more

1 个答案:

答案 0 :(得分:3)

在SQLMapClient属性中,mapperLocations期望mapper文件不是配置文件(提供给sessionfactory)

org.mybatis.spring.SqlSessionFactoryBean类调用试图查找映射器的parse方法

public void parse() {
    if (!configuration.isResourceLoaded(resource)) {
      configurationElement(parser.evalNode("/mapper"));
      configuration.addLoadedResource(resource);
      bindMapperForNamespace();
    }

如果所有mpper文件都放在类路径中的sqlmap文件夹中,您可以如下配置映射器。

<property name="mapperLocations" value="classpath:sqlmap/*.xml" />