如何通过标签重用Mybatis的代码段

时间:2022-12-08 13:44:20

1、首先定义一个sql标签,一定要定义唯一id<sql id="Base_Column_List" >name,age</sql>
2、然后通过id引用
<select id="selectAll">
select
<include refid="Base_Column_List" />
from student
</select>
这个<include refid="Base_Column_List" />会自动把上面的代码贴过来。

例子:

<sql id="allColumn">  ID                     as  id,  SPARTICIPANTBANKNO as sParticipantBankNO,  SPARTICIPANTFULLNAME as sParticipantFullName,  SPHONE as sPhone,  SADDRESS as sAddress   </sql>      <select id="queryCNAPSInfoByCondition" resultType="com.pcitc.ebank.query.entity.CNAPSInfo">   SELECT * FROM (SELECT A.*, ROWNUM RN FROM (    SELECT      <include refid='allColumn'/>     FROM BASE_BANK_INFO    WHERE 1=1     <if test="sparticipantbankno != null">        AND sparticipantbankno like '%'||#{sparticipantbankno}||'%'    </if>      <if test="sparticipantfullname != null">        AND sparticipantfullname like '%'||#{sparticipantfullname}||'%'    </if>        ) A     WHERE ROWNUM  &lt;= #{endindex})   WHERE RN >= #{startindex}   </select>