日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【Mybatis】 mapper 继承

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

前言

  • 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種情況,需要使用繼承:

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

    假設我有一個企業(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

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

    總結(jié)

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

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。