前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Mybatis Mapper.xml继承机制

Mybatis Mapper.xml继承机制

作者头像
颇忒脱
发布2018-10-19 14:41:31
4K0
发布2018-10-19 14:41:31
举报
文章被收录于专栏:颇忒脱的技术博客

Mapper.xml继承机制

github地址

Mybatis实际上隐藏了一个功能:Mapper.xml可以继承,这个在官方文档中并没有提到过,不过在这个issue (commit)里提到过。

Statement覆盖

利用Mapper.xml的继承机制,我们可以做到ChildMapper覆盖ParentMapper中selectinsertdeleteupdate。下面举例说明:

Interface:

代码语言:javascript
复制
@MybatisMapper
public interface ParentMapper {

  String selectFoo();

  String selectBar();
}

@MybatisMapper
public interface ChildMapper extends ParentMapper {

  String selectLoo();

}

Mapper.xml:

代码语言:javascript
复制
<mapper namespace="me.chanjar.mybatislearn.inherit.statement.ParentMapper">

  <select id="selectFoo" resultType="String">
    SELECT 'Foo From Parent'
    FROM dual
  </select>

  <select id="selectBar" resultType="String">
    SELECT 'Bar From Parent'
    FROM dual
  </select>

</mapper>

<mapper namespace="me.chanjar.mybatislearn.inherit.statement.ChildMapper">

  <!-- 覆盖 Parent.selectFoo的定义 -->
  <select id="selectFoo" resultType="String">
    SELECT 'Foo From Child'
    FROM dual
  </select>

  <!-- 不覆盖 Parent.selectBar的定义 -->

  <!-- 自己的 Child.selectLoo的定义 -->
  <select id="selectLoo" resultType="String">
    SELECT 'Loo From Child'
    FROM dual
  </select>

</mapper>

规律可以总结为:

  1. ParentMapper.xml中有,ChildMapper.xml中没有,ChildMapper沿用ParentMapper.xml中的定义
  2. ParentMapper.xml中有,ChildMapper.xml中也有,ChildMapper使用ChildMapper.xml中的定义
  3. ParentMapper.xml中没有,ChildMapper.xml中有,ChildMapper使用ChildMapper.xml中的定义

相关代码:Java代码测试代码配置文件

ResultMap覆盖

Mapper.xml继承机制只针对statement有效,对于sqlresultMap是无效的。 如果要在ChildMapper.xml中覆盖这些,必须要先覆盖ParentMapper.xml中的statement,然后让这些statement使用新的sqlresultMap等。

下面举例一个给ITEM表添加字段,但是不修改原来的ItemMapper的例子:

Model:

代码语言:javascript
复制
public class Item {

  private Integer id;
  private String title;
  // setter and getter ...
}

public class ItemEx extends Item {

  private String name;
  // setter and getter ...

}

Interface:

代码语言:javascript
复制
@MybatisMapper
public interface ItemMapper {

  Item getById(@Param("id") Long id);

}
@MybatisMapper
public interface ItemExMapper extends ItemMapper {

}

Mapper.xml:

代码语言:javascript
复制
<mapper namespace="me.chanjar.mybatislearn.inherit.resultmap.ItemMapper">

  <select id="getById" resultMap="Item">
    select
      *
    from item where id = #{id}
  </select>

  <resultMap id="Item" type="me.chanjar.mybatislearn.inherit.resultmap.Item" autoMapping="true">
    <id property="id" column="id" />
  </resultMap>

</mapper>

<mapper namespace="me.chanjar.mybatislearn.inherit.resultmap.ItemExMapper">

  <!-- 如果这里不写一遍,就会用到ItemMapper.getById的定义,resultMap就不会是ItemEx-->
  <select id="getById" resultMap="Item">
    select
    *
    from item where id = #{id}
  </select>

  <resultMap id="Item" type="me.chanjar.mybatislearn.inherit.resultmap.ItemEx" autoMapping="true">
    <id property="id" column="id" />
  </resultMap>

</mapper>

相关代码:Java代码测试代码配置文件

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Mapper.xml继承机制
    • Statement覆盖
      • ResultMap覆盖
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档