【Mybatis】 mapper 继承
前言
- mybatis 3.4.6
- 若依(ruoyi): v4.3
Mybatis mapper 繼承問題誤區(qū)
Mybatis mapper 這里指的是java的類。類的繼承,就是類的繼承。
開始我沒意識到是類的繼承,進入到了“ Mybatis 的 XML 映射文件繼承”的誤區(qū)中。因為使用的是 xml 作為映射文件(如果用注解就好理解了),那么一直在想,xml 映射文件改如何繼承。
在嘗試成功之后,才明白,是 mapper 類的繼承。
需要繼承的場景
因為在用若依(ruoyi)的時候,碰到需要 mapper 繼承 問題的,就以這個進行說明。
若依(ruoyi)能夠進行代碼的生成(生成一些比較簡單的功能,實現(xiàn)的功能雖然簡單,但是節(jié)省了不少的工作量),也包括了DAO部分的代碼。我遇到2種情況,需要使用繼承:
怎么做繼承呢?
假設我有一個企業(yè)員工表(表明為:enterprise_staff),那么自動生成的代碼就有:企業(yè)員工對象 EnterpriseStaff、企業(yè)員工Mapper接口 EnterpriseStaffMapper、企業(yè)員工映射關系配置XML EnterpriseStaffMapper.xml。
現(xiàn)在我想添加個功能selectEnterpriseStaffByDutyIds(按照職務查詢員工)。那么,需要繼承企業(yè)員工Mapper接口 EnterpriseStaffMapper,創(chuàng)建新的企業(yè)員工Mapper接口 EnterpriseStaffCustomMapper,代碼如下:
public interface EnterpriseStaffCustomMapper extends EnterpriseStaffMapper{EnterpriseStaff selectEnterpriseStaffByDutyIds(@Param("deptid")Long deptid, @Param("dutyIdsList")Integer[] dutyIdsList);}同時,為Mapper接口 EnterpriseStaffCustomMapper 添加映射關系配置XML EnterpriseStaffCustomMapper.xml,代碼如下:
<mapper namespace="com.office.enterprise.mapper.EnterpriseStaffCustomMapper"><select id="selectEnterpriseStaffByDutyIds" parameterType="map" resultMap="com.office.enterprise.mapper.EnterpriseStaffMapper.EnterpriseStaffResult"><include refid="com.office.enterprise.mapper.EnterpriseStaffMapper.selectEnterpriseStaffVo"/>where departid = #{deptid} and dutyid in <foreach item="item" collection="dutyIdsList" open="(" separator="," close=")">#{item}</foreach></select></mapper>這樣做完,就OK了,經(jīng)測試一切安好。
解釋一下:EnterpriseStaffCustomMapper 繼承自 EnterpriseStaffMapper 這是面向?qū)ο蟮睦^承,沒啥好說的,就按照面向?qū)ο蟮睦^承關系理解就好了。什么子類替代父類,父類類型變量持有子類類型對象,…。
比較難于理解的是EnterpriseStaffCustomMapper.xml,xml也要繼承嗎?EnterpriseStaffCustomMapper.xml 繼承 EnterpriseStaffMapper.xml?不需要繼承!!!千萬不要理解成繼承!千萬不要理解成繼承!千萬不要理解成繼承!
對于映射關系配置XML(EnterpriseStaffCustomMapper.xml和EnterpriseStaffMapper.xml)之間是互不相干的。映射關系配置XML中的元素(比如resultMap節(jié)點、select節(jié)點、insert節(jié)點…)也是獨立的(也僅與namespace有些關系,namespace像java的package概念)。XML中的元素有唯一的標識(namespace+id),通過 namespace+id 被另外一個 XML 使用。比如 <include refid="com.office.enterprise.mapper.EnterpriseStaffMapper.selectEnterpriseStaffVo"/>。
有一種關系將映射關系配置XML中的元素與Mapper接口類(EnterpriseStaffCustomMapper和EnterpriseStaffMapper)關聯(lián)起來。大概就是:xml 的 namespace 對應類的絕對路徑(包路徑+類名),XML 中的元素 id 對應類的方法名。
還可以覆蓋
上個例子。EnterpriseStaffCustomMapper 中覆蓋 EnterpriseStaffMapper 的方法 selectEnterpriseStaffById 對應的SQL語句。如下:
public interface EnterpriseStaffMapper {public EnterpriseStaff selectEnterpriseStaffById(Long id); } public interface EnterpriseStaffCustomMapper extends EnterpriseStaffMapper{// 這里是空的 } <mapper namespace="com.office.enterprise.mapper.EnterpriseStaffMapper"><resultMap type="EnterpriseStaff" id="EnterpriseStaffResult"><result property="id" column="id" />...</resultMap><select id="selectEnterpriseStaffById" parameterType="Long" resultMap="EnterpriseStaffResult">select * from enterprise_staffwhere id = #{id}</select>... </mapper> <mapper namespace="com.office.enterprise.mapper.EnterpriseStaffCustomMapper"><select id="selectEnterpriseStaffById" parameterType="Long" resultMap="EnterpriseStaffResult">select * from enterprise_staffwhere id = #{id} and del_flag=0</select> </mapper>參考
https://www.cnblogs.com/51ctoedu/p/9460617.html
http://www.360doc.com/content/15/0227/10/281812_451158994.shtml
https://segmentfault.com/a/1190000012470056
https://github.com/mybatis/mybatis-3/issues/35
https://github.com/mybatis/mybatis-3/commit/4b465eb36f499607a490c8f784504be108a26cd3
總結(jié)
以上是生活随笔為你收集整理的【Mybatis】 mapper 继承的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 突聋后耳鸣多久能消失
- 下一篇: 【若依(ruoyi)】表格实现toolt