无法使用Mybatis运行SQL查询

我正在尝试使用mybatis运行一个简单的sql查询,但是它给了我以下异常

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)

我的UserMapper.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.mycom.myproject.db.mybatis.dao.UserMapper" >

<select id="countByExample" resultType="int">

select count(*) from users

</select>

</mapper>

我的UserMapper是

public interface UserMapper {

int countByExample();

}

我试图在我的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);

}

}

我的spring-servlet.xml文件是

<?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>

我不知道为什么会出现此错误。

回答:

看看错误

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

看来myBatis找不到您的查询。那可能是因为找不到您的xml映射。它应该com.mycom.myproject.db.mybatis.dao

package符合您的配置:

<!-- 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>

在项目结构中,UserMapper.java和UserMapper.xml可以位于不同的文件夹中(如果要从XML文件中分离xml),但是它们应该在具有相同名称的程序包中,并且应该组合在同一文件夹中。战争建立过程,因为它们都应根据您的配置在类路径中可用。

以上是 无法使用Mybatis运行SQL查询 的全部内容, 来源链接: utcz.com/qa/398042.html

回到顶部