1.用maven的web模板生成了项目,然后添加了依赖这里应该没有问题
4.0.0
com.demo
SeverDemo
war
1.0-SNAPSHOT
SeverDemo Maven Webapp
http://maven.apache.org
org.mybatis
mybatis
3.4.0
junit
junit
4.11
test
com.alibaba
fastjson
1.2.11
mysql
mysql-connector-java
6.0.2
SeverDemo
2.然后配置了mybatis-config这里应该也没啥问题
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;
}
}
1.在映射文件中他先给我来个这个
暂时先没有管
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
于是就点开源码看

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

没错,是id?
整个项目在此 http://git.oschina.net/slgxmh/SeverDemo-Learn
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
抓了一下源码 发现以下几个问题
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里???