bean作为jasperReport 的数据源

iReport使用javabean做数据源需要四个步骤

一、建立一个javabean,这是一个普通的bean:

 

/*

 * PersonBean.java

 *

 * Created on 8 luglio 2004, 1.41

 
*/



package  it.businesslogic.ireport.examples.beans;


/**

 *

 * 
@author  Administrator

 
*/


public   class  PersonBean  {

    

    
private String firstName;

    

    
private String lastName;

    

    
private HobbyBean[] hobbies;

    

    
private AddressBean address;

    

    
/** Creates a new instance of PersonBean */

    
public PersonBean() {

       
this(null);

    }


    

    
public PersonBean(String name) {

           
this.setFirstName( name );

        hobbies 
= new HobbyBean[0];

    }


    

    
/**

     * Getter for property firstName.

     * 
@return Value of property firstName.

     
*/


    
public java.lang.String getFirstName() {

        
return firstName;

    }


    

    
/**

     * Setter for property firstName.

     * 
@param firstName New value of property firstName.

     
*/


    
public void setFirstName(java.lang.String firstName) {

        
this.firstName = firstName;

    }


    

    
/**

     * Getter for property lastName.

     * 
@return Value of property lastName.

     
*/


    
public java.lang.String getLastName() {

        
return lastName;

    }


    

    
/**

     * Setter for property lastName.

     * 
@param lastName New value of property lastName.

     
*/


    
public void setLastName(java.lang.String lastName) {

        
this.lastName = lastName;

    }


    

    
/**

     * Getter for property hobbies.

     * 
@return Value of property hobbies.

     
*/


    
public it.businesslogic.ireport.examples.beans.HobbyBean[] getHobbies() {

        
return this.hobbies;

    }


    

    
/**

     * Setter for property hobbies.

     * 
@param hobbies New value of property hobbies.

     
*/


    
public void setHobbies(it.businesslogic.ireport.examples.beans.HobbyBean[] hobbies) {

        
this.hobbies = hobbies;

    }


    

    
/**

     * Getter for property address.

     * 
@return Value of property address.

     
*/


    
public it.businesslogic.ireport.examples.beans.AddressBean getAddress() {

        
return address;

    }


    

    
/**

     * Setter for property address.

     * 
@param address New value of property address.

     
*/


    
public void setAddress(it.businesslogic.ireport.examples.beans.AddressBean address) {

        
this.address = address;

    }


    

}


 

二 实现JRDataSourceProvider接口

       实现JRDataSourceProvider接口最好的办法是继承JRAbstractBeanDataSourceProvider类:

 

import  net.sf.jasperreports.engine. * ;

import  net.sf.jasperreports.engine.data. * ;

import  it.businesslogic.ireport.examples.beans. * ;

import  java.util. * ;


public   class  PersonBeansDataSource  extends  JRAbstractBeanDataSourceProvider  {

  

      
public PersonBeansDataSource() {

             
super(PersonBean.class);

      }


  

      
public JRDataSource create(JasperReport report) throws JRException {

             

             ArrayList list 
= new ArrayList();

/*这里查询数据库或其他途经,把数据包装到bean中

*把bean压入list中

              *

*/


             
return new JRBeanCollectionDataSource(list);

      

      }


      

      
public void dispose(JRDataSource dataSource) throws JRException {

             
// nothing to do

      }


  }


 

三、打开ireport 5.0 ,选择资料来源——〉连接/资料来源——〉new——〉type of connection/datasource选项选择JRDataSourceProvider,——〉填入名称和class——〉save——〉完成

 

四、在报表中 点击   资料来源——〉报表查询——〉选择Use DataSource Provider选项卡——〉点击Get fields from datasource 得到bean中定义的属性为 field




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值