mapper.xml 中共用
mapper.xml 间共用
项目中也许我们会遇到一段sql语句被多个查询、增加等语句用到的情况,如何去偷懒呢,复用sql无疑是较好的选择
这里只提供简单的示范:
如果只是单表查询,并且希望共用的sql只会出现在同一个mapper.xml文件中,那么我们可以直接在
1
|
<mapper namespace= "XXXXX" ></mapper> |
中写下面的业务代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<sql id= "unitSql" > a.userId,a.legalCode,legalName, bidderCodeType,legalRole, licenseNo,DATE_FORMAT(licenseEndDate, "%Y-%m-%d" ) AS licenseEndDate, taxCertNo,DATE_FORMAT(taxCertEndDate, "%Y-%m-%d" ) AS taxCertEndDate, localTaxCertNo,DATE_FORMAT(localTaxCertEndDate, "%Y-%m-%d" ) AS localTaxCertEndDate, organNo,DATE_FORMAT(organCertEndDate, "%Y-%m-%d" ) AS organCertEndDate, legalRepresent,legalType, perresentPhone,legalIndustary,creditRate, countryRegion,legalUnitAddress,regionCode,registerProvince, a.regCapital,a.regCapCurRency,a.regUnit, registerCity,registerCounty,basicBank,basicBranchBank, basicAccountNo,basicAccountName,legalContact,legalContactPhone, legalContactAddress,legalWeb,legalZipCode,legalEmail, legalStatus </sql> |
然后引用
1
2
3
4
5
6
7
8
9
10
|
<select id= "getTenderAgentById" parameterType= "String" resultType= "Map" > select <include refid= "unitSql" />, -- 就是这句话引用 ,refid为共有sql语句id b.auditOpinion,b.createUser, b.createTime, b.lastUpdateUser, b.lastUpdateTime, b.disabled,b.infoStatus from P_legalUnit a, P_tenderAgent b where b.userId=#{userId} AND a.userId = b.userId AND b.disabled= '0' </select> |
如果是连表查询,那么我们极有可能会希望有一个sql语句被多个mapper.xml引用的的方法,很简单。
我们可以新建一个mapper.xml文件(当然,这个mapper.xml要在你的扫描路径下,本文不赘述)。取名为common.xml
该文件内容为
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<?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" > <mapper namespace= "common" > <sql id= "common.unitSql" > a.userId,a.legalCode,legalName, bidderCodeType,legalRole, licenseNo,DATE_FORMAT(licenseEndDate, "%Y-%m-%d" ) AS licenseEndDate, taxCertNo,DATE_FORMAT(taxCertEndDate, "%Y-%m-%d" ) AS taxCertEndDate, localTaxCertNo,DATE_FORMAT(localTaxCertEndDate, "%Y-%m-%d" ) AS localTaxCertEndDate, organNo,DATE_FORMAT(organCertEndDate, "%Y-%m-%d" ) AS organCertEndDate, legalRepresent,legalType, perresentPhone,legalIndustary,creditRate, countryRegion,legalUnitAddress,regionCode,registerProvince, a.regCapital,a.regCapCurRency,a.regUnit, registerCity,registerCounty,basicBank,basicBranchBank, basicAccountNo,basicAccountName,legalContact,legalContactPhone, legalContactAddress,legalWeb,legalZipCode,legalEmail, legalStatus </sql> </mappper> |
在另一个mapper.xml文件引用
1
2
3
4
5
6
7
8
9
10
|
<select id= "getTenderAgentById" parameterType= "String" resultType= "Map" > select <include refid= "common.unitSql" />, -- 就是这句话引用 ,refid为共有sql语句id b.auditOpinion,b.createUser, b.createTime, b.lastUpdateUser, b.lastUpdateTime, b.disabled,b.infoStatus from P_legalUnit a, P_tenderAgent b where b.userId=#{userId} AND a.userId = b.userId AND b.disabled= '0' </select> |
==注:为何我的文件名要取为common.xml并且id也为common.unitSql呢?这是为了后期维护方便,如此,可以更容易找到该共有的sql在哪一个文件==
以上所述是小编给大家介绍的Mybatis中 SQL语句复用,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:http://blog.csdn.net/sinat_33155679/article/details/61914139