我想用带有多个参数的 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}
即使错过了一个参数,请帮我生成报告吗?
答案 0 :(得分:0)
and ( u.login = $P{userLogin} or $P{userLogin} is null )
每行
答案 1 :(得分:0)
在生成报告之前,您可以控制java类中的where子句。
1.在java中你可以检查参数是否为null,然后进行小验证,如
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类中的条件。
答案 2 :(得分: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)