mybatis 中高級結果映射(resultMap)的列前綴映射

mybatis 中高級結果映射的官方文檔示例:

http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html#a

 其中使用的列映射是在xml中直接配置的,比如:


<resultMap id="detailedBlogResultMap" type="Blog">
<constructor>
<idArg column="blog_id" javaType="int"/>
</constructor>
<result property="title" column="blog_title"/>
<association property="author" javaType="Author">
<id property="id" column="author_id"/>
<result property="username" column="author_username"/>
<result property="password" column="author_password"/>
<result property="email" column="author_email"/>
<result property="bio" column="author_bio"/>
<result property="favouriteSection" column="author_favourite_section"/>
</association>
<collection property="posts" ofType="Post">
<id property="id" column="post_id"/>
<result property="subject" column="post_subject"/>
<association property="author" javaType="Author"/>
<collection property="comments" ofType="Comment">
<id property="id" column="comment_id"/>
</collection>
<collection property="tags" ofType="Tag" >
<id property="id" column="tag_id"/>
</collection>
<discriminator javaType="int" column="draft">
<case value="1" resultType="DraftPost"/>
</discriminator>
</collection>
</resultMap>

實際使用中我們經常在屬性配置時使用 autoMapping, columnPrefix 來做字段映射,按照之前的想法直接在 select 時添加前綴的方式匹配不上, 需要在select時,根據層級關系添加對應的父級的columnPrefix:
例子:
<resultMap id="moduleDetail" type="etlModule" autoMapping="true">
<id property="moduleCode" column="module_code"/>
<result property="hospitalName" column="hospital_code"/>
<collection property="workflows" ofType="etlWorkflow" columnPrefix="ew_" autoMapping="true">
<id property="workflowCode" column="workflow_code"/>
<association property="connection" javaType="etlConnection" columnPrefix="ec_" autoMapping="true"/>
<association property="filter" javaType="etlWorkflowTokenFilter" columnPrefix="ewtf_" autoMapping="true"/>

        <collection property="selectList" ofType="etlWorkflowTokenSelect" columnPrefix="ewts_" autoMapping="true">
            <id property="workflowTokenCode" column="workflow_token_code"/>
        </collection>

        <collection property="fromOrJoinList" ofType="etlWorkflowTokenFromOrJoin" columnPrefix="ewtfj_" autoMapping="true">
            <id property="workflowTokenCode" column="workflow_token_code"/>
        </collection>
    </collection>
</resultMap>

select 語句:
SELECT
ew.workflow_code AS ew_workflow_code,
ew.workflow_name AS ew_workflow_name,
ew.workflow_sequence_default AS ew_workflow_sequence_default,
ew.workflow_sequence_customized AS ew_workflow_sequence_customized,
ew.module_code AS ew_module_code,
ew.component_code AS ew_component_code,
ew.is_enable AS ew_is_enable,
ew.is_default AS ew_is_default,
ew.created_at AS ew_created_at,
ew.updated_at AS ew_updated_at,
ec.connection_code AS ew_ec_connection_code,
ec.type AS ew_ec_type,
ec.url AS ew_ec_url,
ec.user AS ew_ec_user,
ec.engine_id AS ew_ec_engine_id,
ec.created_at AS ew_ec_created_at,
ec.updated_at AS ew_ec_updated_at,
ewts.workflow_token_code AS ew_ewts_workflow_token_code,
ewts.source_table_alias_name AS ew_ewts_source_table_alias_name,
ewts.source_column_name AS ew_ewts_source_column_name,
ewts.source_column_expression_default AS ew_ewts_source_column_expression_default,
ewts.source_column_expression_customized AS ew_ewts_source_column_expression_customized,
ewts.target_column_alias_name AS ew_ewts_target_column_alias_name,
ewts.is_enable AS ew_ewts_is_enable,
ewts.is_default AS ew_ewts_is_default,
ewts.created_at AS ew_ewts_created_at,
ewts.updated_at AS ew_ewts_updated_at,
ewtfj.workflow_token_code AS ew_ewtfj_workflow_token_code,
ewtfj.source_db_name AS ew_ewtfj_source_db_name,
ewtfj.source_table_name AS ew_ewtfj_source_table_name,
ewtfj.source_table_expression AS ew_ewtfj_source_table_expression,
ewtfj.source_table_alias_name AS ew_ewtfj_source_table_alias_name,
ewtfj.is_primary_table AS ew_ewtfj_is_primary_table,
ewtfj.join_type AS ew_ewtfj_join_type,
ewtfj.join_on_current_column_name AS ew_ewtfj_join_on_current_column_name,
ewtfj.join_on_right_table_alias_name AS ew_ewtfj_join_on_right_table_alias_name,
ewtfj.join_on_right_table_column_name AS ew_ewtfj_join_on_right_table_column_name,
ewtfj.join_on_expression AS ew_ewtfj_join_on_expression,
ewtfj.is_enable AS ew_ewtfj_is_enable,
ewtfj.is_default AS ew_ewtfj_is_default,
ewtfj.created_at AS ew_ewtfj_created_at,
ewtfj.updated_at AS ew_ewtfj_updated_at,
ewtfj.workflow_token_code AS ew_ewtf_workflow_token_code,
ewtf.common_filter_expression AS ew_ewtf_common_filter_expression,
ewtf.common_filter_expression_customized AS ew_ewtf_common_filter_expression_customized,
ewtf.incremental_filter_expression AS ew_ewtf_incremental_filter_expression,
ewtf.incremental_filter_expression_customized AS ew_ewtf_incremental_filter_expression_customized,
ewtf.range_filter_expression AS ew_ewtf_range_filter_expression,
ewtf.range_filter_expression_customized AS ew_ewtf_range_filter_expression_customized,
ewtf.is_enable AS ew_ewtf_is_enable,
ewtf.is_default AS ew_ewtf_is_default,
ewtf.created_at AS ew_ewtf_created_at,
ewtf.updated_at AS ew_ewtf_updated_at
...

注意其中select as 的字段名稱,添加了resultMap中的層級關系。

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,438評論 0 10
  • [轉]SQLite.swift Documentation InstallationCarthageCocoaPo...
    mqhong閱讀 7,361評論 0 2
  • 本書是過去三年運營早期互聯網孵化器過程中,所有經歷的反思和總結。我們認為,運營早期的互聯網孵化器就是運營創業公司。...
    梅晨斐閱讀 459評論 0 1
  • 文 / suyyan *一個非正統的武俠故事 *格式好難調 “師父,我覺得我們真的很有緣分。” 葉漁面無表情地把這...
    蘇言suyyan閱讀 230評論 0 0
  • 寂寞闌前,無端葉落愁難住。畫橋花塢,攜手曾游處。 石砌空階,懶倚何堪顧,嗟歧路。文園詞賦,惆悵人南浦。
    劉小地閱讀 633評論 30 115