JasperReports:如果使用带参数的subDataset,则IN子句为空

时间:2016-10-09 17:45:33

标签: java jasper-reports

为什么以下查询不会在JasperReports中返回所需的结果。

SELECT bbd.Bill_Id, bbd.ItemName, bbd.ItemQuantity, bbd.ItemPrice, bbd.Date FROM `borrowerbillsdetails` bbd JOIN `borrowerbills` as bb ON bbd.`Bill_Id` = bb.`Bill_Id` WHERE $X{IN ,Bill_Id, billIds}

它只返回3个结果,而在参数列表中我传递了7个ID并且所有ID都有数据库表中的数据。

我传递参数的控制器代码:

String cnic = txtCnic.getText();
String borrowerHistorypath = "/home/JRXML/BorrowerHistory.jrxml";
Connection con = null;
try {
    con = DBConnection.getConnection();

    List<BorrowerBill> borrowerBillLists = borrowerBillService.getAllBillsByCnic(cnic);
    List<Integer> billIds = new ArrayList<>();
    for(BorrowerBill borrowerBill:borrowerBillLists){
        billIds.add(borrowerBill.getBillId());
    }

    FileInputStream  fileInputStream = new FileInputStream(borrowerHistorypath);
    BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
    Map map = new HashMap();
    map.put("cnic", cnic);
    map.put("billIds",billIds);

    JasperReport jasperReport = (JasperReport) JasperCompileManager.compileReport(bufferedInputStream);
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, map, con);

    JasperViewer jasperViewer = new JasperViewer(jasperPrint, false);
    jasperViewer.viewReport(jasperPrint,false);

相关的jasper报告Jrxml文件如下(适用于BorrowerBillItems)

的数据集

<subDataset name="BillItemsDataSet" uuid="39900337-1c6a-403d-a889-82ee3ca1fd8a">
    <parameter name="billIds" class="java.util.List"/>
    <queryString language="SQL">
        <![CDATA[SELECT bbd.Bill_Id, bbd.ItemName, bbd.ItemQuantity, bbd.ItemPrice, bbd.Date FROM `borrowerbillsdetails` bbd JOIN `borrowerbills` as bb ON bbd.`Bill_Id` = bb.`Bill_Id` WHERE $X{IN ,Bill_Id, billIds}]]>
    </queryString>
    <field name="Bill_Id" class="java.lang.Integer">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="ItemName" class="java.lang.String">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="ItemQuantity" class="java.lang.Double">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="ItemPrice" class="java.lang.Double">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="Date" class="java.sql.Date">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
</subDataset>

<jr:table 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="BillItemsDataSet" uuid="ccd44e17-2c5d-417c-b4a7-554d197ce483">
        <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
    </datasetRun>
    <jr:column width="113" uuid="44bd8219-2732-4853-bf2e-960be7c528be">
        <jr:tableHeader style="table 5_TH" height="30" rowSpan="1">
            <staticText>
                <reportElement x="0" y="0" width="113" height="30" uuid="bebb3df5-79aa-4905-b478-6995f7c53f05"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font size="14" isBold="true"/>
                </textElement>
                <text><![CDATA[S No]]></text>
            </staticText>
        </jr:tableHeader>
        <jr:detailCell style="table 8_TD" height="20" rowSpan="1">
            <textField>
                <reportElement x="0" y="0" width="113" height="20" uuid="5a44f3b8-f7e4-48ce-bf0a-fc2ca083a979"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font size="14" isBold="false"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{Bill_Id}]]></textFieldExpression>
            </textField>
        </jr:detailCell>
    </jr:column>
    <jr:column width="135" uuid="c76aecbd-16aa-4ca2-90c9-613c7e5abee1">
        <jr:tableHeader style="table 5_TH" height="30" rowSpan="1">
            <staticText>
                <reportElement x="0" y="0" width="135" height="30" uuid="6441c557-38b8-47dc-bfdc-e2d476da830c"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font size="14" isBold="true"/>
                </textElement>
                <text><![CDATA[Item Name]]></text>
            </staticText>
        </jr:tableHeader>
        <jr:detailCell style="table 8_TD" height="20" rowSpan="1">
            <textField>
                <reportElement x="0" y="0" width="135" height="20" uuid="5a44f3b8-f7e4-48ce-bf0a-fc2ca083a979"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font size="14" isBold="false"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{ItemName}]]></textFieldExpression>
            </textField>
        </jr:detailCell>
    </jr:column>
    <jr:column width="118" uuid="a98065a6-a07d-4bda-9e08-6d81552ce2ff">
        <jr:tableHeader style="table 5_TH" height="30" rowSpan="1">
            <staticText>
                <reportElement x="0" y="0" width="118" height="30" uuid="6928b4cb-519c-4020-930f-44ddaf1ddd6a"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font size="14" isBold="true"/>
                </textElement>
                <text><![CDATA[Quantity]]></text>
            </staticText>
        </jr:tableHeader>
        <jr:detailCell style="table 8_TD" height="20" rowSpan="1">
            <textField>
                <reportElement x="0" y="0" width="118" height="20" uuid="5a44f3b8-f7e4-48ce-bf0a-fc2ca083a979"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font size="14" isBold="false"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{ItemQuantity}]]></textFieldExpression>
            </textField>
        </jr:detailCell>
    </jr:column>
    <jr:column width="119" uuid="fdeb9dac-b4ec-4cf1-936e-ac3c70846b61">
        <jr:tableHeader style="table 5_TH" height="30" rowSpan="1">
            <staticText>
                <reportElement x="0" y="0" width="119" height="30" uuid="f6afc863-f4f1-49d6-97ce-13bf049013f8"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font size="14" isBold="true"/>
                </textElement>
                <text><![CDATA[Price]]></text>
            </staticText>
        </jr:tableHeader>
        <jr:detailCell style="table 8_TD" height="20" rowSpan="1">
            <textField>
                <reportElement x="0" y="0" width="119" height="20" uuid="5a44f3b8-f7e4-48ce-bf0a-fc2ca083a979"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font size="14" isBold="false"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{ItemPrice}]]></textFieldExpression>
            </textField>
        </jr:detailCell>
    </jr:column>
    <jr:column width="145" uuid="476a5c33-5b9a-4c33-8366-3027b490cab6">
        <jr:tableHeader style="table 5_TH" height="30" rowSpan="1">
            <staticText>
                <reportElement x="0" y="0" width="145" height="30" uuid="235f9670-1447-4079-a00a-5fe046d68514"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font size="14" isBold="true"/>
                </textElement>
                <text><![CDATA[Date]]></text>
            </staticText>
        </jr:tableHeader>
        <jr:detailCell style="table 8_TD" height="20" rowSpan="1">
            <textField>
                <reportElement x="0" y="0" width="145" height="20" uuid="5a44f3b8-f7e4-48ce-bf0a-fc2ca083a979"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font size="14" isBold="false"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{Date}]]></textFieldExpression>
            </textField>
        </jr:detailCell>
    </jr:column>
</jr:table>

这是我得到的结果。

借款人票据项目是主要关注点。

enter image description here

On调试从上面的查询生成的查询是 当我指定jasper文件时,在调试模式下没有打印参数。

SQL query string: SELECT bbd.Bill_Id, bbd.ItemName, bbd.ItemQuantity, bbd.ItemPrice, bbd.Date FROM `borrowerbillsdetails` bbd JOIN `borrowerbills` as bb ON bbd.`Bill_Id` = bb.`Bill_Id` WHERE 0 = 0

而对于其他人,它会创建正确的查询,如

SQL query string: Select * from borrowerbills where cnic = ?
   2016-10-09 22:20:10 DEBUG JRJdbcQueryExecuter:509 - Parameter #1 (cnic of type java.lang.String): 1610186922393

1 个答案:

答案 0 :(得分:4)

您的代码正在传递主数据集的值。您应该将参数的值传递给 subDataset

在您的情况下,代码将是:

<jr:table 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="BillItemsDataSet" uuid="ccd44e17-2c5d-417c-b4a7-554d197ce483">
        <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
        <datasetParameter name="billIds">
            <datasetParameterExpression><![CDATA[$P{billIds}]]></datasetParameterExpression>
        </datasetParameter>
    </datasetRun>

或者您可以借助$P{REPORT_PARAMETERS_MAP}

传递所有参数
<jr:table 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="BillItemsDataSet" uuid="ccd44e17-2c5d-417c-b4a7-554d197ce483">
        <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
        <parametersMapExpression><![CDATA[$P{REPORT_PARAMETERS_MAP}]]></parametersMapExpression>
    </datasetRun>