abel533 / Mapper

Mybatis Common Mapper - Easy to use
https://mybatis.io
MIT License
7.29k stars 1.62k forks source link

Unable to put different providers in one self-defined common mapper while using annotation-based dynamic routing #906

Closed YuanHao97 closed 3 months ago

YuanHao97 commented 4 months ago

I'm trying to put all different Provider methods in one mapper: BaseSelectProvider.class,BaseUpdateProvider.class, etc

@DatasourceRouting("ds1")
@tk.mybatis.mapper.annotation.RegisterMapper
public interface DS1Mapper<T> {
    @SelectProvider(type = BaseSelectProvider.class, method = "dynamicSQL")
    T selectOne(T record);
    @InsertProvider(type = BaseInsertProvider.class, method = "dynamicSQL")
    int insert(T record);
}

The annotation DatasourceRouting is used to do dynamic routing, so the common mapper need to be put in one mapper. However I got this error: 一个通用Mapper中只允许存在一个MapperTemplate子类。 Is it possible to add more than 1 Provider in one class? As far as I can see from source code, it is possible to add more than one MapperTemplate in tk.mybatis.mapper.mapperhelper.MapperHelper#fromMapperClass.

abel533 commented 4 months ago

可以将两个接口方法定义到不同接口中,然后通过extends继承到一个接口中。

YuanHao97 commented 4 months ago

放两个接口是可以的,但是有两个问题

  1. 没太明白为啥一个类一定要限制一个provider,为啥不都放一起呢
  2. 我这个使用场景,是希望利用注解实现动态路由。如果放多个接口,每个继承的接口都需要加动态路由的注解,感觉这样不是很合适。如果都放一个类,就只用一个注解解决了
YuanHao97 commented 4 months ago

@abel533 帮忙review下pr