【问题标题】:How to pass a parameter of array/collection type at Jaspersoft Studio and use it at query?如何在 Jaspersoft Studio 中传递数组/集合类型的参数并在查询时使用它?
【发布时间】:2019-05-13 14:51:00
【问题描述】:

我正在尝试使用“IN”子句作为参数传递一个值数组以从我的 DW 获取报告。

问题是,当我将参数设置为“Array”或“Collection”时,Jaspersoft Studio Preview 屏幕上没有提示,无法使用。

第一次,我尝试将参数作为字符串传递,其中 $P{convenio} 是一个字符串,传递给完成“IN”:

//Select and joins here...
WHERE ("o"."status_operacao" IN ('EFETUADO', 'SUSPENSO'))
  AND ("c"."codigo" IN ( $P{convenio}))
  AND ("o"."codigo_produto" = 1)

不起作用,返回错误“在')'处缺少EOF”,但没有关闭错误。

最后一个选项是使用 $X{},所以我将 de $P{convenio} 设置为 Array/Collection 类型,并将查询变成这样:

//Select and joins here...
WHERE ("o"."status_operacao" IN ('EFETUADO', 'SUSPENSO'))
  AND ($X{IN, "c"."codigo", convenio})
  AND ("o"."codigo_produto" = 1)

但是现在参数没有提示,报告给我带来了一切。

【问题讨论】:

    标签: jasper-reports jaspersoft-studio


    【解决方案1】:

    JasperReports 引擎支持嵌套类型 - 我们可以使用类型化集合。

    在 JSS 中显示集合 - 使用对话框设置值

    例如,我们可以像这样声明 Collection 类型的参数:

    <parameter name="collectionParam" class="java.util.Collection" nestedType="java.lang.Long"/>
    

    带有类型集合的报告小示例:

    <?xml version="1.0" encoding="UTF-8"?>
    <jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Blank_A4_2" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
        <parameter name="collectionParam" class="java.util.Collection" nestedType="java.lang.Long"/>
        <title>
            <band height="30" splitType="Stretch">
                <textField>
                    <reportElement x="0" y="0" width="550" height="30"/>
                    <textFieldExpression><![CDATA[$P{collectionParam} != null && !$P{collectionParam}.isEmpty() ? $P{collectionParam}.toArray()[0] : "empty"]]></textFieldExpression>
                </textField>
            </band>
        </title>
    </jasperReport>
    

    我们可以像这样在 Jaspersoft Studio (JSS) 中初始化集合:

    第一步——调用参数对话框:

    第二步 - 将元素添加到 Collection

    我们可以通过预览操作检查(构建)报告:

    - 结果我们在表达式的帮助下显示了 Collection 的第一个元素:

    <textFieldExpression><![CDATA[$P{collectionParam} != null && !$P{collectionParam}.isEmpty() ? $P{collectionParam}.toArray()[0] : "empty"]]></textFieldExpression>
    

    使用默认值表达式初始化Collection类型的参数

    我们可以在 defaultValueExpression 的帮助下初始化 Collection 类型的参数,例如使用 java.util.Arrays.asList() 方法:

    <parameter name="collectionParam" class="java.util.Collection">
        <defaultValueExpression><![CDATA[java.util.Arrays.asList("a", "b", "c")]]></defaultValueExpression>
    </parameter>
    

    在查询中使用

    Collection类型的参数可以通过$X{IN, &lt;column_name&gt;, &lt;parameter_name&gt;}表达式在查询时使用。

    例子:

    <parameter name="param" class="java.util.Collection">
        <defaultValueExpression><![CDATA[java.util.Arrays.asList("val")]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[SELECT * FROM table_name c WHERE $X{IN, c.attr, param}
    </queryString>
    

    这个表达式会在运行时被 JasperReports 引擎转换成这样的查询:

    SELECT * FROM table_name c WHERE c.attr IN ('val')
    

    我们也可以在查询中使用 String 类型的参数和 IN 运算符使用$P!{} 表达式。

    例子:

    <parameter name="param" class="java.lang.String">
        <defaultValueExpression><![CDATA["'val1', 'val2'"]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[SELECT * FROM table_name c WHERE c.attr2 IN ($P!{param})
    </queryString>
    

    这个表达式会在运行时被 JasperReports 引擎转换成这样的查询:

    SELECT * FROM table_name c WHERE c.attr2 IN ('val1', 'val2')
    

    【讨论】:

    • 像 SELECT * FROM table_name c WHERE c.attr2 IN ($P!{param}) 一样传递对我有用。谢谢
    猜你喜欢
    • 2020-10-20
    • 1970-01-01
    • 1970-01-01
    • 2022-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-27
    相关资源
    最近更新 更多