BindingException: Invalid bound statement (not found) when configuring MyBatis with JavaConfig and XML mapper files in Spring

2,240 views
Skip to first unread message

masso...@gmail.com

unread,
Nov 18, 2015, 10:16:06 AM11/18/15
to mybatis-user
Hi,

I am experiencing the exact same issue described in an earlier post here: http://mybatis-user.963551.n3.nabble.com/MapperScan-with-XML-mappers-td4027565.html

However the proposed solution doesn't seem to work in my case. 

I created a sample spring mvc web app using the source code from Christopher's example in his github repo (https://github.com/Ludachrispeed/mybatis-spring-mwe) but am still experiencing the issue he described. My understanding is that the fix applied by Eduardo in that post had to do with adding a full path to the domain object in the XML mapper resultType.

Here's what I did:

EXAMPLE.APPCONFIG - MyBatisConfig.java (myBatis configuration file)

@Configuration
@MapperScan("example.main")
public class MyBatisConfig {

   @Bean
    public DataSource devDataSource() {
             //Datasource setup code for a H2 memory db
    }
    
    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(devDataSource());
        return sessionFactory.getObject();
    }

}

EXAMPLE.APPCONFIG - MyAppInitializer (WebApplication Initializer config)
@Configuration
public class MyAppInitializer implements WebApplicationInitializer{
    
    ... (inside of onStartup() )
    context.register(MyWebMvcConfig.class,MyBatisConfig.class);
   ...

}

There's also another class setting up a viewresolver in EXAMPLE.APPCONFIG

EXAMPLE.MAIN - PersonMapper.java (Mapper interface)
public interface PersonMapper {

    public Person getByFirstName(@Param("firstname") String firstname);
}

EXAMPLE.MAIN - PersonMapper.xml (Mapper XML)
<!DOCTYPE mapper 
<mapper namespace="example.main.PersonMapper">
<select id="getByFirstName" parameterType="string" resultType="example.main.Person">
select * from PEOPLE where PEOPLE.FIRSTNAME = #{firstname}
</select>
</mapper>

EXAMPLE.MAIN - Person.java (Model)
public class Person {

    private String firstname;
    private String lastname;

    public String getFirstname() {
        return firstname;
    }
    
    ...
}

I'm omitting the imports and other irrelevant details. I created a service, PersonService and autowired a PersonMapper to it. On the service I call the getByFirstName method of the mapper, passing a string and expecting a Person object in return.

The PersonService is autowired to a controller that calls the getByFirstName method of the service, which in its turn calls the same method on the mapper and so on.

The project builds without problems all of the dependencies appear to be correct etc. But the login page displays the error below when the controller is hit:

type Exception report

message Request processing failed; nested exception is org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): example.main.PersonMapper.getByFirstName

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): example.main.PersonMapper.getByFirstName
	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
	org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
	org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

root cause

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): example.main.PersonMapper.getByFirstName
	org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:184)


Any ideas? I scavenged the internet for similar scenarios but none of the suggested fixes appear to work. I placed controllers, services, models and mappers all in the same package to simplify things. The funny thing is, like Christopher, if I use the mybatis annotations in the java Mapper interface instead of the XML file then everything works fine. But once I remove the annotations and add the XML mapper I get the exception above.

I've uploaded my code to github https://github.com/mrmassoni/mybatis_javaconfig_xml_example in case anyone is interested in having a look. I'm not using the latest version of myBatis in this particular case but I've tried the latest and got the same error.

Thanks!

Max
Reply all
Reply to author
Forward
0 new messages