create Bean Data Sources

Jasper report创建bean数据源的方法

The bean data source type is a key extension because it allows you to make use of any custom or exotic data that you might need to report on. Bean data sources serve as a bridge between a Spring-defined bean and a JasperReport. The Spring bean is responsible for providing the data or parameters that fill the report.

To use a bean data source, you must first configure the underlying Spring bean and make it available in the server’s web application context. For example, you would add a bean definition to one of the WEB-INF/applicationContext*.xml files.

The bean must resolve to a ReportDataSourceService instance, either directly or by way of a factory no-argument method. You can use any Spring instantiation method (for example, a constructor or factory) and bean scope (for example, singleton or prototype) for the data source service bean.

The ReportDataSourceService instance is responsible for supplying data source parameters to the JasperReport. Custom ReportDataSourceServiceimplementations can follow two approaches:

  If the implementation can provide the data to be used to fill a report, it needs to wrap the data into a suitable JRDataSource implementation and pass the data using the REPORT_DATA_SOURCE report parameter.
  If the data comes from the report query by way of a JasperReports query executor, the data source service must set values for the connection parameters defined by the query executor. The connection parameters are usually obtained from the properties of the data source service instance.

For example, you could implement a Hibernate data source service that would be injected in a session factory. The factory would create a Hibernate session that would be passed as a value for the HIBERNATE_SESSION parameter. The JasperReports Hibernate query executor then uses the parameter to run the HQL report query.

The ReportDataSourceService interface contains two methods: setReportParameterValues and closeConnection. The former provides data and connection parameter values; the latter is required to close and release any resources or connections created during the call to setReportParameterValues.

Once the data source service bean is available through Spring, you can add the bean data source to the repository.

To create a bean data source:

  1. Log on as an administrator.
  2. Click View > Repository, expand the folder tree, and right-click a folder to select Add Resource > Data Source from the context menu. Alternatively, you can select Create > Data Source from the main menu on any page and specify a folder location later. If you installed the sample data, the suggested folder is Data Sources.

The Add Data Source page appears, as shown in Bean Data Source Page.

  3. In the Type field, select Bean Data Source.

The information on the page changes to reflect what’s needed to define a bean data source.

  4. Fill in the required fields, along with any optional information.

If the data source service is to be instantiated through a factory method of the Spring bean, you should also enter the name of the method.

  5. Click Test Connection to validate the data source.

If the validation fails, ensure that the values you entered are correct and that the bean is in the classpath.

  6. When the test is successful, click Save.
上面的是在ireport安装部署的过程,下面是代码

代码:

实体Bean,用来展示字段

package com.isprint.report;

public class ReportOneBean {

	private String name;	//申请人名称
	private String userId;	//申请人ID
	private String organization; //人员组织机构
	private String bizRoleCode;  //岗位ID
	private String bizRoleName;  //岗位名称
	private String resource;   //业务系统
	private String itRoleCode;  //授权角色id
	private String itRoleName;  //授权角色
	private String orgScope;      //机构维度
	private String productScope;  //产品维度
	private String resourceScope; //系统维度
	private String zhangTaoScope; //账户维度
	private String approveName;   //审批人
	private String approveReason; //审批意见
	private String createTime;	//申请时间
	private String processId;	//任务编码
	
	
	public ReportOneBean(String name, String userId, String organization,
			String bizRoleCode, String bizRoleName, String resource,
			String itRoleCode, String itRoleName, String orgScope,
			String productScope, String resourceScope, String zhangTaoScope,
			String approveName, String approveReason, String createTime,
			String processId) {
		this.name = name;
		this.userId = userId;
		this.organization = organization;
		this.bizRoleCode = bizRoleCode;
		this.bizRoleName = bizRoleName;
		this.resource = resource;
		this.itRoleCode = itRoleCode;
		this.itRoleName = itRoleName;
		this.orgScope = orgScope;
		this.productScope = productScope;
		this.resourceScope = resourceScope;
		this.zhangTaoScope = zhangTaoScope;
		this.approveName = approveName;
		this.approveReason = approveReason;
		this.createTime = createTime;
		this.processId = processId;
	}
	 
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	public String getOrganization() {
		return organization;
	}
	public void setOrganization(String organization) {
		this.organization = organization;
	}
	public String getBizRoleCode() {
		return bizRoleCode;
	}
	public void setBizRoleCode(String bizRoleCode) {
		this.bizRoleCode = bizRoleCode;
	}
	public String getBizRoleName() {
		return bizRoleName;
	}
	public void setBizRoleName(String bizRoleName) {
		this.bizRoleName = bizRoleName;
	}
	public String getResource() {
		return resource;
	}
	public void setResource(String resource) {
		this.resource = resource;
	}
	public String getItRoleCode() {
		return itRoleCode;
	}
	public void setItRoleCode(String itRoleCode) {
		this.itRoleCode = itRoleCode;
	}
	public String getItRoleName() {
		return itRoleName;
	}
	public void setItRoleName(String itRoleName) {
		this.itRoleName = itRoleName;
	}
	public String getOrgScope() {
		return orgScope;
	}
	public void setOrgScope(String orgScope) {
		this.orgScope = orgScope;
	}
	public String getProductScope() {
		return productScope;
	}
	public void setProductScope(String productScope) {
		this.productScope = productScope;
	}
	public String getResourceScope() {
		return resourceScope;
	}
	public void setResourceScope(String resourceScope) {
		this.resourceScope = resourceScope;
	}
	public String getZhangTaoScope() {
		return zhangTaoScope;
	}
	public void setZhangTaoScope(String zhangTaoScope) {
		this.zhangTaoScope = zhangTaoScope;
	}
	public String getApproveName() {
		return approveName;
	}
	public void setApproveName(String approveName) {
		this.approveName = approveName;
	}
	public String getApproveReason() {
		return approveReason;
	}
	public void setApproveReason(String approveReason) {
		this.approveReason = approveReason;
	}
	public String getCreateTime() {
		return createTime;
	}
	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}
	public String getProcessId() {
		return processId;
	}
	public void setProcessId(String processId) {
		this.processId = processId;
	}
	
	
}

ReportCustomDataSource 继承JRDataSource,显示数据

package com.isprint.report;
import java.text.SimpleDateFormat;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;

public class ReportCustomDataSource implements JRDataSource
{
	private Object[][] data =
		{
			{"Berne","1", "信科","123","经理","aa","Berne","aa","Berne","aa","Berne","aa","Berne","aa",new Long(1), "adfasdfasfasd"},
			{"Berne2","1", "信科","123","经理","aa","Berne","aa","Berne","aa","Berne","aa","Berne","aa",new Long(2), "adfasdfasfasd"},
			{"Berne3","1", "信科","123","经理","aa","Berne","aa","Berne","aa","Berne","aa","Berne","aa",new Long(3), "adfasdfasfasd"},
			{"Berne4","1", "信科","123","经理","aa","Berne","aa","Berne","aa","Berne","aa","Berne","aa",new Long(4), "adfasdfasfasd"},
			{"Berne5","1", "信科","123","经理","aa","Berne","aa","Berne","aa","Berne","aa","Berne","aa",new Long(5), "adfasdfasfasd"},
			{"Berne6","1", "信科","123","经理","aa","Berne","aa","Berne","aa","Berne","aa","Berne","aa",new Long(6), "adfasdfasfasd"},
			{"Berne7","1", "信科","123","经理","aa","Berne","aa","Berne","aa","Berne","aa","Berne","aa",new Long(7), "aaaaaaa"}
		};

	private int index = -1;
	public ReportCustomDataSource()
	{
	}
	public boolean next() throws JRException
	{
		index++;
		return (index < data.length);
	}

	public Object getFieldValue(JRField field) throws JRException
	{
		SimpleDateFormat sdf = new SimpleDateFormat();
		Object value = null;
		String fieldName = field.getName();
		if ("name".equals(fieldName)) {
			value = data[index][0];
		} else if ("organization".equals(fieldName)) {
			value = data[index][1];
		}else if ("bizRoleCode".equals(fieldName)) {
			value = data[index][2];
		}else if ("bizRoleName".equals(fieldName)) {
			value = data[index][3];
		}else if ("itRoleCode".equals(fieldName)) {
			value = data[index][4];
		}else if ("itRoleName".equals(fieldName)) {
			value = data[index][5];
		}else if ("orgScope".equals(fieldName)) {
			value = data[index][6];
		}else if ("productScope".equals(fieldName)) {
			value = data[index][7];
		}else if ("resourceScope".equals(fieldName)) {
			value = data[index][8];
		}else if ("zhangTaoScope".equals(fieldName)) {
			value = data[index][9];
		}else if ("approveName".equals(fieldName)) {
			value = data[index][10];
		}else if ("approveReason".equals(fieldName)) {
			value = data[index][11];
		}else if ("createTime".equals(fieldName)) {
			try {
				value = sdf.parse(String.valueOf(data[index][12]));
			} catch (Exception pe) {
				// do nothing
			} finally {
			}
		} else if ("processId".equals(fieldName)) {
			value = data[index][13];
		}
		return value;
	}
}

ReportCustomBeanFactory的getBeanCollection用来调用数据。

package com.isprint.report;

import java.util.Arrays;
import java.util.Collection;

public class ReportCustomBeanFactory
{
	private static ReportOneBean[] data =
		{
			new ReportOneBean("Berne","1", "信科","123","经理","aa","Berne","aa","Berne","aa","Berne","aa","Berne","aa","2015-4-2", "277 Seventh Av."),
			new ReportOneBean("Berne2","1", "信科","123","经理","aa","Berne","aa","Berne","aa","Berne","aa","Berne","aa","2015-4-2", "277 Seventh Av."),
			new ReportOneBean("Berne3","1", "信科","123","经理","aa","Berne","aa","Berne","aa","Berne","aa","Berne","aa","2015-4-2", "277 Seventh Av."),
			new ReportOneBean("Berne4","1", "信科","123","经理","aa","Berne","aa","Berne","aa","Berne","aa","Berne","aa","2015-4-2", "277 Seventh Av."),
			new ReportOneBean("Berne5","1", "信科","123","经理","aa","Berne","aa","Berne","aa","Berne","aa","Berne","aa","2015-4-2", "277 Seventh Av."),
			new ReportOneBean("Berne6","1", "信科","123","经理","aa","Berne","aa","Berne","aa","Berne","aa","Berne","aa","2015-4-2", "277 Seventh Av."),
			new ReportOneBean("Berne7","1", "信科","123","经理","aa","Berne","aa","Berne","aa","Berne","aa","Berne","aa","2015-4-2", "277 Seventh Av.")
			
		};  
	public static Object[] getBeanArray()
	{
		return data;
	}
	public static Collection getBeanCollection()
	{
		return Arrays.asList(data);
	}


}

bean数据源可以用来设计报表模板,在ireport里面引入代码的jar报或者class文件,然后经过配置就可以看到代码里的写的数据。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值