mybatis Mapper XML 檔案 Result Maps 一對一與一對查詢
阿新 • • 發佈:2019-01-10
一對一關聯查詢
- 建立一個實體類
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
}
]