mybatis spring application getting - org.apache.ibatis.binding.BindingException:无效的绑定语句(未找到):

时间:2015-08-22 18:22:09

标签: spring-mybatis

我知道这个问题已被问过几次,但没有一个答案对我有帮助。我再次问它。我读到当接口类/包名称与mapper xml的类/包不同时会发生此错误。我使用相同的类/包名称仍然出现此错误。

我正在使用spring-mybatis并获得此异常 org.apache.ibatis.binding.BindingException:无效的绑定语句(未找到):

以下是我的相关文件: -

1)EmployeMapper.java(接口)

  com.XXX.org.mapper
        public interface EmployeeMapper {
        public Employee getEmployeeFullDetails(String employeeId);
        }

2)com.XXX.org.mapper.EmployeeMapper.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="com.XXX.org.mapper.EmployeeMapper">
        <select id="getEmployeeFullDetails" parameterType="String" resultType="com.XXX.org.Domain.Employee">
         SELECT * from employee emp 
         where emp.employeeId = #{employeeId}
        </select>
</mapper>

第3)的applicationContext.xml

<context:annotation-config/>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" >
    <property name="driverClassName" value="${dataSource.driverClassName}" />
    <property name="username" value="${dataSource.username}" />
    <property name="password" value="${dataSource.password}" />
    <property name="url" value="${dataSource.url}" />
</bean>

<context:component-scan base-package="com.XXX.org"/>

<context:annotation-config/>

<tx:annotation-driven />


<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
    <property name="dataSource" ref="dataSource" />
    <property name="typeAliasesPackage" value="com.XXX.org.domain" />
</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
   <property name="basePackage" value="com.XXX.org.mapper" />
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>

4)DBUnit测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml", "classpath:service-bean.xml"})
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class, DbUnitTestExecutionListener.class})
public class EmployeeTest {

    @Autowired
    EmployeeMapper employeeMapper;


@Test
    @DatabaseSetup(value = {"/employee.xml"} , type= com.github.springtestdbunit.annotation.DatabaseOperation.CLEAN_INSERT)
    public void testInsertEmployee() {

      Employee employee=  employeeMapper.getEmployeeFullDetails("testUser");
    }

WEBINF/classes

我可以在WEBINF / classes中看到我的界面和xml映射器,但问题是尽管共享相同的包名,但是创建了2个具有相同名称的单独文件夹。我认为两者都应该在生成的类中的一个包中。

2 个答案:

答案 0 :(得分:2)

我遇到了同样的问题,并通过在pom.xml中添加此块来解决它。在中文博客http://www.lpnote.com/2016/03/04/mybatis-invalid-bound-statement-not-found/

中提出了解决方案
 <build>
    ...
    <resources>            
        <resource>
            <directory>src/main/java</directory>
            <excludes>
                <exclude>**/.svn/*</exclude>
            </excludes>
            <includes>
                <include>**/*.xml</include>
            </includes>
        </resource>
    </resources>
</build>

答案 1 :(得分:1)

在Spring启动应用程序中出现类似错误,结果我错过了application.properties文件中的 mybatis.mapperLocations 属性。

所以我在application.properties

中添加了以下行
mybatis.mapperLocations=classpath*:**/xml/*.xml