Spring Data JPA规范,将条件运算符从相等改为

时间:2019-01-15 15:34:27

标签: java mysql hibernate spring-data-jpa jhipster

我必须修改一个由HTTP-request调用的SQL查询:

SELECT * FROM inventory WHERE interface = 'Ethernet'

对此

SELECT * FROM inventory WHERE interface LIKE '%Ethernet%'

处理HTTP请求的Resource方法如下:

// InventoryResource.java
@GetMapping("/inventories")
@Timed
public ResponseEntity<List<Inventory>> getAllInventories(InventoryCriteria criteria) {
    List<Inventory> page = inventoryQueryService.findByCriteria(criteria);    
    return new ResponseEntity<>(page, HttpStatus.OK);
}

查询服务

// inventoryQueryService.java
@Transactional(readOnly = true)
public List<Inventory> findByCriteria(InventoryCriteria criteria) {
    final Specifications<Inventory> specification = createSpecification(criteria);
    return inventoryRepository.findAll(specification);
}

然后,有一个“条件”构建器:

private Specifications<Inventory> createSpecification(InventoryCriteria criteria) {
    Specifications<Inventory> specification = Specifications.where(null);    
    // ....        
    if (criteria != null) {
        specification = specification.and(buildStringSpecification(criteria.getInterfaceType(), Inventory_.interfaceType));
    }        
    // ....   
    return specification;    
}

在此处定义用于构建查询的字段和方法:

public class InventoryCriteria implements Serializable {

    private static final long serialVersionUID = 1L;

    private LongFilter index;    
    // ...    
    private StringFilter interfaceType;   
    // ...   
    // ...    
    public StringFilter getInterfaceType() {
        return interfaceType;
    }   
    // ...

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("InventoryCriteria [index=");
        builder.append(index); 
        // ...    
        builder.append(", interfaceType=");
        builder.append(interfaceType);    
        // ...    
        builder.append("]");
        return builder.toString();        
    }    
}

所以,我想这个字符串:specification = specification.and(buildStringSpecification(criteria.getInterfaceType(), Inventory_.interfaceType));

应该被修改。但目前尚不清楚。

0 个答案:

没有答案