1 回答
Qyou
TA貢獻(xiàn)8條經(jīng)驗(yàn) 獲得超4個(gè)贊
Mybatis實(shí)際上隱藏了一個(gè)功能:Mapper.xml可以繼承,這個(gè)在官方文檔中并沒(méi)有提到過(guò),不過(guò)在這個(gè)issue?(commit)里提到過(guò)。
Statement覆蓋
利用Mapper.xml的繼承機(jī)制,我們可以做到ChildMapper覆蓋ParentMapper中select、insert、delete、update。下面舉例說(shuō)明:
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>
規(guī)律可以總結(jié)為:
ParentMapper.xml中有,ChildMapper.xml中沒(méi)有,ChildMapper沿用ParentMapper.xml中的定義
ParentMapper.xml中有,ChildMapper.xml中也有,ChildMapper使用ChildMapper.xml中的定義
ParentMapper.xml中沒(méi)有,ChildMapper.xml中有,ChildMapper使用ChildMapper.xml中的定義
添加回答
舉報(bào)
0/150
提交
取消
