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
J'ai récupéré le code source et trouvé les problèmes suivants
1. L'espace de noms pour mapper les fichiers XML doit être com.demo.mapper.StudentMapper
2. Le meilleur répertoire pour mapper les fichiers XML est com/demo/. sous Resource Mapper est plus conforme aux meilleures pratiques. Lorsque vous écrivez l'interface Mapper à l'avenir, aucun problème étrange ne se produira. Lors de la modification du chemin, n'oubliez pas de changer le chemin dans mybatis-config.xml en même temps.
3. Dans le fichier XML de mappage, l'attribut databaseId="mysql" de l'instruction select
doit être supprimé
Il n'y a pas de databaseIdProvider fourni dans votre mybatis-config.xml, donc mybatis ne le sait pas. l'ID de base de données actuel et votre instruction select limite l'ID de base de données, de sorte que l'instruction de mappage est introuvable.
Après avoir résolu localement les 3 problèmes ci-dessus, l'erreur a disparu.
Il ne semble y avoir aucun problème avec le fichier de configuration que vous avez écrit ? Votre dossier de ressources n'est-il pas sur le chemin de classe ? ? ?