mybatis一对一关联查询出错Could not find result map

1.问题:mybatis一对一关联查询报错 org.apache.ibatis.builder.IncompleteElementException: Could not find result map,但是无轮建表,到sql查询均可以查询到,请问哪里出问题了?

  • SQL建表如下:

# 创建teacher 与 class 表
CREATE TABLE IF NOT EXISTS teacher(
  t_id INT PRIMARY KEY AUTO_INCREMENT,
  t_name VARCHAR(20)
);

CREATE TABLE IF NOT EXISTS class(
  c_id INT PRIMARY KEY AUTO_INCREMENT,
  c_name VARCHAR(20),
  teacher_id INT
);
#建立一对一外键关系,这个需要学习了解
ALTER TABLE class ADD CONSTRAINT fk_teacher_id FOREIGN KEY (teacher_id) REFERENCES teacher(t_id);

#插入teacher数据
INSERT INTO teacher(t_name) VALUES('Mandy');
INSERT INTO teacher(t_name) VALUES('Chris');

#插入class数据
INSERT INTO class(c_name, teacher_id) VALUES('class_a', 1);
INSERT INTO class(c_name, teacher_id) VALUES('class_b', 2);


SELECT * FROM teacher;
SELECT * FROM class;
#级联查询表
select * from class c, teacher t where c.teacher_id=t.t_id and c.c_id=1;

java项目中maven配置:(pom.xml)

 <dependencies>
    <!--log4j日志文件-->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.16</version>
    </dependency>
    <!--mysql数据库连接-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.33</version>
    </dependency>
    <!--MyBatis框架-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>${mybatis.version}</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <!-- mybatis idea插件 -->
  <build>
    <finalName>mybatis</finalName>
    <plugins>
      <plugin>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-maven-plugin</artifactId>
        <version>1.3.2</version>
        <configuration>
          <verbose>true</verbose>
          <overwrite>true</overwrite>
        </configuration>
      </plugin>
    </plugins>
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.properties</include>
          <include>**/*.xml</include>
        </includes>
        <!-- 是否替换资源中的属性-->
        <filtering>false</filtering>
      </resource>
      <resource>
        <directory>src/main/resources</directory>
        <!--
            <includes> <include>**/*.properties</include>
            <include>**/*.xml</include> </includes> <filtering>true</filtering>
        -->
      </resource>
    </resources>
  </build>

mybatis配置文档:

<?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>
    <!--重点1:引入db.properties文件 -->
    <properties resource="db.properties" />
    <typeAliases>
        <typeAlias type="com.mybatis.oneToone.domain.Classes" alias="Classes" />
        <typeAlias type="com.mybatis.oneToone.domain.Teacher" alias="Teacher" />
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <!--配置mysql数据库信息-->
            <dataSource type="POOLED">
                <property name="driver" value="${driver}" />
                <property name="url" value="${url}" />
                <property name="username" value="${name}" />
                <property name="password" value="${password}" />
            </dataSource>
        </environment>
    </environments>

        <!-- 配置注册的userMapper-->
    <mappers>
        <mapper resource="com/mybatis/oneToone/mapping/ClassMapper.xml"/>
    </mappers>
</configuration>

两个domain类:

public class Classes {
    private int id;//id===>c_id
    private String name;//name===>c_name
    private Teacher teacher;

    public Classes(int id, String name, Teacher teacher) {
        this.id = id;
        this.name = name;
        this.teacher = teacher;
    }

    public Classes() {
    }

    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 Teacher getTeacher() {
        return teacher;
    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }

    @Override
    public String toString() {
        return "Classes {id="+id+" ,name="+name+" , teacher="+teacher+"}";
    }
}

public class Teacher {

    private int id; // id --> t_id
    private String name; // name --> t_name

    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;
    }

    @Override
    public String toString() {
        return "Teacher {id="+ id+" ,name="+ name +"}";
    }
}

ClassMapper.xml

<mapper namespace="com.mybatis.oneToone.mapping.ClassMapper">
<select id="getClass1" parameterType="int" resultMap="classResultMap">
        select *
        from class c,teacher t
        where c.teacher_id =t.t_id
        and c.c_id=#{id}
    </select>
    <!-- 使用resultMap映射实体类和字段之间的一一对应关系 -->
    <resultMap id="classResultMap" type="Classes">
        <id property="id" column="c_id" />
        <result property="name" column="c_name" />
        <!-- 一对一映射 使用的标签 -->
        <association property="teacher" column="teacher_id" javaType="Teacher">
            <id property="id" column="t_id" />
            <result property="name" column="t_name" />
        </association>
    </resultMap>
</mapper>    

运行代码时候:

public class App 
{
    public static void main( String[] args )
    {
       SqlSession session = MyBatisUtil.getSqlSession();
       String strId = "com.mybatis.oneToone.mapping.ClassMapper.getClass1";
       Classes clazz = session.selectOne(strId,1);
        System.out.println(clazz);
    }
}

显示如下错误:
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException:

Error querying database. Cause: org.apache.ibatis.builder.IncompleteElementException: Could not find result map com.mybatis.oneToone.domain.Teacher

Cause: org.apache.ibatis.builder.IncompleteElementException: Could not find result map com.mybatis.oneToone.domain.Teacher

at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:23)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:104)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:95)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:59)
at com.mybatis.oneToone.App.main(App.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

Caused by: org.apache.ibatis.builder.IncompleteElementException: Could not find result map com.mybatis.oneToone.domain.Teacher

at org.apache.ibatis.builder.MapperBuilderAssistant.setStatementResultMap(MapperBuilderAssistant.java:354)
at org.apache.ibatis.builder.MapperBuilderAssistant.addMappedStatement(MapperBuilderAssistant.java:292)
at org.apache.ibatis.builder.xml.XMLStatementBuilder.parseStatementNode(XMLStatementBuilder.java:113)
at org.apache.ibatis.session.Configuration.buildAllStatements(Configuration.java:579)
at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:505)
at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:500)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:100)
... 8 more

Caused by: java.lang.IllegalArgumentException: Result Maps collection does not contain value for com.mybatis.oneToone.domain.Teacher

at org.apache.ibatis.session.Configuration$StrictMap.get(Configuration.java:672)
at org.apache.ibatis.session.Configuration.getResultMap(Configuration.java:434)
at org.apache.ibatis.builder.MapperBuilderAssistant.setStatementResultMap(MapperBuilderAssistant.java:352)
... 14 more

希望好心人帮忙一下,我已经尽力排查我自己能够知道的错误,但是真的不米昂白为何还是报错。

阅读 20.2k
3 个回答

这个问题,我已经找到方法了,首先mybatis我现在用的版本是有一对一映射,使用association标签。这里需要这样修改:

    <select id="getClass1" parameterType="int" resultMap="classResultMap">
        select *
        from class c LEFT JOIN teacher t
        ON C.teacher_id = T.t_id
        where c.c_id=#{id}
    </select>
    <!-- 使用resultMap映射实体类和字段之间的一一对应关系 -->
    <resultMap id="classResultMap" type="Classes">
        <id property="id" column="c_id" />
        <result property="name" column="c_name" />
        <!-- 一对一映射 使用的标签 -->
        <association property="teacher" column="teacher_id"  javaType="com.mybatis.oneToone.domain.Teacher">
            <id property="id" column="t_id" />
            <result property="name" column="t_name" />
        </association>
    </resultMap>

    <select id="getClass2" parameterType="int" resultMap="classResultMap2" >
        SELECT * FROM  class WHERE c_id=#{id}
    </select>
    <!-- 使用resultMap映射实体类和字段之间的一一对应关系 -->
    <resultMap id="classResultMap2" type="Classes">
        <id property="id" column="c_id" />
        <result property="name" column="c_name" />
        <association property="teacher" column="teacher_id" select="getTeacher" />
    </resultMap>

    <select id="getTeacher" parameterType="int" resultType="Teacher">
        SELECT t_id id,t_name name FROM teacher WHERE t_id=#{id}
    </select>

没用过mybatis

不过可以这样:

遇到这种情况直接看最后一个caused by:

Caused by: java.lang.IllegalArgumentException: Result Maps collection does not contain value for com.mybatis.oneToone.domain.Teacher

说结果集中集合不包括Teacher成员,我记得mybatis是弱封装,是不支持单纯的1对1的,即时是1对1也是要用collection去接受,然后取第一个

也有可能是我太老了,呵呵

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