在xxxMapper中

<select id="getClazz" parameterType="int" resultType="getClazzMap">
        SELECT * FROM class c,teacher t WHERE c.tid = t.tid AND c.cid=#{id}
    </select>
    
    <resultMap type="Clazz" id="getClazzMap">
        <id property="id" column="cid"/>
        <result property="name" column="cname"/>
        <!-- 关联班级对应的teacher -->
        <association property="teacher" javaType="Teacher">
            <id property="id" column="tid"/>
            <result property="name" column="tname"/>
        </association>
    </resultMap>

查各种资料发现,用到resultType,必须在mybatis的配置文件中进行别名申明该resultType属于哪个实体类

<!-- 配置xxxMapper.xml中的实体类的别名 -->
    <typeAliases>
        <!-- 单个实体类配置别名 -->
        <typeAlias type="com.mlxs.mybatis.test1.User" alias="User"/>
        <typeAlias type="com.mlxs.mybatis.test1.Clazz" alias="getClassMap"/>
        <!-- 整个包配置,别名默认为类名 推荐 -->
        <package name="com.mlxs.mybatis.bean"/>
    </typeAliases>

但是,其实我这上面用错了,我真正要用的是resultMap,不是resultType

<select id="getClazz" parameterType="int" resultMap="getClazzMap">
        SELECT * FROM class c,teacher t WHERE c.tid = t.tid AND c.cid=#{id}
    </select>
    
    <resultMap type="Clazz" id="getClazzMap">
        <id property="id" column="cid"/>
        <result property="name" column="cname"/>
        <!-- 关联班级对应的teacher -->
        <association property="teacher" javaType="Teacher">
            <id property="id" column="tid"/>
            <result property="name" column="tname"/>
        </association>
    </resultMap>

这个在写的时候要非常注意... ...

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-04-26
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-29
  • 2021-12-29
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-17
  • 2022-12-23
  • 2022-01-14
  • 2021-12-09
相关资源
相似解决方案