mybatis實現多表一對一,一對多,多對多關聯查詢


原文:https://blog.csdn.net/m0_37787069/article/details/79247321

1、一對一
關鍵字:association
作用:針對pojo對象屬性的映射
      property:pojo的屬性名
      javaType:pojo類名
(1) 嵌套結果: 使用嵌套結果映射來處理重復的聯合結果的子集

<resultMap type="com.gec.domain.Person" id="basePersonResultMap">
      <id column="id" property="personId"/>
      <result column="name" property="name"/>
      <result column="sex" property="sex"/>
      <result column="age" property="age"/>
    
      <association property="card" javaType="com.gec.domain.Card">
          <!-- 1、構造器注入
        <constructor>
              <idArg column="id" javaType="int"/>
              <arg column="code" javaType="string"/>
          </constructor> 
        -->
        <!-- 2、setter注入 -->
          <result column="id" property="cardId"/>
          <result column="code" property="code"/>
      </association>
 </resultMap>
  <select id="queryUserList" resultMap="basePersonResultMap">
      select p.id as personId,p.name,p.sex,p.age,c.*
      from tbl_person p,tbl_card c where p.card_id=c.id;
  </select> 

(2) 嵌套查詢:通過執行另外一個SQL映射語句來返回預期的復雜類型
    column="引入執行另外定制sql方法的參數值(外鍵)"
    select="執行定制sql方法名"
PersonMapper2.xml

<resultMap type="com.gec.domain.Person" id="basePersonResultMap">
      <id column="id" property="personId"/>
      <result column="name" property="name"/>
      <result column="sex" property="sex"/>
      <result column="age" property="age"/>
      <association property="card" 
          javaType="com.gec.domain.Card"
          column="card_id" 
          select="com.gec.mapper.CardMapper.queryCardById">
      </association>
</resultMap>
<select id="queryUserList" resultMap="basePersonResultMap">
    select * from tbl_person;
</select>

CardMapper.xml

<resultMap type="com.gec.domain.Card" id="baseCardResultMap">
      <id column="card_id" property="cardId"/>
      <result column="code" property="code"/>
</resultMap>
<select id="queryCardById" resultMap="baseCardResultMap">
      select c.id as card_id,c.code from tbl_card c 
    where c.id=#{id};
</select>
<resultMap type="com.gec.domain.Card" id="queryCardResultMap" extends="baseCardResultMap">
    <association property="person" javaType="com.gec.domain.Person">
          <id column="card_id" property="personId"/>
          <result column="name" property="name"/>
          <result column="sex" property="sex"/>
          <result column="age" property="age"/>
      </association>
</resultMap>
<select id="queryCardList" resultMap="queryCardResultMap">
    SELECT c.id AS card_id, c.code, p.* 
      FROM tbl_card c,tbl_person p WHERE c.id=p.card_id;
</select>

2 、一對多
mybatis如何實現一對多的實現?(學生與班級)
(1) 嵌套結果:
ClazzMapper.xml

<resultMap type="com.gec.domain.Clazz" id="baseClazzResultMap">
    <id column="id" property="clazzId"/>
    <result column="clazz_name" property="clazzName"/>
    <result column="code" property="code"/>
</resultMap>
<resultMap type="com.gec.domain.Clazz" id="queryClazzList2ResultMap" extends="baseClazzResultMap">
    <collection property="studentList" javaType="ArrayList" ofType="com.gec.domain.Student">
        <id column="stu_id" property="studentId"/>
        <result column="name" property="name"/>
        <result column="sex" property="sex"/>
        <result column="age" property="age"/>
    </collection>
</resultMap>
<select id="queryClazzList2" resultMap="queryClazzList2ResultMap">
    SELECT c.*, s.id AS stu_id,s.name,s.sex,s.age
    FROM tbl_clazz c LEFT JOIN tbl_student s
    ON c.id=s.clazz_id;
</select>

StudentMapper.xml

<resultMap type="com.gec.domain.Student" id="baseStudentResultMap">
    <id column="id" property="studentId"/>
    <result column="name" property="name"/>
    <result column="sex" property="sex"/>
    <result column="age" property="age"/>
</resultMap> 
<resultMap type="com.gec.domain.Student" id="queryStudentListResultMap" extends="baseStudentResultMap">
    <association property="clazz" javaType="com.gec.domain.Clazz">
        <id column="cls_id" property="clazzId"/>
        <result column="clazz_name" property="clazzName"/>
        <result column="code" property="code"/>
    </association>
</resultMap> 
<select id="queryStudentList" resultMap="queryStudentListResultMap">
    SELECT s.*,c.id AS cls_id,c.clazz_name,c.code
    FROM tbl_student s,tbl_clazz c WHERE s.clazz_id=c.id;
</select>

(2) 嵌套查詢:
ClazzMapper.xml

<resultMap type="com.gec.domain.Clazz" id="baseClazzResultMap">
    <id column="id" property="clazzId"/>
    <result column="clazz_name" property="clazzName"/>
    <result column="code" property="code"/>
</resultMap>
<resultMap type="com.gec.domain.Clazz" id="queryClazzListResultMap" extends="baseClazzResultMap">
    <collection property="studentList" javaType="ArrayList" 
        column="id" ofType="com.gec.domain.Student" 
        select="com.gec.mapper.StudentMapper.queryStudentByClazzId"
    >
    </collection>
</resultMap>
<select id="queryClazzList" resultMap="queryClazzListResultMap">
    select * from tbl_clazz;
</select>

StudentMapper.xml

<resultMap type="com.gec.domain.Student" id="baseStudentResultMap">
    <id column="id" property="studentId"/>
    <result column="name" property="name"/>
    <result column="sex" property="sex"/>
    <result column="age" property="age"/>
</resultMap> 
<resultMap type="com.gec.domain.Student" id="queryStudentListResultMap" extends="baseStudentResultMap">
    <association property="clazz" javaType="com.gec.domain.Clazz">
        <id column="cls_id" property="clazzId"/>
        <result column="clazz_name" property="clazzName"/>
        <result column="code" property="code"/>
    </association>
</resultMap> 
<select id="queryStudentList" resultMap="queryStudentListResultMap">
    SELECT s.*,c.id AS cls_id,c.clazz_name,c.code
    FROM tbl_student s,tbl_clazz c WHERE s.clazz_id=c.id;
</select>
<!-- 根據班級的id,獲取學生列表 -->
<select id="queryStudentByClazzId" resultMap="baseStudentResultMap">
      select * from tbl_student where clazz_id=#{id};        
</select>

3、多對多
商品表、訂單表之間就是以多對多關聯
商品與訂單的關系表
描述多對多的數據表實現
(1)商品pojo:
Article.java

public class Article implements Serializable {
    private Integer articleId;
    private String name;
    private Double price;
    private String remark;
    private List<Order> orders;
    省略setter/gettera方法
}

(2)商品表映射:
ArticleMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
<mapper namespace="com.gec.mapper.ArticleMapper">
    <resultMap type="article" id="baseArticleResultMap">
        <id column="id" property="articleId"/>
        <result column="NAME" property="name"/>
        <result column="price" property="price"/>
        <result column="remark" property="remark"/>
    </resultMap>
    <resultMap type="article" id="findArtcleByIdResultMap" extends="baseArticleResultMap">
        <collection property="orders" javaType="ArrayList"
            ofType="com.gec.domain.Article" column="id"
            select="com.gec.mapper.OrderMapper.findOrderByArticleId"
        >
        </collection>
    </resultMap>
    <!-- 根據訂單id查詢商品 -->
    <select id="findArtcleByOrderId" resultMap="baseArticleResultMap">
        select * from tb_article  where id 
        in (select article_id from tb_item where order_id=#{id}) 
    </select>
    <select id="findArtcleById" resultMap="findArtcleByIdResultMap">
        select * from tb_article  where id=#{id}
    </select>
</mapper>

(3)訂單pojo:
Order.java

public class Order {
    private Integer orderid;
    private String code;
    private Double total;
    private List<Article> articles;
    省略setter/getter方法
}

(4)訂單表映射:
OrderMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
<mapper namespace="com.gec.mapper.OrderMapper">
    <resultMap type="order" id="baseOrderResultMap">
        <id column="orderId" property="orderid"/>
        <result column="CODE" property="code"/>
        <result column="total" property="total"/>
    </resultMap>
    <resultMap type="order" id="queryOrderByUserIdRsultMap" extends="baseOrderResultMap">
        <collection property="articles" javaType="ArrayList"
            ofType="article" column="orderId"
            select="com.gec.mapper.ArticleMapper.findArtcleByOrderId">
        </collection>
    </resultMap>
    <!-- 根據商品id查詢訂單 -->
    <select id="findOrderByArticleId" resultMap="baseOrderResultMap">
        select * from tb_order  where id 
        in (select order_id from tb_item where article_id=#{id}) 
    </select>
</mapper>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM