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.在映射文件中他先给我来个这个
暂时先没有管
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
大家讲道理2017-04-17 17:50:45
I grabbed the source code and found the following problems
1. The namespace for mapping xml files should be com.demo.mapper.StudentMapper
2. The directory for mapping xml files is best to be com/demo/mapper under resource, which is more in line with best practices , when you write the Mapper interface in the future, strange problems will not occur. When modifying the path, don't forget to change the path in mybatis-config.xml at the same time.
3. In the mapping xml file, the attribute databaseId="mysql" of the select statement should be removed.
You do not provide a databaseIdProvider in your mybatis-config.xml, so mybatis does not know the current databaseId, and your select statement limits it. databaseId, so the mapping statement cannot be found.
伊谢尔伦2017-04-17 17:50:45
There seems to be no problem with the configuration file you wrote? Isn't your resources folder on the classpath? ? ?