项目目录结构是
mybatis.xml代码:
<?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:3308/test"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="dao/mapper.xml"/>
<mapper resource="dao/ClassDaoMapper.xml"/>
</mappers>
</configuration>
mapper.xml中的代码:
`<?xml version="1.0" encoding="UTF-8" ?>
<select id="getStudent" resultMap="studentMap">
select * from student where classid = #{classid}
</select>
<!-- public Student getStudentAndClassByid(int id); -->
<resultMap type="vo.Student" id="studentAndClass">
<id column="stuid" property="stuid"/>
<result column="stuname" property="stuname"/>
<result column="stunum" property="stunum"/>
<result column="stuscore" property="stuscore"/>
<result column="id" property="classinfo.id"/>
<result column="classname" property="classinfo.className"/>
</resultMap>
<resultMap type="vo.Student" id="studentAndClassByAssociation">
<id column="stuid" property="stuid"/>
<result column="stuname" property="stuname"/>
<result column="stunum" property="stunum"/>
<result column="stuscore" property="stuscore"/>
<association property="classinfo" javaType="vo.Class">
<id column="id" property="id"/>
<result column="classname" property="className"/>
</association>
</resultMap>
<select id="getStudentAndClassByid" resultMap="studentAndClassByAssociation">
select * from student as s,class as c where s.classid=c.id and s.stuid=#{stuid}
</select>
<!-- public Student getStudentAndClassByidStep(int id); -->
<resultMap type="vo.Student" id="getStudentAndClassStep">
<id column="stuid" property="stuid"/>
<result column="stuname" property="stuname"/>
<result column="stunum" property="stunum"/>
<result column="stuscore" property="stuscore"/>
<association property="classinfo" select="dao.ClassDaoMapper.getClassByid"
column="classid">
</association>
</resultMap>
<select id="getStudentAndClassByidStep" resultMap="getStudentAndClassStep">
select * from student where stuid=#{stuid}
</select>
`
ClassDaoMapper.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="dao.ClassDao">
<!-- public Class getClassByid(int id); -->
<select id="getClassByid" resultType="vo.Class">
select * from class where id=#{id}
</select>
</mapper>
Student.java中的代码:
package vo;
public class Student {
private int stuid;
private String stuname;
private int stunum;
private double stuscore;
private Class classinfo;
public Class getClassinfo() {
return classinfo;
}
public void setClassinfo(Class classinfo) {
this.classinfo = classinfo;
}
public int getStuid() {
return stuid;
}
public void setStuid(int stuid) {
this.stuid = stuid;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public int getStunum() {
return stunum;
}
public void setStunum(int stunum) {
this.stunum = stunum;
}
public double getStuscore() {
return stuscore;
}
public void setStuscore(double stuscore) {
this.stuscore = stuscore;
}
@Override
public String toString() {
return "Student [stuid=" + stuid + ", stuname=" + stuname + ", stunum=" + stunum + ", stuscore=" + stuscore
+ ", classinfo=" + classinfo + "]";
}
}
Class.java中的代码:
package vo;
public class Class {
private int id;
private String className;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
@Override
public String toString() {
return "Class [id=" + id + ", className=" + className + "]";
}
}
StudentDao.java
package dao;
import java.util.Map;
import org.apache.ibatis.annotations.MapKey;
import vo.Student;
public interface StudentDao {
@MapKey("stuid")
Map<Integer,Student> getStudent(int classid);
public Student getStudentAndClassByid(int id);
public Student getStudentAndClassByidStep(int id);
}
ClassDao.java
package dao;
import vo.Class;
public interface ClassDao {
public Class getClassByid(int id);
}
main.java
package main;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import dao.StudentDao;
import vo.Student;
public class main {
public static void main(String[] args) {
String resource="config/mybatis.xml";
try {
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory =new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session=sqlSessionFactory.openSession();
StudentDao s=session.getMapper(StudentDao.class);
// Map<Integer, Student> map=s.getStudent(1);
Student ss=s.getStudentAndClassByidStep(1);
System.out.println(ss);
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
异常如下:
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for dao.ClassDaoMapper.getClassByid
### The error may exist in dao/mapper.xml
### The error may involve dao.StudentDao.getStudentAndClassByidStep
### The error occurred while handling results
### SQL: select * from student where stuid=?
### Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for dao.ClassDaoMapper.getClassByid
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 org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:83)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
at com.sun.proxy.$Proxy3.getStudentAndClassByidStep(Unknown Source)
at main.main.main(main.java:25)
Caused by: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for dao.ClassDaoMapper.getClassByid
at org.apache.ibatis.session.Configuration$StrictMap.get(Configuration.java:888)
at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:721)
at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:714)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.getNestedQueryMappingValue(DefaultResultSetHandler.java:755)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.getPropertyMappingValue(DefaultResultSetHandler.java:464)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.applyPropertyMappings(DefaultResultSetHandler.java:440)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.getRowValue(DefaultResultSetHandler.java:403)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValuesForSimpleResultMap(DefaultResultSetHandler.java:355)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValues(DefaultResultSetHandler.java:330)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSet(DefaultResultSetHandler.java:303)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSets(DefaultResultSetHandler.java:196)
at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:64)
at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:79)
at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:63)
at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:326)
at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:83)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:148)
... 6 more
求大佬帮忙解答一下,谢谢