Servlet Error:

HTTP Status 500 - Request processing failed; nested exception is org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): by.gsu.epamlab.mapper.IUserMapper.getUser

exception org.springframework.web.util.NestedServletException: Request processing failed;

nested exception is org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): by.gsu.epamlab.mapper.IUserMapper.getUser ...

root cause org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): by.gsu.epamlab.mapper.IUserMapper.getUser

Servlet:

@Controller public class MyFirstController { @Autowired private IUserMapper userMapper; @RequestMapping(value = "/start", method = RequestMethod.GET) public ModelAndView execute(Model model) { model.addAttribute("user", userMapper.getUser(1));//ОШИБКА ТУТ return new ModelAndView("start"); } } 

Interface mapper:

 public interface IUserMapper { User getUser(int userId); } 

IUserMapper.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="by.gsu.epamlab.mapper.IUserMapper"> <sql id="userColumns">user_id as userID, first_name as firstName, last_name as lastName, email_address as emailAddress, role_id as role, password as password</sql> <select id="getUser" parameterType="int" resultType="User"> select <include refid="userColumns"/> from user where user_id = 1 </select> </mapper> 

Bins in settings:

 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/views/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <mvc:resources mapping="/resources/**" location="/resources/"/> <context:annotation-config /> <context:component-scan base-package="by.gsu.epamlab.controller" /> <context:component-scan base-package="by.gsu.epamlab.service" /> <mvc:annotation-driven /> <context:property-placeholder location="/WEB-INF/MySQLconnectionDB.properties" /> <tx:annotation-driven transaction-manager="transactionManager" /> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${draft.db.driver}" /> <property name="url" value="${draft.db.url}" /> <property name="username" value="${draft.db.user}" /> <property name="password" value="${draft.db.password}" /> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="typeAliasesPackage" value="by.gsu.epamlab.entity" /> <property name="mapperLocations" value="classpath*:by/gsu/epamlab/mapper/*.xml" /> </bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="by.gsu.epamlab.mapper" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean> 

Tell me, please, what am I doing wrong? Thank.

Project structure:

Project structure:

Addition to the project structure: extra to structure

  • Show the project structure. Where are the mappers and .xml for them? - mit
  • IUserMapper.xml lies in the resources? - mit
  • <property name = "mapperLocations" value = "classpath *: by / gsu / epamlab / mapper / *. xml" /> The path to the mappers is registered here. Added project structure - Daria

1 answer 1

IUserMapper.xml and all other *.xml should be located in resources . The structure in it should be:

 -src -main -java -resources -by -gsu -epamlab -mapper -IUserMapper.xml -*.xml 

You have it, but I do not see what is in it, because in the screenshot it is not disclosed.

  • transferred to resources, the same error, in this case mapperLocations differently prescribed? screen attached - Daria
  • mapperLocations spelled correctly. Try clean and rebuild. And add a <context:component-scan base-package="by.gsu.epamlab.mapper" /> or simply one <context:component-scan base-package="by.gsu.epamlab" /> - mit
  • so far nothing helps, are there any other options? Can there be a mistake in the mapper itself? - Daria
  • all right now with you. How to create a folder in the resources? Just spelled everything with dots? These are subfolders like you have packages in java . Uncheck the Compact Empty Middle Packages checkbox in the Project Editor and make the folders nested. Updated the answer to make it clearer. - mit
  • Error and that's it, I will try to describe the logic, maybe I don’t understand: 1) I go to the localhost: 8080 / start page 2) thanks to the line <context: component-scan base-package = "by.gsu.epamlab" /> finds a method in the MyFirstController servlet that has @RequestMapping (value = "/ start", method = RequestMethod.GET) in the annotation and accordingly this method is executed 3) @Autowired private IUserMapper userMapper; 4) when calling userMapper.getUser (1), it finds xml using the SqlSessionFactoryBean and MapperScannerConfigurer bins - Daria