0

I know this question has been asked several times but none of the answers helped me.Hence I am asking it again. I read that this error occurs when interface class/package name is different than mapper xml's class/package. I am using the same class/package name still getting this error.

I am using spring-mybatis and getting this exception org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):

Here are my associated files:-

1)EmployeMapper.java (Interface)

  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 test

@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

I can see both my interface and xml mapper in WEBINF/classes but the issue is that inspite of sharing the same package name , 2 separate folders are created with same name. I think both should be inside one package in generated classes.

2
  • can someone please help here. I am stuck since last 2 days. :( Commented Aug 23, 2015 at 18:40
  • I faced a similar exception but in a different scenario. When using Spring application with MyBatis and MapStruct. I had autowired an mapper object of Mapstruct. Somehow, Mybatis interfered in that autowired field. To resolve it, I had to use a static variable of the MapStruct's mapper instead of the autowired instance. Commented May 28, 2020 at 18:24

3 Answers 3

3

I had the same issue and solved it by adding this block in pom.xml. Solution was suggested in a chinese blog 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>
Sign up to request clarification or add additional context in comments.

Comments

3

I found solution to this problem. Basically there was some issue with the package for the mapper.xml in my resources folder (I use Intellij Idea). I think it was created as a folder instead of package. I just created a package again and it worked.

Make sure you create the package with: New > Directory and then type the directory with slashes (/), for example: com/example/mappers and NOT com.example.mappers.

I guess , earlier mapper.xml was undetected because it was a folder instead of a package.

2 Comments

what did you mean to create a package again? can u give more details?
@Bryant create folder naming com.XXX.org.mapper and naming com/XXX/org/mapper are not same.
1

Had similar error in a spring boot app, turns out i was missing the mybatis.mapperLocations property in the application.properties file.

So i added the following line in application.properties

mybatis.mapperLocations=classpath*:**/xml/*.xml

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.