user911
user911

Reputation: 1559

mybatis spring application getting - org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):

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.

Upvotes: 0

Views: 15661

Answers (3)

user911
user911

Reputation: 1559

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.

Upvotes: 3

kev
kev

Reputation: 1190

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

Upvotes: 1

Balnur Duisenbayeva
Balnur Duisenbayeva

Reputation: 31

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>

Upvotes: 3

Related Questions