MyBatis Spring MVC Error: Invalid bound statement (not found)

Jail picture Jail · May 6, 2015 · Viewed 41.8k times · Source

Here is the stack trace when I try to execute a simple query using MyBatis:

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.my.package.persistence.BrandMapper.getBrand
    org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:189)
    org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:43)
    org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:58)
    org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:51)
    com.sun.proxy.$Proxy25.getBrand(Unknown Source)
    com.my.package.service.BrandService.getBrand(BrandService.java:18)
    com.my.package.service.BrandService$$FastClassBySpringCGLIB$$1140c60a.invoke(<generated>)
    org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:649)
    com.my.package.service.BrandService$$EnhancerBySpringCGLIB$$ea6f89cd.getBrand(<generated>)
    com.my.package.controller.HomeController.getBrands(HomeController.java:28)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:483)
    org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:777)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:706)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

I'm using Javaconfig syntax instead of XML configuration. Here is my PersistenceConfig:

@Configuration
@EnableTransactionManagement
@MapperScan("com.my.package.persistence")
public class PersistenceConfig {

    @Bean
    public DataSource dataSource() {

        DriverManagerDataSource dataSource = new DriverManagerDataSource();

        try {
            dataSource.setDriverClassName("com.mysql.jdbc.Driver");
            dataSource.setUrl("jdbc:mysql//localhost:3306/db");
            dataSource.setUsername("dbuser");
            dataSource.setPassword("dbpassword");
        } catch (Exception e) {
            System.out.print(e);
        }
        return dataSource;
    }

    @Bean
    public DataSourceTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource());
    }

    @Bean
    public SqlSessionFactoryBean sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        sessionFactory.setTypeAliasesPackage("com.my.package.domain");
        return sessionFactory;
    }
}

Here is my controller:

@Controller
public class HomeController {

    private static Logger logger = LoggerFactory.getLogger(HomeController.class);

    @Autowired
    private BrandService brandService;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index() {
        return "index";
    }

    @RequestMapping(value = "/brands", method = RequestMethod.GET)
    public String getBrands(Model model) {
        model.addAttribute("brands",brandService.getBrand(1));
        return "brands";
    }

}

Here is my Brand Mapper Interface:

public interface BrandMapper {

    Brand getBrand(int id);

    Brand getBrandByName(String name);

    List<Brand> getBrandList();

    void addBrand(Brand brand);

    void updateBrand(Brand brand);

    void deleteBrand(int id);

}

Here is my BrandMapper 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.my.package.persistence.BrandMapper">

    <select id="getBrand" resultType="Brand" parameterType="int">
        SELECT id, name
        FROM brand
        WHERE id = #{id};
    </select>

    <select id="getBrandByName" resultType="Brand" parameterType="String">
        SELECT id, name
        FROM brand
        WHERE name = #{name};
    </select>

    <select id="getBrandList" resultType="Brand">
        SELECT id, name
        FROM brand;
    </select>

    <insert id="addBrand" parameterType="Brand">
        INSERT INTO brand (id, name)
        VALUE (#{id}, #{name})
    </insert>

    <update id="updateBrand" parameterType="Brand">
        UPDATE brand
        SET
        name = #{name}
        where id = #{id}
    </update>

    <delete id="deleteBrand" parameterType="int">
        DELETE FROM brand
        WHERE id = #{id}
    </delete>

</mapper>

I've done some research, but none of the solutions have worked for me. My XML Mapper files are under resources in a package named "com.my.package.persistence"

Does anyone have an idea what's wrong here?

Thanks in advance

Answer

Aldo Ernesto Arias Figueroa picture Aldo Ernesto Arias Figueroa · Jul 21, 2015

Try checking the mybatis-conf.xml (whatever name your called this file) file and see if you have your xml mapper like this:


<mappers> 
    <mapper resource="BrandMapper.xml">
<mappers>