技术开发 频道

使用JasperReport与iBATIS开发Web报表


三、        处理iBati返回数据
 
如果iBATIS没有采用JavaBean作为返回对象,则可以采用java.util.map作为数据的返回对象。采用java.util.Map对象,需要额外的一些步骤。下面的代码则说明了iBATIS的select语句返回的java.util.Map对象。Src/ iBATIS.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 1.0//EN" "http://iBATIS.apache.org/dtd/sql-map-2.dtd"> <sqlMap> <select id="salesByListOfMapsSQL" resultClass="java.util.HashMap"> SELECT E.EMPLOYEE_ID "ID", E.FIRST_NAME "FIRST", E.LAST_NAME "LAST", MS.TOTAL_SALES "TOTAL", MS.LATEST_SALE FROM EMPLOYEE E, MONTHLY_SALES MS WHERE E.EMPLOYEE_ID = MS.EMPLOYEE_ID AND MS.MONTH = #value# </select> <resultMap id="searchResultList" class="MonthlySalesBean"> <result property="employeeID" column="ID"/> <result property="first" column="FIRST"/> <result property="last" column="LAST"/> <result property="total" column="TOTAL"/> <result property="latestSale.amount" column="LATEST_SALE"/> </resultMap> <select id="salesByJavaBeansSQL" resultMap="searchResultList"> SELECT E.EMPLOYEE_ID "ID", E.FIRST_NAME "FIRST", E.LAST_NAME "LAST", MS.TOTAL_SALES "TOTAL", MS.LATEST_SALE FROM EMPLOYEE E, MONTHLY_SALES MS WHERE E.EMPLOYEE_ID = MS.EMPLOYEE_ID AND MS.MONTH = #value# </select> </sqlMap>

上面的代码返回的对象即为map对象。请注意,map对象中的Key值直接来自于select语句,因此,像TO_CHAR(MS.TOTAL_SALES)这样的表达式在报表中不提倡使用。因此,比较人性化的为字段命名,是一件很值得的事情。因为map的key值是作为java.lang.Object类型来进行存储的,因此有必要对字段返回类型进行一下整理。
真正的数据填充类应该是ServiceLocatorBean.java类,其代码如下所示:
import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; import javax.servlet.ServletContext; import org.springframework.context.ApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; public class ServiceLocatorBean implements ServiceLocatorIF { private static final long serialVersionUID = -7166271873610635886L; //the Spring application context private ApplicationContext appContext; DAO dao = null; public ServiceLocatorBean() { try { // get the spring context ServletContext context = FacesUtils.getServletContext(); this.appContext = WebApplicationContextUtils.getRequiredWebApplicationContext(context); // create instance of the business object this.dao = (DAO) this.lookupService("dao"); Connection conn = this.dao.getSqlMapClient().getDataSource().getConnection(); conn.setAutoCommit(false); /* Creating a statement lets us issue commands against the connection. */ Statement s = conn.createStatement(); // just in case old tables from prior run (after first run which // will create the USER1 schema) try { s.execute("drop table employee"); s.execute("drop table monthly_sales"); } catch (Exception ex) { // not to be concerned (at least in this example } /* We create a table, add a few rows, and update one. */ s.execute("create table employee (employee_id int, first_name varchar(40), last_name varchar(40))"); s.execute("insert into employee values (1,'sterning', 'chen')"); s.execute("insert into employee values (2,'yuxuan', 'Wand')"); s.execute("insert into employee values (3,'Mickey', 'Li')"); s.execute("create table monthly_sales (employee_id int, total_sales numeric(16, 2), latest_sale numeric(8, 2), month int)"); s.execute("insert into monthly_sales values (1, 1600.50, 32.50, 1)"); s.execute("insert into monthly_sales values (2, 1544.20, 12.50, 1)"); s.execute("insert into monthly_sales values (3, 18814.80, 78.65, 1)"); s.execute("insert into monthly_sales values (1, 1450.50, 10.65, 2)"); s.execute("insert into monthly_sales values (2, 2004.25, 52.10, 2)"); s.execute("insert into monthly_sales values (3, 9819.00, 40.65, 2)"); s.close(); conn.commit(); } catch (SQLException sqle) { // just means the tables already exist sqle.printStackTrace(); } catch (Exception ex) { ex.printStackTrace(); } } public DAO getDao() { return this.dao; } public Object lookupService(String serviceBeanName) { return appContext.getBean(serviceBeanName); } }
0
相关文章