For example, I have one common mapper 'CommonUserMapper':
Now I want to add some custom methods without changing CommonUserMapper.
So I write a new mapper called CustomUserMapper extends from CommonUserMapper:
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>
Copied from the user mail list:
Current version does not have that option.
Let me try to explain how inheritance works.
interface Parent {
@select()
annotatedParentMethod();
xmlParentMethod();
}
interface Child extends Parent {
}
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:
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.
OK. Thanks for your patiently explanation.
I am using mybatis version 3.2.3 and I would like to use the mapper extending feature.
My xml code is:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.pino.mappers.AssetMapper" extends="com.pino.mappers.mybatis.AssetMapper">
</mapper>
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?
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
public interface BaseMapper<T, PK extends Serializable, E> {
int countByExample(E example);
int deleteByExample(E example);
int deleteByPrimaryKey(PK id);
int insert(T record);
int insertSelective(T record);
List<T> selectByExample(E example);
T selectByPrimaryKey(PK id);
int updateByExampleSelective(@Param("record") T record, @Param("example") E example);
int updateByExample(@Param("record") T record, @Param("example") E example);
int updateByPrimaryKeySelective(T record);
int updateByPrimaryKey(T record);
}
//generated mapper
interface MyMapper extends BaseMapper{
}
//my custom mapper
interface MyCustomMapper{
extQuery();
}
It only query statement in MyCustomMapper and BaseMapper, but of course there's no statement in BaseMapper namespace.
P.S.

package com.self.app.dao;
import com.self.app.dto.ContentDto;
public interface ContentDao extends ContentDtoMapper {
ContentDto getC();
}
<resultMap id="ResultMap" type="com.self.app.dto.ContentDto" extends="com.self.app.dao.ContentDtoMapper.BaseResultMap" />
<select id="getC" resultMap="ResultMap">
select * from content where id=1
</select>
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?
Most helpful comment
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
MyCustomMapperandBaseMapper, but of course there's no statement inBaseMappernamespace.P.S.
