【发布时间】:2020-08-15 16:52:35
【问题描述】:
我正在将 Mybatis 与 Spring Boot 应用程序一起使用。我曾经使用 ProviderMethodResolver 生成 MySql 查询的地方。我的应用支持mybatis注解处理器和XML处理器。
为了实现这一点,我使用了这个 Mybatis 配置:
在 appication.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 实现是
VariantRepository.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;
}
VariantSqlProvider.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 = ?)
查询基于属性键值对 在productAttributeDto中
这里mybatis寻找的是arg0而不是productAttributeDto。 任何人都可以帮助解决这个问题。我在这里做错了什么?提前致谢。
【问题讨论】:
-
尝试将
@Param也添加到提供程序方法中,即public static String getProductAttribute(@Param("productAttributeDto") final ProductAttributeDto productAttributeDto) {。 -
@ave 这有帮助。您能否解释一下并创建一个答案以供将来参考。