有如下 mapper:
public interface PeopleMapper extends BaseMapper<People> {}
现在我只能写成这样:
List<SelectAllMapper> mappers = Util.getMappers(SelectAllMapper.class, People.class);
这个工具函数意思是:找到全部的以 People 为实体类,并实现了 SelectAllMapper 接口的 mapper:
public static <T> List<T> getMappers(Class<T> mapperClass, Class<?> entityClass){...}
但我想做到这样:
List<SelectAllMapper<? extends People>> mappers = Util.getMappers(SelectAllMapper.class, People.class);
这样就不需要我之后再强制转换类型:
(List<People>) mapper.selectAll()
但不知道该怎么声明这个函数,有木有大佬教教我
1
mk0114 OP `public static <T,R> List<T<R>> getMappers(Class<T> mapperClass, Class<R> entityClass) {...}`
这样明显是不对的 |
2
mk0114 OP public static <T<R>,R> List<T> getMappers(Class<T> mapperClass, Class<R> entityClass) {...}
这样也是不行 |
3
xingda920813 2019-05-05 18:19:26 +08:00
public static <T extends BaseMapper<R>, R> List<T> getMappers(Class<T> mapperClass) {...}
List<? extends BaseMapper<People>> mappers = getMappers(new BaseMapper<People>() {}.getClass()); |
4
mk0114 OP @xingda920813 BaseMapper 是 SelectAllMapper 的子接口,这样写应该是不行的。我怀疑是不是 java 不可能做到这样
|
5
HuHui 2019-05-05 19:16:24 +08:00
用我的理解写了你的需求,感觉没你的复杂
``` public class People { } public interface SelectAllMapper<T> extends BaseMapper<T> { List<T> selectAll(); } public interface PeopleMapper extends BaseMapper<People> { } public static <T> List<SelectAllMapper<T>> getMappers(Class<T> clazz) { return null; } List<SelectAllMapper<People>> mappers = Util.getMappers(People.class); List<People> list = mappers.get(0).selectAll(); ``` |
6
billlee 2019-05-05 22:09:51 +08:00
换 scala (逃
|