mybatis中 標簽的作用


MyBatis中sql標簽定義SQL片段,
include標簽引用,可以復用SQL片段

sql標簽中id屬性對應include標簽中的refid屬性。通過include標簽將sql片段和原sql片段進行拼接成一個完整的sql語句進行執行。

<sql id="sqlid"> res_type_id,res_type </sql> <select id="selectbyId" resultType="com.property.vo.PubResTypeVO"> select <include refid="sqlid"/> from pub_res_type </select>
  • 引用同一個xml中的sql片段
<include refid="sqlid"/>
  • 引用公用的sql片段
<include refid="namespace.sqlid"/>

include標簽中也可以用property標簽,用以指定自定義屬性。在sql標簽中通過${}取出對應的屬性值。

<select id="queryPubResType" parameterType="com.property.vo.PubResTypeVO" resultMap="PubResTypeList"> select a.res_type_id, <include refid="com.common.dao.FunctionDao.SF_GET_LNG_RES_TYPE"> <property name="AI_RES_TYPE_ID" value="a.res_type_id"/> <property name="lng" value="#{lngId}"/> <property name="female" value="'女'"/> </include> as res_type from pub_res_type a </select>

 

 

使用resultType進行輸出映射,只有查詢出來的列名和pojo中的屬性名一致,該列才可以映射成功。

如果查詢出來的列名和pojo的屬性名不一致,通過定義一個resultMap對列名和pojo屬性名之間作一個映射關系。

resultMap:適合使用返回值是自定義實體類的情況

resultType:適合使用返回值得數據類型是非自定義的,即jdk的提供的類型


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM