查询在MySql中工作正常,但不返回JdbcTemplate的记录

时间:2017-07-18 14:35:59

标签: spring jdbctemplate

我有以下服务方法:

public Page<ProductSalesReportLineDto> getReport(
        Unit unit, Organization customer, Organization supplier, Date startDate, Date endDate, Pageable pageable
        ){

    List<ProductSalesReportLineDto> lines = new ArrayList<ProductSalesReportLineDto>();

    String sql = "SELECT product.id, product.code as code, product.barcode as barcode, product_name.name__text AS name, " +
            "SUM(order_line.quantity) as quantity, SUM(order_line.commissioned_price_price * order_line.quantity) as price " +
            "FROM product LEFT JOIN order_line ON order_line.product_id = product.id " +
            "JOIN `order` ON order_line.order_id = order.id " +
            "JOIN product_name ON product_name.product_id = product.id " +
            "WHERE product_name.name__locale = \"en-US\" " +
            "AND (:customer is null OR :customer='' OR `order`.customer_id = :customer) " +
            "AND (:supplier is null OR :supplier='' OR `order`.supplier_id = :supplier) " +
            "AND (:startDate is null OR :startDate='' OR `order`.date >= :startDate) " +
            "AND (:endDate is null OR :endDate='' OR `order`.date <= :endDate) " +
            "AND (:unit is null OR :unit='' OR product.unit = :unit) " +
            "GROUP BY product.id, product_name.name__text " +
            "LIMIT :offset, :pageSize";

    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("offset", pageable.getOffset());
    parameters.put("pageSize", pageable.getPageSize());
    parameters.put("customer", customer != null ? customer.id : null);
    parameters.put("supplier", supplier != null ? supplier.id : null);
    parameters.put("startDate", startDate);
    parameters.put("endDate", endDate);
    parameters.put("unit", unit);

    List<Map<String, Object>> rows = namedParameterJdbcTemplate.queryForList(sql, parameters);

    for (Map<String, Object> row : rows) {

        ProductSalesReportLineDto line = new ProductSalesReportLineDto();
        line.productName = row.get("name") != null ? row.get("name").toString() : "";
        line.productCode = row.get("code") != null ? row.get("code").toString() : "";
        line.productBarcode = row.get("barcode") != null ? row.get("barcode").toString() : "";
        line.quantity = row.get("quantity") != null ? Double.parseDouble(row.get("quantity").toString()) : 0;
        line.price = row.get("price") != null ? Double.parseDouble(row.get("price").toString()) : 0;
        lines.add(line);

    }

    Page<ProductSalesReportLineDto> page = new PageImpl<ProductSalesReportLineDto>(lines);
    return page;

}

参数来自@RestController方法:

public @ResponseBody
    ResponseEntity<?> get(
            @RequestParam(value = "unit", required = false) Unit unit,
            @RequestParam(value = "customer", required = false) Organization customer,
            @RequestParam(value = "supplier", required = false) Organization supplier,
            @DateTimeFormat(pattern = "yyyyy-MM-dd") @RequestParam(value = "startDate", required = false) Date startDate,
            @DateTimeFormat(pattern = "yyyyy-MM-dd") @RequestParam(value = "endDate", required = false) Date endDate,
            Pageable pageable
    )

UnitProduct上的枚举类型字段:

@Enumerated(EnumType.STRING)
public Unit unit;
public enum Unit {

    ROLL("ROLL", "ROLL", AbstractUnit.ONE),
    METRE("METRE", Units.METRE.getSymbol(), Units.METRE),
    KILOGRAM("KILOGRAM", Units.KILOGRAM.getSymbol(), Units.KILOGRAM),
    PIECE("PIECE", "PIECE", AbstractUnit.ONE),
    SAMPLE("SAMPLE", "SAMPLE", AbstractUnit.ONE);

我无法使用此查询按单位检索产品。使用MySql,查询工作正常,并返回记录。例如:

SELECT product.id, product.code as code, product.barcode as barcode, product_name.name__text AS name, SUM(order_line.quantity) as quantity, SUM(order_line.commissioned_price_price) as price FROM product LEFT JOIN order_line ON order_line.product_id = product.id JOIN `order` ON order_line.order_id = `order`.id JOIN product_name ON product_name.product_id = product.id WHERE product_name.name__locale = "en-US" AND (null is null OR null='' OR `order`.customer_id = null) AND (null is null OR null='' OR `order`.supplier_id = null) AND (null is null OR null='' OR `order`.date >= null) AND (null is null OR null='' OR `order`.date <= null) AND ("ROLL" is null OR "ROLL"='' OR product.unit = "ROLL") GROUP BY product.id, product_name.name__text

此查询返回一行。当我通过rest接口和NamedParameterJdbcTemplate运行相同的查询时,它不返回任何记录。任何人都可以告诉我缺少什么吗?

以下代码也返回一行:

String sql2 = "SELECT * FROM product WHERE product.unit = :unit";
Map<String, Object> parameters2 = new HashMap<String, Object>();
parameters2.put("unit", "ROLL");
List<Map<String, Object>> rows2 = namedParameterJdbcTemplate.queryForList(sql2, parameters2);

0 个答案:

没有答案