Mybatis处理一对一 、 一对多关系

1. 查询到的数据映射到实体类中的包含的实体类中属性字段上

https://blog.csdn.net/qq_20610631/article/details/81671997

一对一

方式一()==:
例如 班级类ClassRoom类,这个类中有一个成员变量:班主任Teacher。班主任有一个name属性。

public class ClassRoom {
    //班级编号
    private Long id;
    //一个班级有一个班主任
    private Teacher teacher;
    //班主任的id
    private Long TeacherId;
    //get set
 
}
 
class Teacher {
    //班主任名称
    private String name;
    //get set  
}

核心:映射到某个对象的某个属性中

SELECT 
    c.id    c.id ,
    t.name  "teacher.name", //映射到某个对象的某个属性中
    t.id    teacherId
FROM T_CLASSROOM c  LEFT JOIN T_TEACHER  t  ON  c.tid = t.id;

方式二()
在这里插入图片描述

<resultMap type="Student" id="StudentResult">
		<id property="id" column="id"/>
		<result property="name" column="name"/>
		<result property="age" column="age"/>
		<association property="address" javaType="Address">
			<result property="id" column="id"/>
			<result property="sheng" column="sheng"/>
			<result property="shi" column="shi"/>
			<result property="qu" column="qu"/>
		</association>
	</resultMap>
<select id="findStudentWithAddress" resultMap="StudentResult" parameterType="Integer">
        select * from t_student t1,t_address t2 where t1.addressId=t2.id and t1.id=#{id}
</select>

方式三(建议使用)
在这里插入图片描述
第一步:首先为address建立mapper接口和xml映射文件
在这里插入图片描述
在这里插入图片描述
第二步:为student建立mapper接口和xml映射文件

<resultMap type="Student" id="StudentResult">
		<id property="id" column="id"/>
		<result property="name" column="name"/>
		<result property="age" column="age"/>
		<association property="address" column="addressId" select="com.java1234.mappers.AddressMapper.findById"></association>
	</resultMap>
<select id="findStudentWithAddress" parameterType="Integer" resultMap="StudentResult">
		select * from t_student where id=#{id}
</select>  
这种方式简洁,而且和根据id查询单独的学生信息写法很类似
		     <select id="findById" parameterType="Integer" resultType="Student">
			 	 select * from t_student where id=#{id}
			 </select>

或者
  <select id="findStudentWithAddress" resultMap="StudentResult" parameterType="Integer">
		select * from t_student t1,t_address t2 where t1.addressId=t2.id and t1.id=#{id}
</select>

一对多

在这里插入图片描述

<resultMap type="Student" id="StudentResult">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="age" column="age"/>
        <association property="address" column="addressId" select="com.java1234.mappers.AddressMapper.findById"></association>
        <association property="grade" column="gradeId" select="com.java1234.mappers.GradeMapper.findById"></association>
    </resultMap>
<select id="findStudentWithGrade" resultMap="StudentResult" parameterType="Integer">
        SELECT * FROM t_student t1 INNER JOIN t_address t2 ON t1.addressId = t2.id INNER JOIN t_grade t3 on t3.id = t1.gradeId WHERE t1.id=#{id}
    </select>

2. 实体类中含有List集合

一个学生,他的住址可能会有多个。所以,在Student实体中,有一个List<Address> addressList集合。

然后我们,在查询的时候,查到了这个学生的主键ID。再拿学生的主键ID去地址表里面查询。

@Getter
@Setter
public class Address {
 
    private Long id;
 
    private String addressName;
 
    private String addressCode;
 
    private String stuId;
}
@Getter
@Setter
public class Student {
 
    private Long id;
 
    private String stuName;
 
    private String stuAge;
 
    private List<Address> addressesList;
}

Mybatis处理文件如下:

<!-- (1)首先是一个查询所有学生的SQL-->
<!-- (2)然后映射学生的基本数据,再使用collection级联查询-->
<!-- (3)最后再通过相关查询条件,查询并封装List集合数据-->
<!-- (1)首先是一个查询所有学生的SQL-->
<select id="getAllStudent" resultMap="studentMaps">
       select
        ID ,
        STU_NAME ,
        STU_AGE
        from T_STUDENT
  </select>
 
<!-- (2)然后映射学生的基本数据,再使用collection级联查询-->
    <resultMap id="studentMaps" type="com.safesoft.elephant.domain.address.entity.Student">
        <id column="ID" property="id"/>
        <result column="STU_NAME" property="stuName"/>
        <result column="STU_AGE" property="stuAge"/>
        <!--
               property:Java中list的引用名
               ofType:List的泛型
               select:即将要查询的SQL语句的id
               column:传递过去的查询参数
        -->
        <collection property="addressesList" ofType="com.safesoft.elephant.domain.address.entity.Address"
                    select="getAddress" column="ID">
        </collection>
    </resultMap>
 
<!-- (3)最后再通过相关查询条件,查询并封装List集合数据-->
    <select id="getAddress" parameterType="int" resultType="com.safesoft.elephant.domain.address.entity.Address">
        SELECT
        ADDRESS_NAME addressName,
        ADDRESS_CODE addressCode
        FROM `T_ADDRESS` WHERE STU_ID = #{ID}
    </select>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值