MyBatis Spring MVC error: invalid binding operator (not found)

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 use 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 Map 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 did some research, but none of the solutions 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

+5
source share
8 answers

Try checking the mybatis-conf.xml file (any name called by this file) and see if you have such an xper converter:


 <mappers> <mapper resource="BrandMapper.xml"> <mappers> 
+4
source

If you ran the db service code JUNIT, this would work. Therefore, I believe that this is a problem when a WAR file is generated, from your configuration * Mapper.java and * Mapper.xml points to the same location. Therefore, when the war is gerenated, the path to the folder for both java and xml files will be WEB-INF/classes/com/my/package/persistence . However, the * .xml file is not copied to the location. One way is to configure a script to copy the xml files or another (which I would prefer) to create the sqlmap directory in the resource directory and copy all the xml mapper files to the directory and specify the location of the mapperfile using sessionFactory.setMapperLocations and once a war file will be created, make sure that the sqlmap directory is present in the WEB-INF/classes

+1
source

You might need the @Alias annotation :

@Alias ​​("Brand") brand class {...}

This is the first thing that comes to mind, I hope this helps!

0
source

Remove ; from request:

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

Error message :

 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): 

Most likely due to an incorrect match. Query syntax . I had this problem many times and every time the error was caused by incorrect syntax of the request written in the Mapper xml files and the interface. I suggest you,

  • Recheck your request, which is the main cause of this error.
  • Configuration => It may also occur due to incorrect configuration of map files (interface and xml) in the configuration files.
  • Do maven clean, maven install (rebuild) and then restart the server
0
source

If you initialize sqlSessionFactory, you must specify the locations of the maps:

  @Autowired private ResourceLoader resourceLoader; @Bean public SqlSessionFactoryBean sqlSessionFactory() throws Exception { SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(dataSource()); //sessionFactory.setTypeAliasesPackage("com.my.package.domain"); sessionFactory.setMapperLocations(ResourcePatternUtils.getResourcePatternResolver(resourceLoader). getResources("classpath:path/to/mappers/*.xml")); return sessionFactory; } 

In your case, replace "classpath: path / to / mappers / *. Xml" with classpath: com / my / package / persistence / *. xml

Hope this helps you.

0
source

I had this exact error message, it turned out that this is the namespace path in the mapper XML file, I changed the package name in the java interface file, but forgot to update the package name in maper xml, an odd bit is one. Updated package namespace and hey presto!

0
source

Your mapper file must be in the same package as the * Repo.java file. Also check out the mapper namespace.

0
source

All Articles