日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

【Mybatis】 mapper 继承

發布時間:2024/9/19 68 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Mybatis】 mapper 继承 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言

  • mybatis 3.4.6
  • 若依(ruoyi): v4.3

Mybatis mapper 繼承問題誤區

Mybatis mapper 這里指的是java的類。類的繼承,就是類的繼承。

開始我沒意識到是類的繼承,進入到了“ Mybatis 的 XML 映射文件繼承”的誤區中。因為使用的是 xml 作為映射文件(如果用注解就好理解了),那么一直在想,xml 映射文件改如何繼承。

在嘗試成功之后,才明白,是 mapper 類的繼承。

需要繼承的場景

因為在用若依(ruoyi)的時候,碰到需要 mapper 繼承 問題的,就以這個進行說明。

若依(ruoyi)能夠進行代碼的生成(生成一些比較簡單的功能,實現的功能雖然簡單,但是節省了不少的工作量),也包括了DAO部分的代碼。我遇到2種情況,需要使用繼承:

  • 在快速迭代時,有些功能的表結構會經常變化。這就導致對應的DAO也有經常的修改。為了能夠保留代碼生成的便利性,就需要將追加的功能與自動生成的功能分開(放一起就會導致,在表結構變化時,不能使用新生成的代碼覆蓋原來的代碼)。這就需要用到繼承,在生成的代碼上進行擴展。
  • 在添加表關聯查詢(一對多、多對多)功能時,不希望修改原來的代碼。因為,對于關聯關系,有的時候需要關聯后的結果,有的時候不需要,所以原來的代碼要保留。這就需要用到繼承,在生成的代碼上進行擴展。
  • 怎么做繼承呢?

    假設我有一個企業員工表(表明為:enterprise_staff),那么自動生成的代碼就有:企業員工對象 EnterpriseStaff、企業員工Mapper接口 EnterpriseStaffMapper、企業員工映射關系配置XML EnterpriseStaffMapper.xml。

    現在我想添加個功能selectEnterpriseStaffByDutyIds(按照職務查詢員工)。那么,需要繼承企業員工Mapper接口 EnterpriseStaffMapper,創建新的企業員工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了,經測試一切安好。

    解釋一下:EnterpriseStaffCustomMapper 繼承自 EnterpriseStaffMapper 這是面向對象的繼承,沒啥好說的,就按照面向對象的繼承關系理解就好了。什么子類替代父類,父類類型變量持有子類類型對象,…。

    比較難于理解的是EnterpriseStaffCustomMapper.xml,xml也要繼承嗎?EnterpriseStaffCustomMapper.xml 繼承 EnterpriseStaffMapper.xml?不需要繼承!!!千萬不要理解成繼承!千萬不要理解成繼承!千萬不要理解成繼承!

    對于映射關系配置XML(EnterpriseStaffCustomMapper.xml和EnterpriseStaffMapper.xml)之間是互不相干的。映射關系配置XML中的元素(比如resultMap節點、select節點、insert節點…)也是獨立的(也僅與namespace有些關系,namespace像java的package概念)。XML中的元素有唯一的標識(namespace+id),通過 namespace+id 被另外一個 XML 使用。比如 <include refid="com.office.enterprise.mapper.EnterpriseStaffMapper.selectEnterpriseStaffVo"/>。

    有一種關系將映射關系配置XML中的元素與Mapper接口類(EnterpriseStaffCustomMapper和EnterpriseStaffMapper)關聯起來。大概就是: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

    與50位技術專家面對面20年技術見證,附贈技術全景圖

    總結

    以上是生活随笔為你收集整理的【Mybatis】 mapper 继承的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。