MyBatis多表查询(2.2)

两表联查(多对一)

注:数据库表以及实体类见上一章节

1、创建StudentAndGradeDao接口中的方法

    //根据学生id查找学生信息以及对应的班级信息
    public Student findByStudentId(int sid);

2、配置StudentAndGradeDaoMapper.xml映射文件信息并将其配置到mybatis-config.xml

<resultMap id="a2" type="Student">
        <id property="sid" column="sid"></id>
        <result property="sname" column="sname"></result>
        <association property="grade" javaType="Grade">
            <id property="cid" column="cid"></id>
            <result property="cname" column="cname">
</result>
        </association>
</resultMap>

<select id="findByStudentId" resultMap="a2">
        select * from grade,student where student.cid=grade.cid and student.sid=#{sid} </select>

mybatis-config.xml获取对应的映射文件

<mapper resource="Mapper/StudentAndGradeDaoMapper.xml"></mapper>

3、添加测试类

import com.fan.dao.StudentAndGradeDao;
import com.fan.entity.Student;
import com.fan.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

public class Test3 {
    public static void main(String[] args) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        StudentAndGradeDao mapper = sqlSession.getMapper(StudentAndGradeDao.class);
        Student byStudentId = mapper.findByStudentId(1);
        System.out.println(byStudentId.getSname()+"是"+byStudentId.getGrade().getCname()+"学生");
    }
}

测试结果如下:

测试结果

总结(两表联查:一对多和多对一 )

格式:
   一对多:
   <resultMap type="" id="自定义名称">  
         <id property="java 属性名" column="列名" /><!--主键列-->  
         <result property="java 属性名" column="列名" />  
         <collection property="属性名" ofType="java 类型">  
             <id property="属性名" column="列名" />  
             <result property="属性名" column="列名" />  
         </collection>  
     </resultMap>  
例:
 <resultMap id="a1" type="Grade">
        <id property="cid" column="cid"></id>
        <result property="cname" column="cname"></result>
        <collection property="studentList" ofType="Student">
            <id property="sid" column="sid"></id>
            <result property="sname" column="sname"></result>
            <result property="cid" column="cid">
</result>
        </collection>
 </resultMap>
   多对一:
     <resultMap type="" id="">  
          <id property="" column="" />  
          <result property="" column="" />  
          <association property="" javaType="">  
              <id property="" column="" />  
              <result property="" column="" />  
          </association>  
      </resultMap>  
例:
<resultMap id="a2" type="Student">
        <id property="sid" column="sid"></id>
        <result property="sname" column="sname"></result>
        <association property="grade" javaType="Grade">
            <id property="cid" column="cid"></id>
            <result property="cname" column="cname"></result>
        </association>
</resultMap>

对结果进行映射时
     存的是集合的话使用Collection 子标签
     存的是一方的话使用association 子标签
     resultType 和resultMap 属性只能出现一个

JavaType 和 ofType 都是用来指定对象类型的,但是JavaType是用来指定pojo中属性的类型,而ofType指定的是映射到list集合属性中pojo的类型。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容