当前位置: 首页 > 面试题库 >

无法使用Mybatis运行SQL查询

马正初
2023-03-14
问题内容

我正在尝试使用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),但是它们应该在具有相同名称的程序包中,并且应该组合在同一文件夹中。战争建立过程,因为它们都应根据您的配置在类路径中可用。



 类似资料:
  • 问题内容: 所涉及的rdms是oracle错误是 我想用这个查询做的是: 阈值表包含列阈值类型,其中包含现金交易表的列名称 对于阈值表中的每个记录,我们需要根据现金交易表中的阈值类型来比较总和(金额)组。 并将获取的数量与阈值表的threshold_amount比较 我需要选择threshold_id 阈值表: 现金交易表: 期望的输出: 让我们进行第一个提取:阈值表中的第一个记录 1.现在thr

  • 本文向大家介绍Mybatis传递多个参数进行SQL查询的用法,包括了Mybatis传递多个参数进行SQL查询的用法的使用技巧和注意事项,需要的朋友参考一下 PS:ibatis3如何传递多个参数有两个方法:一种是使用java.Map,另一种是使用JavaBean。 当只向xxxMapper.xml文件中传递一个参数时,可以简单的用“_parameter”来接收xxxMapper.java传递进来的参

  • 以下是Liquibase输入文件: --更改集dank:1 runalways=true failonerror:false 删除表aaa_schema.foo IBM错误代码-104是关于语法问题的。根据查看错误消息,我猜测它与行尾字符“;”有关。但是我已经尝试了使用分号和不使用分号的查询。IBM自己的db2也接受分号,因此它似乎是一个有效的选择。 非常感谢在找出此错误原因方面的任何帮助。

  • 注意我使用Mybatis

  • 本文向大家介绍mybatis 查询sql中in条件用法详解(foreach),包括了mybatis 查询sql中in条件用法详解(foreach)的使用技巧和注意事项,需要的朋友参考一下 foreach属性主要有item,index,collection,open,separator,close 1、item表示集合中每一个元素进行迭代时的别名, 2、index指定一个名字,用于表示在迭代过程中,

  • 我有一个用TestNG运行的简单代码,但是我不能用Gradle运行,因为它说没有找到主方法,这并不奇怪,因为我使用注释。 但在这种情况下,如果必须使用Gradle,如何运行代码。 请注意,我对Gradle很陌生,对这方面的知识不多。 代码: 上面的代码与TestNG库完美运行。然而不是格拉德尔。 以下是我的Gradle构建设置: Gradle返回没有Main方法。 谢谢你的帮助。