选择提供程序 Mybatis 错误:找不到参数'arg0'。可用的参数是 [productAttributeDto, param1]



我正在将Mybatis与spring-boot应用程序一起使用。我曾经使用ProviderMethodResolver生成MySql查询。我的应用程序支持mybatis注释处理器和XML处理器。

为了实现这一点,我使用了这种Mybatis配置:

在appition.properties文件中

mybatis.mapper-locations=classpath*:/repository/**/*Repository.xml

和MybatisConfiguration.java

@Configuration
@EnableTransactionManagement
public class MybatisConfiguration {
@Bean
ConfigurationCustomizer mybatisConfigurationCustomizer() {
return new ConfigurationCustomizer() {
@Override
public void customize(org.apache.ibatis.session.Configuration configuration) {
configuration.setMapUnderscoreToCamelCase(true);
configuration.setCacheEnabled(true);
}
};
}
}

有了以上配置,除了@SelectProvider的实现之外,一切(@Select, @Insert, @ResultMap(似乎都可以正常工作。

SelectProvider实现是

VariantPosition.java

@Mapper
public interface VariantRepository {
@SelectProvider(type = VariantSqlProvider.class, method = "getProductAttribute")
VariantDto findByProductAttribute(@Param("productAttributeDto") ProductAttributeDto productAttributeDto);
}

我正在使用org.apache.ibatis.annotations.Param

ProductAttributeDto.java

@Data
public class ProductAttributeDto {
private Integer productId;
Map<String, String> attributes;
}

VariantqlProvider.class

public class VariantSqlProvider implements ProviderMethodResolver {
@SuppressWarnings("unused")
public static String getProductAttribute(final ProductAttributeDto productAttributeDto) {
return new SQL() {{
SELECT("*");
FROM("ec_product_variant AS pv");
if (Objects.nonNull(productAttributeDto.getAttributes())) {
for (Entry<String, String> entry : productAttributeDto.getAttributes().entrySet()) {
if(Objects.nonNull(entry.getValue())) {
INNER_JOIN(
new StringBuilder("ec_attributes AS ")
.append(entry.getKey())
.append(" ON ")
.append(entry.getKey()).append(".id = pv.").append(entry.getKey())
.append(" AND ")
.append(entry.getKey()).append(".value=#{productAttributeDto.attributes.").append(entry.getKey())
.append("}").toString()
);
} else {
WHERE("pv." + entry.getKey() + " IS NULL");
}
}
}
if(Objects.nonNull(productAttributeDto.getProductId())) {
WHERE("pv.product_id = #{productAttributeDto.productId}");
}
}}.toString();
}
}

当我调用findByProductAttribute方法时,我得到一个类似的错误

org.apache.juli.logging.DirectJDKLog: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.builder.BuilderException: Error invoking SqlProvider method 'public static java.lang.String com.ecommerce.app.repository.product.sqlprovider.VariantSqlProvider.getProductAttribute(com.ecommerce.app.model.product.ProductAttributeDto)' with specify parameter 'class org.apache.ibatis.binding.MapperMethod$ParamMap'.  Cause: org.apache.ibatis.binding.BindingException: Parameter 'arg0' not found. Available parameters are [productAttributeDto, param1]] with root cause
org.apache.ibatis.binding.BindingException: Parameter 'arg0' not found. Available parameters are [productAttributeDto, param1]
at org.apache.ibatis.binding.MapperMethod$ParamMap.get(MapperMethod.java:212)
at org.apache.ibatis.builder.annotation.ProviderSqlSource.extractProviderMethodArguments(ProviderSqlSource.java:223)

我期望生成的SQL查询是:

SELECT *
FROM ec_product_variant AS pv 
INNER JOIN ec_attributes AS color ON color.id = pv.color AND color.value=? 
WHERE (pv.size IS NULL AND pv.product_id = ?)

查询基于属性键值对在产品中AttributeD到

此处mybatis正在查找arg0,而不是productAttributeDto。有人能帮上忙吗。我在这里做错了什么?提前谢谢。

错误是由映射程序方法和提供程序方法之间的参数名称不匹配引起的。

  • 映射器方法的参数名称由@Param注释指定,即productAttributeDto
  • 提供程序方法中未指定param名称,因此使用默认名称arg0

这些名称必须匹配。


有两种解决方案。

  1. 在提供程序方法参数上添加@Param注释
  2. 在启用-parameters编译器选项的情况下构建应用程序

解决方案1非常简单。

public static String getProductAttribute(
@Param("productAttributeDto") final ProductAttributeDto productAttributeDto) {

解决方案2取决于如何构建应用程序。

如果您使用Maven,您可能需要在pom.xml中配置Maven编译器插件。例如

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>

使用IDE时,可能需要配置生成设置。

  • Eclipse:https://stackoverflow.com/a/38198699/
  • IntelliJ IDEA:https://stackoverflow.com/a/39232302/

通过这种方式,MyBatis可以从方法签名中检索参数名称,因此您不需要使用@Param注释(不过,如果您愿意,您仍然可以使用@Param(。

因此,mapper方法可以更简单。。。

@SelectProvider(type = VariantSqlProvider.class, method = "getProductAttribute")
VariantDto findByProductAttribute(ProductAttributeDto productAttributeDto);

您的问题中的provider方法应该按原样工作。

相关内容

最新更新