0

我想使用带有多个参数的JasperReports创建一个报告,当用户传递所有参数时会正确生成报告,但是当缺少一个参数时不会生成任何内容我使用此请求

  SELECT
     t.*,
     u."name" AS username,
     c."name" AS componentName,
     s."designation" AS statusName,
     pr."name" AS priorityName,
     p."name" AS projectName
FROM
     "component" c INNER JOIN "ticket" t ON c."id" = t."component_id"
     INNER JOIN "personne" u ON t."personne_id" = u."id"
     INNER JOIN "status" s ON t."status_id" = s."id"
     INNER JOIN "priority" pr ON t."priority_id" = pr."id"
     INNER JOIN "project" p ON c."project_id" = p."id"
WHERE
     pr.name = $P{priority}
and u.login = $P{userLogin}
and s.designation = $P{status}
and t.creation_date between $P{start} and $P{end}
and c.name = $P{componenet}

即使缺少一个参数,您能帮我生成报告吗?

4

3 回答 3

0

您可以为每个参数设置默认值或使用此查询:-

     SELECT
 t.*,
 u."name" AS username,
 c."name" AS componentName,
 s."designation" AS statusName,
 pr."name" AS priorityName,
 p."name" AS projectName
FROM
 "component" c INNER JOIN "ticket" t ON c."id" = t."component_id"
 INNER JOIN "personne" u ON t."personne_id" = u."id"
 INNER JOIN "status" s ON t."status_id" = s."id"
 INNER JOIN "priority" pr ON t."priority_id" = pr."id"
 INNER JOIN "project" p ON c."project_id" = p."id"
WHERE
 (pr.name = $P{priority} or $P{priority}  is null)
  and (u.login = $P{userLogin} or $P{userLogin}  is null)
  and (s.designation = $P{status}  or $P{status} is null )
  and t.creation_date between $P{start}  and $P{end}
  and (c.name = $P{componenet} or $P{componenet}  is null)
于 2013-07-16T07:12:54.453 回答
0

你想要外连接吗?或者您可以选择这样的结构:

and ( u.login = $P{userLogin} or $P{userLogin} is null )

对于每一行

于 2013-07-15T21:03:01.177 回答
0

您可以在生成报告之前控制 java 类中的 where 子句。

1.在java中你可以检查参数是否为空,然后做一些小的验证,比如

StringBuffer sb = new StringBuffer();
if(StringUtils.isNotEmpty(priority)){
        sb.append(" AND pr.name = "+priority);
    }

2.为其他所有条件做u.login, s.designation

3.之后你可以通过reportParam.put("sqlQuery", sb.toString());

4.在 ireport 中只需更改您的查询

WHERE
pr.name = $P{priority}
and u.login = $P{userLogin}
and s.designation = $P{status}
and t.creation_date between $P{start} and $P{end}
and c.name = $P{componenet}

WHERE 1=1 $P!{sqlQuery}

现在您不必担心null,因为它会忽略 java 类中的条件。

于 2013-07-16T03:05:19.617 回答