单个jasper文档中的多个查询 [英] Multiple queries in a single jasper document
问题描述
到目前为止,jasper-reports一直受到限制。
我只能在每个文档中写一个数据查询。
当我需要编写另一个查询时我必须创建一个子报表,传递它所需的参数等等。
Until now I have always been constrained by one thing with jasper-reports.
I can only write one data query in each document.
When I need to write another query I must create a subreport, pass its needed parameters and so on.
但我绝对不相信这是做到这一点的好方法。
But I'm definitely not convinced that it's the good way to do it.
那么是否有另一种方法可以在一个jasper文档中触发多个数据查询?
So is there another way to fire multiple data queries in a single jasper document?
推荐答案
可以使用 subDataset
和 datasetRun
从单个报告中使用执行多个查询。行为就像将一个或多个子报表嵌入到单个报表文件中一样。
It is possible to use execute multiple queries from a single report by using a subDataset
and datasetRun
. The behaviour is like having one or more subreports embedded into a single report file.
定义一个这样的子数据集:
Define a subDataset like this:
<subDataset name="dataset1">
<parameter name="someParam" class="java.lang.String"/>
<queryString><![CDATA[SELECT column1, column2 FROM table1 WHERE column1=$P!{someParam}]]></queryString>
<field name="column1" class="java.lang.String"/>
<field name="column2" class="java.lang.String"/>
</subDataset>
子数据集可以像报表一样包含参数,字段,变量和组。每个子数据集都可以拥有自己的查询,并且报表可以包含任意数量的子数据集。
Subdatasets can have parameters, fields, variables and groups just like a report can. Each subdataset can have it's own query, and the report can have as many subdatasets as you want.
要使用子数据集,您需要定义 datasetRun
。这只能在特定元素内完成:图表,交叉表,表格和列表。我们将使用一个列表,因为它的行为几乎与另一个细节带一样。
To use a subdataset, you need to define a datasetRun
. This can only be done inside particular elements: charts, crosstabs, tables and lists. We will use a list as it behaves almost exactly like another detail band.
此代码定义了一个使用我们的子数据集的列表:
This code defines a list that uses our subdataset:
<componentElement>
<reportElement x="0" y="0" width="100" height="40"/>
<jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
<datasetRun subDataset="dataset1">
<datasetParameter name="someParam"><datasetParameterExpression><![CDATA["some value for column 1"]]></datasetParameterExpression></datasetParameter>
</datasetRun>
<jr:listContents height="40">
<textField>
<reportElement x="0" y="0" width="100" height="20"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{column1}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="0" y="20" width="100" height="20"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{column2}]]></textFieldExpression>
</textField>
</jr:listContents>
</jr:list>
</componentElement>
一些注释:
-
jr:listContents
元素类似于细节band元素。您可以放置几乎所有其他元素。
The
jr:listContents
element is analogous to a detail band element. You can place almost any other elements inside.
datasetRun
元素非常类似于子报表元素。它可以在里面有一个 dataSourceExpression
或 connectionExpression
,这将改变数据的来源。如果这两个都不存在,则使用报告数据源。
The datasetRun
element is much like a subreport element. It can have a dataSourceExpression
or connectionExpression
inside, which will change where the data comes from. If neither of these are present, the report datasource is used.
许多datasetRuns可以使用相同的子数据集,因此您可以轻松地多次运行查询具有不同的参数。
The same subDataset can be used by many datasetRuns, so you can easily run a query multiple times with different parameters.
这篇关于单个jasper文档中的多个查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!