mybatis神奇问题

slgxmh
  • 108

1.用maven的web模板生成了项目,然后添加了依赖这里应该没有问题

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.demo</groupId>
    <artifactId>SeverDemo</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>SeverDemo Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.11</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>6.0.2</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>SeverDemo</finalName>
    </build>
</project>

2.然后配置了mybatis-config这里应该也没啥问题

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatisdemo"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com.demo/mapper/StudentMapper.xml"></mapper>
    </mappers>
</configuration>

3.设置好实体类和映射文件好像也没啥问题

package com.demo.bean;

import java.util.Date;

/**
 * Created by 73196 on 2016/5/2.
 */
public class Student {
    private int id;
    private String name;
    private Date birthday;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
}
<?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.demo.bean.Student">
    <select id="selectStudent" resultType="com.demo.bean.Student" databaseId="mysql">
        SELECT * FROM student WHERE ID=#{id}
    </select>
</mapper>

接下来就是幺蛾子,大神先别看我的思路可能有错

1.在映射文件中他先给我来个这个
clipboard.png
暂时先没有管
2.运行,这错误

org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.demo.mapper.StudentMapper.selectStudent
### Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.demo.mapper.StudentMapper.selectStudent
    at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:150)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:77)
    at com.demo.APP.main(APP.java:22)
Caused by: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.demo.mapper.StudentMapper.selectStudent
    at org.apache.ibatis.session.Configuration$StrictMap.get(Configuration.java:853)
    at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:686)
    at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:679)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:147)
    ... 3 more

于是就点开源码看

clipboard.png
get参数得到了个null抛出错误,然后我有看是哪个参数?

clipboard.png
没错,是id?

这是什么鬼,id为空????????????????

整个项目在此 http://git.oschina.net/slgxmh/SeverDemo-Learn

回复
阅读 3.7k
2 个回答
✓ 已被采纳

抓了一下源码 发现以下几个问题
1.映射xml文件的namespace应该是com.demo.mapper.StudentMapper
2.映射xml文件的目录最好是resource下com/demo/mapper比较符合最佳实践,等你以后写Mapper的接口,就不会出奇怪的问题。修改路径时不要忘了同时改mybatis-config.xml里的路径。
3.在映射xml文件里select语句的属性 databaseId="mysql"
这个要去掉
你mybatis-config.xml里并没有提供databaseIdProvider,所以mybatis并不知道当前的databaseId,而你的select语句限定了databaseId,所以就找不到映射语句了。

在我本地修复了以上3个问题后 报错消失。

你这个配置文件写的貌似没有问题啊?难道你的resources文件夹没有放到classpath里???

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏