Mybatis二----关联查询(1)一对一关联查询

关联查询包括以下四种方式:

  1)一对一关联查询

  2)一对多关联查询

  3)多对一关联查询

  4)多对多关联查询

接下来了解下一对一关联查询

实体类:

package com.mybatisdemo.beans;

public class Clazz {

    private Integer id;
    private String className;
    private HeadTeacher headTeacher;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public HeadTeacher getHeadTeacher() {
        return headTeacher;
    }

    public void setHeadTeacher(HeadTeacher headTeacher) {
        this.headTeacher = headTeacher;
    }

    @Override
    public String toString() {
        return "Clazz [id=" + id + ", className=" + className + ", headTeacher=" + headTeacher + "]";
    }

}
package com.mybatisdemo.beans;

public class HeadTeacher {

    private Integer id;
    private String name;
    // private Clazz clazz;                //这边不引用,防止关联查询导致死循环
    
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

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

}

Dao层对应的映射类Mapper:

<?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="com.mybatisdemo.dao.ClazzDao">

    <resultMap type="com.mybatisdemo.beans.Clazz" id="clazzMapper">
        <id column="id" property="id"/>
        <result column="class_name" property="className"/>
        <!-- <association property="headTeacher" javaType="com.mybatisdemo.beans.HeadTeacher">
            <id column="id" property="id"/>
            <result column="name" property="name"/>
        </association> -->
        
        <association property="headTeacher" javaType="com.mybatisdemo.beans.HeadTeacher" 
                column="id" select="com.mybatisdemo.dao.HeaderTeacherDao.queryHeadTeacherById"/>
    </resultMap>
    
    <select id="queryClazzById" parameterType="int" resultMap="clazzMapper">
        select id,class_name,head_teacher_id from clazz where id = #{id}
    </select>
    
</mapper>
<?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="com.mybatisdemo.dao.HeaderTeacherDao">

    <resultMap type="com.mybatisdemo.beans.HeadTeacher" id="headTeacherMapper">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
    </resultMap>
    
    <select id="queryHeadTeacherById" parameterType="int" resultMap="headTeacherMapper">
        select id,name from head_teacher where id = #{id}
    </select>
    
</mapper>

测试:

@Test
    public void testClazz() {
        ClazzDao clazzDao = sqlSession.getMapper(ClazzDao.class);
        Clazz clazz = clazzDao.queryClazzById(1);
        System.out.println(clazz);
    }

 PS:表结构

create table if not exists head_teacher(
    id int(10) not null primary key,
    name varchar(25) not null
) charset utf8;

create table if not exists clazz (
    id int(10) not null primary key,
    class_name varchar(25) not null unique,
    head_teacher_id int(1),
    foreign key(head_teacher_id) references head_teacher(id)
) charset utf8;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值