个人感觉,关联查询就类似sql中的合并函数
UNION ALL
,有些sql要写很复杂的关联查询的时候,可以用mybatis的关联查询来做
用项目中的例子来说明:
<resultMap id="BaseResultMap" type="com.uhope.data.export.domain.EdTemplateField">
<!--
WARNING - @mbg.generated
-->
<id column="id" jdbcType="VARCHAR" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="template_id" jdbcType="VARCHAR" property="templateId" />
<result column="default_value" jdbcType="VARCHAR" property="defaultValue" />
<result column="sort" jdbcType="INTEGER" property="sort" />
</resultMap>
<resultMap id="BaseResultMapDTO" type="com.uhope.data.export.dto.EdTemplateFieldDTO" extends="BaseResultMap">
<collection property="edTools" ofType="com.uhope.data.export.dto.EdToolsDTO" select="selectToolsByFieldId" column="id">
</collection>
<collection property="edTableFields" ofType="com.uhope.data.export.domain.EdTableField" select="selectTableFieldsByFieldId" column="id">
</collection>
</resultMap>
<select id="queryByTempId" resultMap="BaseResultMapDTO" parameterType="java.lang.String">
SELECT
etf.*
FROM
ed_template_field etf
WHERE
etf.template_id = #{templateId}
</select>
<select id="selectToolsByFieldId" parameterType="java.lang.String" resultType="com.uhope.data.export.dto.EdToolsDTO">
SELECT
et.*,eft.type AS failType, eft.other AS other
FROM
ed_field_tools eft,
ed_tools et
WHERE
et.id = eft.tool_id
AND eft.field_id = #{filedId}
</select>
<select id="selectTableFieldsByFieldId" parameterType="java.lang.String" resultType="com.uhope.data.export.domain.EdTableField">
SELECT
etf.Id AS id,
etf.field_name AS fieldName,
etf.table_name AS tableName,
etf.field_type AS fieldType,
etf.type AS type,
etf.template_id AS templateId
FROM
ed_table_field etf,
ed_template_tablefield ettf
WHERE
etf.id = ettf.tableField_id
AND ettf.templateField_id = #{templateField_id}
</select>
说明
利用mybatis的resultMap标签中的collection
子标签,能够实现合并查询集合的效果,接下来对collection
子标签中属性做说明
- property
这个可以理解为是平时resultMap标签中的子标签result
,对应父标签中的返回对象的一个属性,父resultMap:BaseResultMapDTO
里type返回的是一个DTO中属性对应的 - ofType
顾名思义,就是说这个数据返回的类型 - select
指定查询的sql语句id - column
字段
通过这样说明,估计也看懂了上面的mybatis写的含义,父resultMap下面两个collection
,对应两个集合查询select
标签,分别是selectToolsByFieldId
和selectTableFieldsByFieldId
,而这个父resultMap就对应一条sql语句,就是queryByTempId
,可以看到,它的resultMap对应的就是这个父resultMap,下面两个子查询是返回各自的对象的,两个子查询的逻辑关联查询不会影响到主查询语句,最后的结果只是拼接了起来,装入了父resultMap的返回对象的两个集合里,而主查询也是自然返回集合到自己的对象里,说明一下,我这个DTO对象是继承下来的,父resultMap也是有继承属性extends
的,所以父resultMap中没有result标签,因为会对应上,要是有需要,可以加上这个标签,通过AS转换
public class EdTemplateFieldDTO extends EdTemplateField {
private List<EdToolsDTO> edTools;
private List<EdTableField> edTableFields;
public List<EdToolsDTO> getEdTools() {
return edTools;
}
public void setEdTools(List<EdToolsDTO> edTools) {
this.edTools = edTools;
}
public List<EdTableField> getEdTableFields() {
return edTableFields;
}
public void setEdTableFields(List<EdTableField> edTableFields) {
this.edTableFields = edTableFields;
}
@Override
public String toString() {
return "EdTemplateFieldDTO{" +
"edTools=" + edTools +
", edTableFields=" + edTableFields +
'}';
}
}