mybatis继承mapper接口扩展多层dao接口
在MyBatis中,继承Mapper接口可以方便地使用已经定义好的通用CURD方法。而要扩展多层的DAO接口,则可以通过继承已经继承了Mapper接口的上一层接口来实现。
下面是一个示例代码:
- 定义一个通用的Mapper接口,例如BaseMapper:
public interface BaseMapper<T, ID> extends Mapper<T> {
// 这里可以自定义一些通用的方法
}
- 定义第一层扩展的DAO接口,例如UserMapper:
public interface UserMapper extends BaseMapper<User, Long> {
// 这里可以添加一些与User相关的特定方法
}
- 定义第二层扩展的DAO接口,例如UserExtMapper:
public interface UserExtMapper extends UserMapper {
// 这里可以添加一些与User扩展相关的特定方法
}
- 在MyBatis的配置文件中,将第二层扩展的DAO接口注册为Mapper:
<mappers>
<mapper class="com.example.dao.UserExtMapper"/>
</mappers>
这样,在使用UserExtMapper接口时,就可以同时使用BaseMapper和UserMapper的方法,以及自定义的UserExtMapper方法。
注意:在使用多层扩展的DAO接口时,需要保证每一层的接口都继承上一层的接口,以便继承上一层接口的方法
原文地址: http://www.cveoy.top/t/topic/i1RN 著作权归作者所有。请勿转载和采集!