Qyou
Mybatis实际上隐藏了一个功能:Mapper.xml可以继承,这个在官方文档中并没有提到过,不过在这个issue (commit)里提到过。Statement覆盖利用Mapper.xml的继承机制,我们可以做到ChildMapper覆盖ParentMapper中select、insert、delete、update。下面举例说明:Interface:@MybatisMapperpublic interface ParentMapper { String selectFoo(); String selectBar();
}@MybatisMapperpublic interface ChildMapper extends ParentMapper { String selectLoo();
}Mapper.xml:<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>规律可以总结为:ParentMapper.xml中有,ChildMapper.xml中没有,ChildMapper沿用ParentMapper.xml中的定义ParentMapper.xml中有,ChildMapper.xml中也有,ChildMapper使用ChildMapper.xml中的定义ParentMapper.xml中没有,ChildMapper.xml中有,ChildMapper使用ChildMapper.xml中的定义