【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>
Report Query
为了填充报表,我们要给reporting engine提供报表数据,或者至少告诉他该怎么取得我们要的数据。通常是要接收到一个dori.jasper.engine.JRDataSource类型的对象,但是也支持JDBC的SQL查询。
<queryString>,这个字段出现在报表参数之后,report fields之前。
XML Syntax
<!ELEMENT queryString (#PCDATA)>
例如:<queryString><![CDATA[SELECT * FROM Orders]]></queryString>
一个很重要的方面是报表参数在这里的应用,例如:
第一种方式是参数像普通的java.sql.PreparedStatement一样使用,规范是:
<queryString>
<!CDATA[
SELECT * FROM Orders WHERE OrderID <= $P{MaxOrderID} ORDER BY ShipCountry
]]>
</queryString>
第二种方式是将一条查询语句作为一个参数传递过来,例如:
<queryString>
<!CDATA[
SELECT * FROM $P!{MyTable} ORDER BY $P!{OrderByClause}
]]>
</queryString>
这样就要求再交给java.sql.PreparedStatemen之前,将参数解析出来。<queryString>$P!{MySQLQuery}</queryString>
jasper, subreport, scriptlet, and webapp use internal SQL有例子
Fields
XML Syntax
<!ELEMENT field (fieldDescription?)>
<!ATTLIST field
name NMTOKEN #REQUIRED
class (java.lang.Object | java.lang.Boolean | java.lang.Byte |
java.util.Date | java.sql.Timestamp | java.lang.Double | java.lang.Float |
java.lang.Integer | java.io.InputStream | java.lang.Long | java.lang.Short |
java.math.BigDecimal | java.lang.String) "java.lang.String"
>
<!ELEMENT fieldDescription (#PCDATA)>
例如:
ColumnName DataType Length
EmployeeID int 4
LastName varchar 50
FirstName varchar 50
HireDate datetime 8
相应应该声明的report fields 如下:
<field name="EmployeeID" class="java.lang.Integer"/>
<field name="LastName" class="java.lang.String"/>
<field name="FirstName" class="java.lang.String"/>
<field name="HireDate" class="java.util.Date"/>
Field Class包含的类型有:java.lang.Object java.lang.Boolean java.lang.Byte java.util.Date java.sql.Timestamp
java.lang.Double java.lang.Float java.lang.Integer java.io.InputStream java.lang.Long java.lang.Short java.math.BigDecimal
这里有一个java.lang.Object类型,当数据源是custom made data sources时,要用这种类型。
有一个附加的属性<fieldDesciption>,这个有时会很有用处,你可以在里面存储key等任何需要的信息,以便在runtime从custom data source取得值,例如:
<field name="PersonName" class="java.lang.String" isForPrompting="true">
<fieldDesciption>PERSON NAME</fieldDesciption>
</field>
Variables
是在report expression建立的一种特殊的对象,可以用来简化报表设计,比如只声明一此次频繁使用的expression,或者在相应expression的上实现个各种各样的计算。
XML Syntax
<!ELEMENT variable (variableExpression?, initialValueExpression?)>
<!ATTLIST variable
name NMTOKEN #REQUIRED
class NMTOKEN "java.lang.String"
resetType (None | Report | Page | Column | Group) "Report"
resetGroup CDATA #IMPLIED
calculation (Nothing | Count | Sum | Average | Lowest | Highest |
StandardDeviation | Variance | System) "Nothing"
>
<!ELEMENT variableExpression (#PCDATA)>
<!ELEMENT initialValueexpression_r(#PCDATA)>
变量声明的位置很重要,后声明的可以引用先声明的。
Variable Name
Variable Class
Reset Type,来指示在报表填充进程中,这个变量的重新加载特性,有五个级别:
There are 5 reset types for a variable:
No Reset: The variable will never be initialized using its initial value expression and will only
contain values obtain by evaluating the variable''''s expression_r(resetType="None").
Report Level Reset: The variable is initialized only once, at the beginning of the report filling
process, with the value returned by the variable''''s initial value expression
(resetType="Report").
Page Level Reset: The variable is reinitialized at the beginning of each new page
(resetType="Page").
Column Level Reset: The variable is reinitialized at the beginning of each new column
(resetType="Column").
Group Level Reset: The variable is reinitialized every time the group specified by the resetGroup
attributes breaks (resetType="Group").
默认的是 resetType="Report".
Reset Group
Calculations
变量可以按着它们相应的表达式的值来实现内建式的计算,类型有:
Calculation Nothing,表示数据源的每次迭代变量都要进行重新加载,只是简单的根据变量的表达式得到值。
Calculation Count,
Calculation Sum
Calculation Average
Calculation Lowest and Highest
Calculation StandardDeviation and Variance
Calculation System
“TODO:具体的各个的含义还要再细看 P36”
例子:这个例子中的每页汇总
<variable name="QuantitySum" class="java.lang.Double" resetType="Page"
calculation="Sum">
<variableExpression>$F{Quantity}</variableExpression>
<initialValueExpression>new Double(0)</initialValueExpression>
</variable>
Built-in Report Variables
一些系统内建的变量,
Variable PAGE_NUMBER,用它可以得到当前的页号,在完成是还可以得到总的页号(利用evaluationTime)。
Variable COLUMN_NUMBER,
Variable REPORT_COUNT,记录的总数
Variable PAGE_COUNT,当页的记录总数
Variable COLUMN_COUNT,当前列的记录总数
Variable COLUMN_COUNT,当声明了一个组的时候,则会自动的声明一个,用来计算当前组的记录数
==Report Sections
建立一个report design的时候,我们需要定义内容和格式,格式的限定是由下面的定义的<title>, <pageHeader>,<columnHeader>, <groupHeader>,
<detail>, <groupFooter>, <columnFoter>,<pageFooter>, <summary>,当定义格式时,用<band>。
XML Syntax
<!ELEMENT band (printWhenExpression?, (line | rectangle | image | staticText
| textField | subreport | elementGroup)*)>
<!ATTLIST band
height NMTOKEN "0"
>
<!ELEMENT printWhenexpression_r(#PCDATA)>
Band Height,制定这个band 的高度,如果发现对象超过这个高度,则编译时会有报警信息。
Skipping Bands,“TODO:不清楚”
Main Report Sections
XML Syntax
<!ELEMENT title (band?)>
<!ELEMENT pageHeader (band?)>
<!ELEMENT columnHeader (band?)>
<!ELEMENT detail (band?)>
<!ELEMENT columnFooter (band?)>
<!ELEMENT pageFooter (band?)>
<!ELEMENT summary (band?)>
Title,要在page header section之前加载,要是想page header section在title section 之前被加载出来,则需要在title section开始之前,
将page header section 中的elements也都拷贝过来,这样他们就可以利用<printWhenExpression>在第一页上suppress真正的page header section,
当然这要有PAGE_NUMBER的支持。
Page Header
This section appears at the top of each page in the generated document.
Column Header
This section appears at the top of each column in the generated document.
Detail
For each record in the data source, the engine will try to generate this section.
Column Footer
This section appears at the bottom of each column in the generated document. It never stretches
downward to acquire the content of its containing text fields and will always remain of declared fixed
height.
Page Footer
This section appears at the bottom of each page in the generated document. Just like the column footer
section above, the page footer never stretches downwards to acquire the content of its containing text
fields and will always remain of declared fixed height.
Summary
This section is generated only once per report and appears at the end of the generated document, but is
not necessarily the last section being generated.
That''''s because in some cases, the column footer or/and page footer of the last page can follow it.
As mentioned in the 4.3 Report Properties paragraph, the summary section can start a new page of its
own, by setting the isSummaryNewPage attribute to "true". Even if this attribute remains false, the
summary section always starts a new page if it does not fit on the remaining space of the last page or if
the report has more than one column and on the last page it has already started a second column.
If the main report sections that we have seen here are not sufficient for what you need, maybe you
should consider introducing supplementary sections like group headers and group footers.
We are now going to see how to group data on the report.
Data Grouping
group report 有三个组件:
group expression;
group header section;
group footer section.
当group expression的值在报表填充时,随着数据库的迭代而变化时,将会有一个group rupture产生,相应的<groupFooter> and <groupHeader>会变化。
值得注意的是,所有用来group的应该是已经经过排序的数据,因为jasperReport 并不进行排序。
XML Syntax
<!ELEMENT group (groupExpression?, groupHeader?, groupFooter?)>
<!ATTLIST group
name NMTOKEN #REQUIRED
isStartNewColumn (true | false) "false"
isStartNewPage (true | false) "false"
isResetPageNumber (true | false) "false"
isReprintHeaderOnEachPage (true | false) "false"
minHeightToStartNewPage NMTOKEN "0"
>
<!ELEMENT groupExpression (#PCDATA)>
<!ELEMENT groupHeader (band?)>
<!ELEMENT groupFooter (band?)>
来源:oschina
链接:https://my.oschina.net/u/107671/blog/38107