A little idea about the mybatis extension

Source: Internet
Author: User

The main purpose of this article is to add extended attributes to the include tag for MyBatis



When using MyBatis for a single-table query, it is convenient to use the <include> tag to include the defined <sql> node content, such as the following


<sql id= "T_agent_base_column_list" >id,  worker_id, <span  style= "font-family:arial, Helvetica, Sans-serif; " >worker_name,  </span>worker_main_num,  worker_ext_num,  sale_phone,  is_part_time,  Last_login_time, sign  ,  head_pic,wei_xin,  wx_ewm,  WEIBO,  Practic_cert,  work_year,  Service_json,  Service_area,  Praise_count,  shop_view_count,  city_code,  area_code,  area_name  , Place_code,  place_name,  sort_score,  Vocat_skill, FEATURES,  lead_reputably,  STATUS,  VERSION,  Insert_time,  update_time,  business_type,  MANIFESTO,  background_image,  title_id</ sql><!--query--><select id= "Selectbyid" resultmap= "Agentinfo" parametertype= "by primary key > Select<include refid= "t_agent_base_column_list"/>fromagent_infowhereid = #{id,jdbctype=integer}</select >


But when we need some fields from other tables, it's not easy to re-use the SQL node of the t_agent_base_column_list, which is to federate the data of the other table. For example, there is a table for the broker level and the broker with the number of records and other information, we use the following SQL statement

SELECT a.id, a.worker_id, A.worker_name,..... Other field B of the broker table. Level,---broker grade B.subjects_num,--complete number of subjects b.look_num,--with see b.deal_num--Number of transactions <span style= "White-space:pre" ></span >from agent_info aleft JOIN agent_title_info B on A.title_id=b.idwhere a.id = #{id}


The required resultmap is defined as follows

<!--agentinfo are all brokers table fields  --><resultmap id= "Agentexendmap" type= " Com.kevin.learning.modules.entity.agent.AgentInfoExtend "extends=" Agentinfo "><result column=" level " Property= "level" jdbctype= "varchar"/><result column= "Subjects_num" property= "subjectsnum" jdbctype= "varchar" /><result column= "Look_num" property= "Looknum" jdbctype= "VARCHAR"/><result column= "DEAL_NUM" property= " Dealnum "jdbctype=" VARCHAR "/></resultmap>

we can see that the above SQL statement does not reuse the defined T_agent_base_column_list node to refer to the entire t_agent field, but instead writes it again, and the broker details table is not reused.


Here is an extension of my plan, because our SQL is to query the broker information, as well as the broker details of several fields, respectively, is the Agen_info table and the Agent_title_info table, look at the extension of the configuration file, in fact, this is the entire article only the effect you want to achieve


<select id= "selectextendlist" resultmap= "Agentexendmap" parametertype= "map" >select <include refid= "t_agent _base_column_list "prefix=" A. " />,<include refid= "Com.kevin.learning.modules.dao.agenttitle.IAgentTitleInfoDAO.t_agent_info_title_Base_ Column_list "prefix=" B. "  excludecols= "Update_time,insert_time,status,description,id"/>from agent_info ALEFT JOIN AGENT_TITLE_INFO B on A. Title_id=b.idwhere a.id = #{id}</select>
Note: Another include tag is defined by another mapper file, and MyBatis supports SQL nodes that reference other files


There are two main places that have been expanded

1. Add the prefix attribute to the <include> node so that the field referencing this node will be prefixed

2. To add the Excludecols attribute to the <include> node, MyBatis can actually refer to the SQL node defined in another mapper file, and we refer to the Broker Details table above Base_column_ List (contains all the fields of the table), the fields defined in this property are excluded, separated by commas

Result: Our SQL contains fields that are all fields of the Agent_info table + (fields defined in Agent_title_info table all fields-Excludecols)





Level and Looknum are the fields in the broker details, so you can find out, and the excluded Insert_time and update_time have no queries.


Implementation Method Analysis

This change is mainly to the source code modification is very few, only in the Myabtis boot load parsing mapper file to add their own required features, the class to be modified is Xmlincludetransformer Applyinclude method




MyBatis the include parsing completes when parsing SQL replaces the original include node



The processing part of the code is very simple, that is, the SQL node in the prefix, and then the entire string is set to the DOM node, so that the extension is complete, and there is a place to change, that is, to make a small change to the MyBatis DTD file, Because the mapper XML is validated with a DTD at startup






A little idea about the mybatis extension

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.