1

I am trying to run a simple sql query using mybatis but its giving me following exception

java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.mycom.myproject.db.mybatis.dao.UserMapper.countByExample
org.apache.ibatis.session.Configuration$StrictMap.get(Configuration.java:660)
org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:495)
org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:488)
org.apache.ibatis.binding.MapperMethod.setupCommandType(MapperMethod.java:236)
org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:71)
org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:39)
$Proxy9.countByExample(Unknown Source)
com.mycom.myproject.controller.LoginController.login(LoginController.java:39)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:601)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

My UserMapper.xml is

<?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.mycom.myproject.db.mybatis.dao.UserMapper" >
<select id="countByExample" resultType="int">
select count(*) from users
</select>
</mapper>

My UserMapper is

public interface UserMapper {
   int countByExample();
}

I am trying to access it in my LoginController

public class LoginController
 {
static final Logger logger = Logger.getLogger(LoginController.class);

@Autowired
private UserMapper userMapper;



@RequestMapping("/login")
public ModelAndView login(@ModelAttribute User userBean, Model model){  

    int count = userMapper.countByExample();

    System.out.println("Count: "+ count);       

    return new ModelAndView("login", "userBean", userBean); 

}
    }

My spring-servlet.xml file is

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">



<context:annotation-config />   

<context:component-scan
    base-package="com.mycom.myproject" />

<!-- Enable annotation driven controllers, validation etc... -->
<mvc:annotation-driven />

<mvc:resources mapping="/resources/**" location="/resources/" />


<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost/mydatabase"/>
    <property name="username" value="root"/>
    <property name="password" value="root"/>
</bean>

<!-- Declare a transaction manager -->

<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="datasource" />


<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="datasource" />
</bean>

<!-- scan for mappers and will automatically scan the whole classpath for xmls -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    <property name="basePackage" value="com.mycom.myproject.db.mybatis.dao" />
</bean> 


<bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

I don't know why this error is coming.

Harry
  • 4,705
  • 17
  • 73
  • 101
  • 1
    It looks like myBatis doesnt find your query. Are UserMapper.java and UserMapper.xml in the same folder? If you are using Maven, be sure that UserMapper.xml is copied properly to the classpath. – jddsantaella Aug 06 '12 at 10:58
  • Hi jddsantaella, thanks for your reply. My UserMapper.xml is in com.mycom.myproject.db.mybatis.sqlmap package and my UserMapper.java class is in com.mycom.myproject.db.mybatis.dao package. So do you mean that UserMapper.xml and UserMapper.java should be in either dao pr sqlmap package? No I am not using Maven – Harry Aug 06 '12 at 11:13
  • Yep, both of them should be in com.mycom.myproject.db.mybatis.dao because is where you are saying to myBatis to scan. Let me know if it works and I will write an answer. – jddsantaella Aug 06 '12 at 11:21
  • Great Man. You again saved me. It works!!. Could you put this as answer so that I can mark it as accepted answer. – Harry Aug 06 '12 at 11:29
  • So now it looks like you have definitely your myBatis project running! Congratulations! – jddsantaella Aug 06 '12 at 11:37
  • 1
    Just a comment. You are calling UserMapper.java directly from the controller. I would put a service layer in the middle in order to keep the controller as simple as possible. Let say you need something from the database that requires to call few mappers (because you need to build some complex data object). Better to call one service that calls few mappers and do not call few mappers from the controller. – jddsantaella Aug 06 '12 at 11:51

1 Answers1

7

Taking a look to the error

Mapped Statements collection does not contain value for com.mycom.myproject.db.mybatis.dao.UserMapper.countByExample

it looks like myBatis is not finding your query. That is probably becuase it is not finding your xml mapped. It should be in com.mycom.myproject.db.mybatis.dao package according with your configuration:

<!-- scan for mappers and will automatically scan the whole classpath for xmls -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    <property name="basePackage" value="com.mycom.myproject.db.mybatis.dao" />
</bean>

In the project structure, UserMapper.java and UserMapper.xml can be in different folders (if you want to separate xml from java files) but they should be in a package with the same name and they should be combined in the same folder in the war build process because they both should be available in the classpath according with your configuration.

jddsantaella
  • 3,657
  • 1
  • 23
  • 39
  • Hi jddsantaella, I need your help again. Could you please look at the question posted at http://stackoverflow.com/questions/12103606/get-the-id-of-last-inserted-record-in-mybatis – Harry Aug 24 '12 at 08:38