MyBatis Common code extraction to a separate mapper.xml file

Source: Internet
Author: User

As with any code base, in mapper, there will usually be some common SQL snippets that are referenced by many business mapper.xml, such as paging and data permission filtering, especially in Oracle's paging syntax. In order to reduce the skeleton code, they are often abstracted into SQL, but certainly not included in each mapper, which makes no sense. At this point, you can move this section to a dedicated mapper.xml, such as Common.xml, which contains the following:

<?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 "> <Mappernamespace= "Common">    <SQLID= "Common.pagingstart">
</SQL> <SQLID= "Common.pagingend"> <! [Cdata[Limit #{startwith,jdbctype=integer},#{rows,jdbctype=integer}]]> </SQL></Mapper>

Then the specific mapper.xml can be referenced by namespace, as follows:

    <SelectID= "Querypage"Resultmap= "Clientpage"ParameterType= "Java.util.Map">        <includerefID= "Common.pagingstart"/>        <includerefID= "Commonselect"/>            <!--here's an extra 1 to avoid the extra handling of the last "," -1<includerefID= "Commonfrom"/>        <includerefID= "Commonwhere"/>          <ifTest= "ClientId! = null" >and client_id = #{clientid,jdbctype=varchar}</if>          <ifTest= "ClientName! = null" >and client_name like '%${clientname} '</if>          <ifTest= "Telephone! = NULL" >and telephone = #{telephone,jdbctype=varchar}</if>ORDER BY client_id<includerefID= "Common.pagingend"/>    </Select>

MyBatis Common code extraction to a separate mapper.xml file

Related Article

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.