将 Java Bean 的集合作为数据源传递给 Jasper,如何在 ireports 中进行设计
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/3042975/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing a Collection of Java Beans as DataSource to Jasper, How to design this in ireports
提问by Vaishak Suresh
I need to design a report, that displays the data from a collection(Say List). This list contains multiple POJOs.
我需要设计一个报告,显示集合中的数据(说列表)。此列表包含多个 POJO。
The POJOs are populated by the data access layer of the application. How do I design a report template for this requirement in iReports?
POJO 由应用程序的数据访问层填充。如何在 iReports 中针对此要求设计报告模板?
采纳答案by Vaishak Suresh
Ok! Found the answer. The steps are as below.
行!找到了答案。步骤如下。
- Compile the bean classes and create a JAR file. This needs to have the complete package
 - Add this jar to the LIB folder in ireports
 - Create a factory/wrapper class that has a createBeanCollection method that populates a collection
 - Use this class's top level package as the class path in ireports
 - Use this class as the JavaBean datasource with the method.
 
- 编译 bean 类并创建一个 JAR 文件。这个需要有完整的包
 - 将此 jar 添加到 ireports 中的 LIB 文件夹
 - 创建具有填充集合的 createBeanCollection 方法的工厂/包装器类
 - 使用这个类的顶级包作为 ireports 中的类路径
 - 使用此类作为方法的 JavaBean 数据源。
 
Once all this is done, create a report with the new datasource and in report query, give the FQN on the Java bean and add the desired field.
完成所有这些后,使用新数据源创建报告,并在报告查询中,在 Java bean 上提供 FQN 并添加所需字段。
回答by Bozho
Use JRBeanCollectionDataSourcefor your report.
使用JRBeanCollectionDataSource您的报告。
回答by Dark Army
BankDetailsList list = new BankDetailsList();       
ArrayList<BankDetails> lst = list.getDataBeanList();        
JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(lst);
Here BankDetails is a POJO
这里的 BankDetails 是一个 POJO
public class BankDetails {
    public String bank_name;
    public Account account;
    public String custodian_account;
    public String custodian_name;
    public String agreement_type;
    public double exposure;
    public double collateral;
    public double independant_amount;
        public double net_exposure;
    BankDetails(String b_name, Account acc, String cust_account,
        String cust_name, String agr_type, double expo, double collat,
        double independant_amt, double net_exp) {
        this.bank_name = b_name;
        this.account = acc;
        this.custodian_account = cust_account;
        this.custodian_name = cust_name;
        this.agreement_type = agr_type;
        this.exposure = expo;
        this.collateral = collat;
        this.independant_amount = independant_amt;
        this.net_exposure = net_exp;
    }
    public String getBank_name() {
        return bank_name;
    }
    public void setBank_name(String bank_name) {
        this.bank_name = bank_name;
    }
    public Account getAccount() {
        return account;
    }
    public void setAccount(Account account) {
        this.account = account;
    }
    public String getCustodian_account() {
        return custodian_account;
    }
    public void setCustodian_account(String custodian_account) {
        this.custodian_account = custodian_account;
    }
    public String getCustodian_name() {
        return custodian_name;
    }
    public void setCustodian_name(String custodian_name) {
        this.custodian_name = custodian_name;
    }
    public String getAgreement_type() {
        return agreement_type;
    }
    public void setAgreement_type(String agreement_type) {
        this.agreement_type = agreement_type;
    }
    public double getExposure() {
        return exposure;
    }
    public void setExposure(double exposure) {
        this.exposure = exposure;
    }
    public double getCollateral() {
        return collateral;
    }
    public void setCollateral(double collateral) {
        this.collateral = collateral;
    }
    public double getIndependant_amount() {
        return independant_amount;
    }
    public void setIndependant_amount(double independant_amount) {
        this.independant_amount = independant_amount;
    }
    public double getNet_exposure() {
        return net_exposure;
    }
    public void setNet_exposure(double net_exposure) {
        this.net_exposure = net_exposure;
    }
}
Account POJO:
帐户POJO:
public class Account {
    public int account_id;
    public String account_name;
    Account(int acc_id, String acc_name){
        this.account_id = acc_id;
        this.account_name = acc_name;
    }
    public int getAccount_id() {
        return account_id;
    }
    public void setAccount_id(int account_id) {
        this.account_id = account_id;
    }
    public String getAccount_name() {
        return account_name;
    }
    public void setAccount_name(String account_name) {
        this.account_name = account_name;
    }
}

