如何在 MyBatis 映射器接口中为整个方法设置 class 提供者类型
How to set class provider type for whole methods in MyBatis mapper interface
我有接口 personMapper 和这样的方法。
public interface EmployeeMapper {
@SelectProvider(type = EmployeeMapperImpl.class, method = "getAll")
List<Employee> getAll();
//similar methods
}
而且我还有一个 class 用于整个界面
public class EmployeeMapperImpl{
public String getAll() {
return new SQL().
SELECT("*").
FROM(Employee.class.getSimpleName()).toString();
}
}
我想知道是否可以,如何标记与定义的class关联的接口,以摆脱在每个方法中写"type=EmployeeMapperImpl"?并且完全去掉 method = "getAll" 的写法,因为它的标题在 class 和界面中是一样的。我想要这样的东西
@ProviderForAllMethods("EmployeeMapperImpl")
public interface EmployeeMapper {
//and methods are associeted by their names(the same in class and constructor)
List<Employee> getAll();
//similar methods
}
感谢您的帮助
- 如果您使用的是 MyBatis 3.5.1 或更高版本,则可以省略
method
。
- 目前无法省略
type
(当前最新版本是 3.5.4)。
- 注释映射器接口本身在技术上很困难。
要省略 method
,您的 SQL 提供商 class 需要实施 ProviderMethodResolver
接口。
如您所料,默认实现搜索与接口方法同名的提供者方法。
public class EmployeeMapperImpl implements ProviderMethodResolver {
我们还在 3.5.2 版本中将 SQL 提供程序注释的 type
属性作为 value
的别名,因此您的映射器方法可以更简单一些。
public interface EmployeeMapper {
@SelectProvider(EmployeeMapperImpl.class)
List<Employee> getAll();
我有接口 personMapper 和这样的方法。
public interface EmployeeMapper {
@SelectProvider(type = EmployeeMapperImpl.class, method = "getAll")
List<Employee> getAll();
//similar methods
}
而且我还有一个 class 用于整个界面
public class EmployeeMapperImpl{
public String getAll() {
return new SQL().
SELECT("*").
FROM(Employee.class.getSimpleName()).toString();
}
}
我想知道是否可以,如何标记与定义的class关联的接口,以摆脱在每个方法中写"type=EmployeeMapperImpl"?并且完全去掉 method = "getAll" 的写法,因为它的标题在 class 和界面中是一样的。我想要这样的东西
@ProviderForAllMethods("EmployeeMapperImpl")
public interface EmployeeMapper {
//and methods are associeted by their names(the same in class and constructor)
List<Employee> getAll();
//similar methods
}
感谢您的帮助
- 如果您使用的是 MyBatis 3.5.1 或更高版本,则可以省略
method
。 - 目前无法省略
type
(当前最新版本是 3.5.4)。 - 注释映射器接口本身在技术上很困难。
要省略 method
,您的 SQL 提供商 class 需要实施 ProviderMethodResolver
接口。
如您所料,默认实现搜索与接口方法同名的提供者方法。
public class EmployeeMapperImpl implements ProviderMethodResolver {
我们还在 3.5.2 版本中将 SQL 提供程序注释的 type
属性作为 value
的别名,因此您的映射器方法可以更简单一些。
public interface EmployeeMapper {
@SelectProvider(EmployeeMapperImpl.class)
List<Employee> getAll();