Jasper report可以同时使用connection和bean吗?

Can Jasper report use both connection and bean togehter?

在我的例子中,我需要在 jasper 报告中同时使用 JRBeanCollectionDataSource 和嵌入式查询。

如果我需要连接,我会按如下方式提供报告,

JasperPrint jprint = (JasperPrint) JasperFillManager.fillReport(is, hm, ds.getConnection() );

如果我有一个集合要在报告中显示,请按以下方式提供,

JasperPrint jprint = (JasperPrint) JasperFillManager.fillReport(is, hm, getBeanDataSource());

getBeanDataSource 方法 returns 一个 JRBeanColleactionDataSource 对象包含其中所需的数据。

两者都很好。但在某些情况下,我需要为报告的一部分提供 bean,而另一部分则提供查询。有没有办法做到这一点 ?

是也不是,主报表需要一个数据源,但您可以在报表中使用子报表,如果您想发送数据源,可以通过参数发送。

HashMap<String,Object> hm= new HashMap<String,Object>();
hm.put("datasource", getBeanDataSource());

在 jrxml 中

<parameter name="datasource" 
 class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>

使用这个数据源的子报表应该是这样的

<subreport>
  <reportElement positionType="Float" x="-20" y="28" width="595" height="102" isRemoveLineWhenBlank="true" uuid="84c78233-8870-4563-9138-e166bb6000fd"/>
   <dataSourceExpression><![CDATA[$P{datasource}]]></dataSourceExpression>
   <subreportExpression><![CDATA[$P{your sub report jrxml}]]></subreportExpression>
</subreport>