mybatis查詢的結果集物件中包含物件和集合的用法
平時專案中使用mybatis查詢資料庫,物件結果集可能比較複雜,物件中巢狀物件或者集合。
如下圖所示,返回結果集物件project中包含其他子物件(查詢的主表與關聯子表資料一對一關係)和list集合(查詢的主表與關聯子表資料一對多關係):
解決方案:
物件下面巢狀的物件採用<association>寫法,巢狀的集合採用<collection>寫法
寫法例項:
(1)其中projectInfo與projectCharge物件為Project的巢狀物件,採用<association>寫法,而requires是list集合,採用<collection>寫法
<resultMap type="com.utry.ucsc.task.vo.Project" id="ProjectDetailResultMap">
<association property="projectInfo" javaType="com.utry.ucsc.task.vo.ProjectInfo">
<result column="id" jdbcType="VARCHAR" property="projectId" />
<result column="version" jdbcType="INTEGER" property="version" />
<result column="project_name" jdbcType="VARCHAR" property="projectName" />
<result column="project_type" jdbcType="VARCHAR" property="projectType" />
<result column="description" jdbcType="VARCHAR" property="description" />
<result column="project_status" jdbcType="VARCHAR" property="projectStatus" />
<result column="payment_status" jdbcType="VARCHAR" property="paymentStatus" />
<result column="start_date" jdbcType="TIMESTAMP" property="startDate" />
<result column="end_date" jdbcType="TIMESTAMP" property="endDate" />
<result column="workbench_name" jdbcType="VARCHAR" property="workbenchName" />
<result column="project_logo" jdbcType="VARCHAR" property="projectLogo" />
<result column="project_display_level" jdbcType="VARCHAR" property="projectDisplayLevel" />
<result column="task_total" jdbcType="INTEGER" property="taskTotal" />
<result column="max_task_once" jdbcType="INTEGER" property="maxTaskOnce" />
<result column="accepted_task_count" jdbcType="INTEGER" property="acceptedTaskCount" />
<result column="completed_task_count" jdbcType="INTEGER" property="completedTaskCount" />
<result column="company_id" jdbcType="VARCHAR" property="companyId" />
<result column="creator" jdbcType="VARCHAR" property="creator" />
<result column="create_date" jdbcType="TIMESTAMP" property="createDate" />
<result column="modified_date" jdbcType="TIMESTAMP" property="modifiedDate" />
</association>
<association property="projectCharge" javaType="com.utry.ucsc.task.vo.ProjectCharge">
<result column="charge_type" jdbcType="VARCHAR" property="chargeType" />
<result column="unit_price" jdbcType="DECIMAL" property="unitPrice" />
<result column="extract_price" jdbcType="DECIMAL" property="extractPrice" />
</association>
<collection property="requires" resultMap="ProjectRequireResultMap"/>
</resultMap>
<resultMap type="com.utry.ucsc.task.vo.ProjectRequirement" id="ProjectRequireResultMap">
<result column="require_type" jdbcType="VARCHAR" property="requireType" />
<result column="require_value" jdbcType="VARCHAR" property="requireValue" />
</resultMap>
SQL寫法:
(2)<association>寫法不變,<collection>中採用ofType屬性指向集合中的元素型別
<resultMap type="com.utry.ucsc.task.vo.Project" id="ProjectDetailResultMap">
<association property="projectInfo" javaType="com.utry.ucsc.task.vo.ProjectInfo">
<result column="projectId" jdbcType="VARCHAR" property="projectId" />
<result column="version" jdbcType="INTEGER" property="version" />
<result column="project_name" jdbcType="VARCHAR" property="projectName" />
<result column="project_type" jdbcType="VARCHAR" property="projectType" />
<result column="description" jdbcType="VARCHAR" property="description" />
<result column="project_status" jdbcType="VARCHAR" property="projectStatus" />
<result column="payment_status" jdbcType="VARCHAR" property="paymentStatus" />
<result column="start_date" jdbcType="TIMESTAMP" property="startDate" />
<result column="end_date" jdbcType="TIMESTAMP" property="endDate" />
<result column="workbench_name" jdbcType="VARCHAR" property="workbenchName" />
<result column="project_logo" jdbcType="VARCHAR" property="projectLogo" />
<result column="project_display_level" jdbcType="VARCHAR" property="projectDisplayLevel" />
<result column="task_total" jdbcType="INTEGER" property="taskTotal" />
<result column="max_task_once" jdbcType="INTEGER" property="maxTaskOnce" />
<result column="accepted_task_count" jdbcType="INTEGER" property="acceptedTaskCount" />
<result column="completed_task_count" jdbcType="INTEGER" property="completedTaskCount" />
<result column="company_id" jdbcType="VARCHAR" property="companyId" />
<result column="creator" jdbcType="VARCHAR" property="creator" />
<result column="create_date" jdbcType="TIMESTAMP" property="createDate" />
<result column="modified_date" jdbcType="TIMESTAMP" property="modifiedDate" />
</association>
<association property="projectCharge" javaType="com.utry.ucsc.task.vo.ProjectCharge">
<result column="charge_type" jdbcType="VARCHAR" property="chargeType" />
<result column="unit_price" jdbcType="DECIMAL" property="unitPrice" />
<result column="extract_price" jdbcType="DECIMAL" property="extractPrice" />
</association>
<collection property="requires" ofType="com.utry.ucsc.task.vo.ProjectRequirement">
<result column="require_type" jdbcType="VARCHAR" property="requireType" />
<result column="require_value" jdbcType="VARCHAR" property="requireValue" />
</collection>
</resultMap>
SQL寫法:
(3)projectInfo與projectCharge物件仍然採用<association>寫法,requires採用內部巢狀查詢方式
<resultMap type="com.utry.ucsc.task.vo.Project" id="ProjectDetailResultMap">
<association property="projectInfo" javaType="com.utry.ucsc.task.vo.ProjectInfo">
<result column="projectId" jdbcType="VARCHAR" property="projectId" />
<result column="version" jdbcType="INTEGER" property="version" />
<result column="project_name" jdbcType="VARCHAR" property="projectName" />
<result column="project_type" jdbcType="VARCHAR" property="projectType" />
<result column="description" jdbcType="VARCHAR" property="description" />
<result column="project_status" jdbcType="VARCHAR" property="projectStatus" />
<result column="payment_status" jdbcType="VARCHAR" property="paymentStatus" />
<result column="start_date" jdbcType="TIMESTAMP" property="startDate" />
<result column="end_date" jdbcType="TIMESTAMP" property="endDate" />
<result column="workbench_name" jdbcType="VARCHAR" property="workbenchName" />
<result column="project_logo" jdbcType="VARCHAR" property="projectLogo" />
<result column="project_display_level" jdbcType="VARCHAR" property="projectDisplayLevel" />
<result column="task_total" jdbcType="INTEGER" property="taskTotal" />
<result column="max_task_once" jdbcType="INTEGER" property="maxTaskOnce" />
<result column="accepted_task_count" jdbcType="INTEGER" property="acceptedTaskCount" />
<result column="completed_task_count" jdbcType="INTEGER" property="completedTaskCount" />
<result column="company_id" jdbcType="VARCHAR" property="companyId" />
<result column="creator" jdbcType="VARCHAR" property="creator" />
<result column="create_date" jdbcType="TIMESTAMP" property="createDate" />
<result column="modified_date" jdbcType="TIMESTAMP" property="modifiedDate" />
</association>
<association property="projectCharge" javaType="com.utry.ucsc.task.vo.ProjectCharge">
<result column="charge_type" jdbcType="VARCHAR" property="chargeType" />
<result column="unit_price" jdbcType="DECIMAL" property="unitPrice" />
<result column="extract_price" jdbcType="DECIMAL" property="extractPrice" />
</association>
<collection property="requires" ofType="com.utry.ucsc.task.vo.ProjectRequirement"
select="com.utry.ucsc.task.dao.ProjectInfoBeanMapper.getProjectRequire" column="{projectId=projectId,version=version}">
</collection>
</resultMap>
<resultMap type="com.utry.ucsc.task.vo.ProjectRequirement" id="RequiresResultMap">
<result column="require_type" jdbcType="VARCHAR" property="requireType" />
<result column="require_value" jdbcType="VARCHAR" property="requireValue" />
</resultMap>
對應的sql片段:
注意:這裡巢狀的查詢getProjectRequire查詢中parameterType採用Map,<collection>標籤中的column多個引數寫法也需按照上述規範,=左邊的是getProjectRequire這個方法中的入參名稱,==右邊是getProjectDetail這個查詢方法中的查詢出來對應的欄位名稱。另外,結果集<resultMap>中各個標籤順序也是有規定的:(constructor?, id*, result*, association*, collection*, discriminator?),不按照這個規定,編譯會報錯
三種寫法的比較:
通過日誌列印sql發現,其中第一種和第二種寫法都只查詢了一次sql,第三種查詢了兩次sql,但是用的是一個sql連線,並不是新建一個sql連線,所以效能上差別不大,但是第三種寫法明顯複雜不少,推薦第一種和第二種寫法。