mybatis Mapper XML 文件 Result Maps 一对一与一对查询

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Frankenstein_/article/details/85051389

 一对一关联查询

  • 创建一个实体类
package com.touchspring.annualparty.base.entity;

import java.util.Date;

public class Chat {
    private String id;
    private String userId;
    private String content;
    private Date createAt;

    public String getId() {
        return id;
    }

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

。。。。。。略  自行补足
或在类名前添加  @Data  注解可省略getter 和 setter
  }
  • 创建Dto类
package com.touchspring.annualparty.base.dto;

import com.touchspring.annualparty.base.entity.User;

import java.util.Date;

public class ChatDto {

    private String id;
    private String userId;
    private String content;
    private Date createAt;

    /**
     * 用户信息                一对一关联查询的对象
     */
    private User user;

   
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}
  • 编写XML文件 
<?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.touchspring.annualparty.base.dao.ChatDao">
    <resultMap id="BaseResultMap" type="com.touchspring.annualparty.base.entity.Chat">
        <id column="id" property="id"/>
        <result column="user_id" property="userId"/>
        <result column="content" property="content"/>
        <result column="create_at" property="createAt"/>
    </resultMap>

    <resultMap id="BaseDtoResultMap" type="com.touchspring.annualparty.base.dto.ChatDto">
        <id column="id" property="id"/>
        <result column="user_id" property="userId"/>
        <result column="content" property="content"/>
        <result column="create_at" property="createAt"/>


    <association property="user" javaType="com.touchspring.annualparty.base.entity.User">                                                                   
            <result column="firstName" property="firstName"/>
            <result column="lastName" property="lastName"/>
            <result column="photo" property="photo"/>
     </association>
    </resultMap>




    <select id="findAllChats" resultMap="BaseDtoResultMap">
        SELECT c.*,u.first_name AS firstNmae, u.last_name AS lastName,u.photo AS photo
        FROM chat c LEFT JOIN user u
        ON  c.user_id = u.id
    </select>


</mapper>
property 映射到列结果的字段或属性。对应实体中setter()的参数
column 数据库中的列名,或者是列的别名。一般情况下,这和 传递给 resultSet.getString(columnName) 方法的参数一样。

 一对一关联 association 中使用 JavaType 返回项目实体类  一对多collection关联查询中的使用  ofType 返回项目实体类

 结果集如下:

 "comments": [
            {
                "id": "3",
                "userId": "10051002",
                "topic": "他",
                "content": "信标",
                "createAt": "2018-12-18 09:41",
                "user": {
                    "id": null,
                    "lastName": "1",
                    "firstName": "1",
                    "title": null,
                    "function": null,
                    "department": null,
                    "country": null,
                    "telephone": null,
                    "photo": "2",
                    "password": null,
                    "email": null,
                    "userGroupId": null,
                    "color": null,
                    "passportId": null,
                    "visa": null,
                    "arrivalFlight": null,
                    "hotelCheckInAt": null,
                    "hotelCheckOutAt": null,
                    "clothingSize": null,
                    "foodRestriction": null,
                    "createAt": null
                }
            }
             ]

一对多关联查询

  • 实体类和Dto类创建基本一致,Dto中稍有不同  一对一为实体对象,一对多为List<Object>对象
public class EngineeringDto {

    /**
     * ID
     */
    private String id;
    /**
     * 名称
     */
    private String name;
    /**
     * 创建时间
     */
    private Date createAt;
    /**
     * 更新时间
     */
    private Date updateAt;
    /**
     * 分包商
     */
    private List<Subcontractor> subcontractors;
}
  • xml文件如下
<mapper namespace="com.touchspring.isite.base.dao.EngineeringDao">
    <resultMap id="BaseResultMap" type="com.touchspring.isite.base.entity.Engineering">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="createAt" property="createAt"/>
        <result column="updateAt" property="updateAt"/>

    </resultMap>

    <resultMap id="BaseResultMapDto" type="com.touchspring.isite.base.dto.EngineeringDto">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="createAt" property="createAt"/>
        <result column="updateAt" property="updateAt"/>
        <result column="projectId" property="projectId"/>

        <collection property="subcontractors" ofType="com.touchspring.isite.base.entity.Subcontractor">
            <result column="subcontractorId" property="id"/>
            <result column="subcontractorName" property="name"/>
        </collection>

    </resultMap>

 <select id="findListInProject" resultMap="BaseResultMapDto">
        SELECT e.*,s.id subcontractorId,s.name subcontractorName FROM engineering e
        LEFT JOIN engineering_subcontractor es
        ON e.id = es.engineering_id
        LEFT JOIN subcontractor s
        ON s.id = es.subcontractor_id
        WHERE
        <if test="id != null and id != ''">
            es.project_id = #{id}
        </if>
    </select>

</mapper>
  • 结果如下
 "engineering": [
            {
                "id": "111",
                "name": "机电消防安装",
                "createAt": null,
                "updateAt": null,
                "subcontractors": [
                    {
                        "id": "1041156821873725440",
                        "name": "上海建工集团股份有限公司",
                        "phone": null,
                        "subType": null,
                        "subManager": null,
                        "subAddress": null,
                        "parentId": null,
                        "createAt": null,
                        "updateAt": null,
                        "children": null,
                        "totalCount": null,
                        "image": null
                    },
                    {
                        "id": "1041199607712976896",
                        "name": "上海威盟斯建筑工程有限公司",
                        "phone": null,
                        "subType": null,
                        "subManager": null,
                        "subAddress": null,
                        "parentId": null,
                        "createAt": null,
                        "updateAt": null,
                        "children": null,
                        "totalCount": null,
                        "image": null
                    }
                ],
                "projectId": null,
                "engAndSubRelationIdArr": null
            }
               ]

猜你喜欢

转载自blog.csdn.net/Frankenstein_/article/details/85051389