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