分享知识-快乐自己:论 Mybatis中的关联关系(一对多,多对一,多对多)

时间:2023-03-10 04:31:08
分享知识-快乐自己:论 Mybatis中的关联关系(一对多,多对一,多对多)

论:一对多:(举例一个省有多个市)就是实体类中有(市)类型集合属性;多对一:(多个市有一个共同的省)就是类中有(省)类型的属性。下面来介绍:一对一、多对一的使用方式。

一对多方:

package mlq.bean;
import java.io.Serializable;
import java.util.List;
public class Country implements Serializable {
private Integer cid;
private String cname;
private List<Provincial> list;
get、set 省略...
}

 多对一方:

package mlq.bean;

import java.io.Serializable;
public class Provincial implements Serializable {
private Integer pid;
private String pname;
private Integer countryid;
private Country country;
 get、set 省略...
 }

映射文件配置:(仅供参考)

  <!--使用懒加载技术实现查询-->
<select id="allLzcCountry" resultMap="MyLzcCountrys">
SELECT `cid`,`cname` FROM `country` AS a
WHERE a.`cid`=#{id}
</select> <select id="allLzcCountrys" resultType="Provincial">
select pid,pname,countryid from provincial AS b where b.countryid=#{cid}
</select> <resultMap id="MyLzcCountrys" type="Country">
<id property="cid" column="cid"/>
<result property="cname" column="cname"/>
<!--一对多实体类中的集合赋值-->
<collection property="list" ofType="Provincial" select="allLzcCountrys" column="cid">
<id property="pid" column="pid"/>
<result property="pname" column="pname"/>
</collection>
</resultMap>
  <!--使用延迟加载技术实现多对一的关系-->
<!--首先根据查询id查询省会-->
<select id="selectProvincialByid" resultMap="MeMap">
SELECT `pid`,`pname`,`countryid` FROM `provincial`
WHERE `pid`=#{pid}
</select>
<!--再根据省会查询出来的id查询国家-->
<select id="selectCountryByid" resultType="Country">
SELECT `cid`,`cname` FROM `country` WHERE `cid`=#{cid}
</select>
<resultMap id="MeMap" type="Provincial">
<id property="pid" column="pid"/>
<result property="pname" column="pname"/>
<result property="countryid" column="countryid"/>
<!--根据省会查询出来的id查询国家-->
<association property="country" javaType="Provincial" select="selectCountryByid" column="countryid"/>
</resultMap>

多对多:(举例老师和学生)就是双方都有集合类属性(多对多的实现需要第三张表维持关联关系,不需要显示具体的实体类)

package mlq.bean;
import java.util.List;
/**
* 学生类
*/
public class Student {
private Integer sid;
private String sname;
private Integer age;
private List<Teacher> teachers;
 get、set 省略...
}
package mlq.bean;
import java.util.List;
/**
* 教师类
*/
public class Teacher {
private Integer id;
private String name;
private Integer tid;
private List<Student> students;
get、set 省略...
}

映射文件配置:

  <resultMap id="studentMap" type="Student">
<id property="sid" column="sid"></id>
<result property="sname" column="sname"/>
<result property="age" column="age"/>
<collection property="teachers" ofType="Teacher">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="tid" column="tid"/>
</collection>
</resultMap> <select id="findAllByStudent" resultMap="studentMap">
SELECT s.`sid`,s.`sname`,s.`age`,t.`id`,t.`name`,t.`tid`
FROM `student` AS s,`middle` AS m,`teacher` AS t
WHERE s.`sid`=m.`sid` AND m.`tid`=t.`tid` AND s.sid=#{id}
</select>   <--以上为学生-->
  <resultMap id="teacherMap" type="Teacher">
   <id property="id" column="id"/>
  <result property="name" column="name"/>
  <result property="tid" column="tid"/>
   <collection property="students" ofType="Student">
   <id property="sid" column="sid"></id>
   <result property="sname" column="sname"/>
   <result property="age" column="age"/>
   </collection>
  </resultMap>   <select id="findAllByTeacher" resultMap="teacherMap">
   SELECT s.`sid`,s.`sname`,s.`age`,t.`id`,t.`name`,t.`tid`
   FROM `student` AS s,`middle` AS m,`teacher` AS t
   WHERE s.`sid`=m.`sid` AND m.`tid`=t.`tid` AND t.`tid`=#{id}
  </select>
 

以上为关键代码的展示。