场景:以学生和班级为例,一个学生只有一个班级,一个班级包含多个学生

说明:mapper 标签中我没有添加 namespace

  1. StudentMapper.xml
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper>
    <resultMap id="studentResultMap" type="com.demo.domain.Student">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="sex" column="sex"/>
        <result property="age" column="age"/>
 
        <!-- 关联映射:一个学生关联一个班级 -->
        <association property="clazz" column="clazz_id" javaType="com.demo.domain.Clazz" select="selectClazzWithId"/>
    </resultMap>
 
    <!-- 查询关联班级 -->
    <select id="selectClazzWithId" resultType="com.demo.domain.Clazz">
        SELECT * FROM TB_CLAZZ WHERE id = #{id}
    </select>
 
    <!-- 查询学生 -->
    <select id="selectStudent" resultMap="studentResultMap">
        SELECT * FROM TB_STUDENT
    </select>
</mapper>
  1. ClazzMapper.xml
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper>
    <resultMap id="clazzResultMap" type="com.demo.domain.Clazz">
        <id property="id" column="id"/>
        <result property="code" column="code"/>
 
        <!-- 关联映射:一个班级有多个学生,返回一个 List 集合 -->
        <collection property="students" javaType="ArrayList" column="id" ofType="com.demo.domain.Student" select="selectStudentWithId"/>
    </resultMap>
 
    <!-- 查询关联的学生 -->
    <select id="selectStudentWithId" resultType="com.demo.domain.Student">
        SELECT * FROM TB_STUDENT WHERE clazz_id = #{id}
    </select>
 
    <!-- 查询班级 -->
    <select id="selectClazz" resultMap="clazzResultMap">
        SELECT * FROM TB_CLAZZ
    </select>
</mapper>
更新于 阅读次数

请我喝[茶]~( ̄▽ ̄)~*

Jalen Chu 微信支付

微信支付

Jalen Chu 支付宝

支付宝

Jalen Chu 公众号

公众号