Closed
Description
For example, I have one common mapper 'CommonUserMapper':
- CommonUserMapper.java (interface)
- CommonUserMapper.xml (mapper xml)
Now I want to add some custom methods without changing CommonUserMapper.
So I write a new mapper called CustomUserMapper extends from CommonUserMapper:
- CustomUserMapper.java (interface)
- CustomUserMapper.xml (mapper xml)
For this working, I can copy all the codes from the CommonUserMapper.xml to CustomUserMapper.xml.
Of course I can reference some sqls by namespace + ids, but I can't reference the whole select, insert, update, delete...right? So I have to write many duplicate codes.
If the mapper xml can be extended, I think to do this work is more easy.
e.g.
<mapper namespace="demo.CustomUserMapper" extends="demo.CommonUserMapper">
<!-- Now I don't have to copy other statements used by
the methods extended from CommonUserMapper.java -->
<!-- I just define the custom statements -->
<select id="findByXxx" parameterType="string" resultType="User">
<!-- findColumn is defined in CommonUserMapper.xml -->
<include refid="findColumn" />
where xxx = #{value}
</select>
...
</mapper>
Metadata
Metadata
Assignees
Type
Projects
Relationships
Development
No branches or pull requests
Activity
emacarron commentedon Apr 30, 2013
Copied from the user mail list:
Current version does not have that option.
Let me try to explain how inheritance works.
When loading the Child type MyBatis does two things:
When executing MyBatis gets the id of the statement by concatenating
the name of the mapper and the called method. So if you call
annotatedParentMethod on Child it will execute the statement name
"Child.annotatedParentMethod"
Note that when using annotations there is no need to load the Parent
to use the method annotatedParentMethod. In fact, if the parent is
loaded a new mapped statement called Parent.annotatedParentMethod will
be created. (with the same exact content than
Child.annotatedParentMethod())
Probably we could change the way MyBatis gets the id of the statement:
already being done)
(Parent.xmlParentMethod)
Unlike with annotated methods, with xml you have to load the Parent
type (or the Parent.xml) so that MyBatis creates the
Parent.xmlParentMethod statement.
akuma commentedon May 1, 2013
OK. Thanks for your patiently explanation.
pinootto commentedon Nov 14, 2013
I am using mybatis version 3.2.3 and I would like to use the mapper extending feature.
My xml code is:
where com.pino.mappers.mybatis.AssetMapper is generated automatically by mybatis generator.
Eclipse gives me the error: Attribute "extends" must be declared for element type "mapper".
How can I resolve this problem? Is this feature working in mybatis version 3.2.3?
themez commentedon May 12, 2014
This issue seems not completely fixed yet. Here's a case:
I add a configuration option on generator to make it generate Mapper interfaces extends a BaseMapper interface
It only query statement in
MyCustomMapper
andBaseMapper
, but of course there's no statement inBaseMapper
namespace.P.S.

hengchengfei commentedon Dec 4, 2014
#Child Dao
package com.self.app.dao;
import com.self.app.dto.ContentDto;
public interface ContentDao extends ContentDtoMapper {
ContentDto getC();
}
#Child Xml
ilkomiliev commentedon Oct 6, 2015
As themez pointed out above this issue is not completed - I've added an additional implementation to check the superinterfaces of the mapperInterface and try to get the statement from there, if the current implementation still returns null - what should be the best way to share it?
fixes #481 Looks for a matching statement defined in super interfaces…
fixes mybatis#481 Looks for a matching statement defined in super int…