*制定一個類,該類的對象屬性 obMap 將查詢得到的每個字段的值存貯
*/
import java.util.TreeMap;
public class ListObject {
private TreeMap obMap;
public TreeMap getObMap() {
return obMap;
}
public void setObMap(TreeMap obMap) {
this.obMap = obMap;
}
}
/**
* 該類的對象將調用Session對象的方法對數據庫進行各種操作
*/
public class ObjectHibernate{
................................................
/**
* 按條件查詢記錄(第一條記錄位置,每次查詢記錄數)
*/
public List findObject(int firstResult, int maxResults) {
Session session = HibernateSessionFactory.currentSession();
Transaction transaction = null;
try {
String strSql="select t.orderlistId ,t1.orderId,t1.orderTime," +
" t.orderGoodsName ,t.orderGoodsCount from Orderlist t,Order t1 "+
" where t.orderId=t1.orderId";
transaction = session.beginTransaction();
Query query = session.createQuery(strSql);
if (query != null && query.list() != null
&& query.list().size() > 0) {
query.setFirstResult(firstResult);
query.setMaxResults(maxResults);
transaction.commit();
session.flush();
List list=query.list();
List list1=new ArrayList(); //該集合將封裝後的ListObject存貯,並返回
if (list != null && list.size() > 0) {
Iterator it = list.iterator();
ListObject lo = null;
TreeMap treeMap = null;
while (it.hasNext()) {
Object ob[] = (Object[]) it.next();
treeMap = new TreeMap();
for (int i = 0; i <ob.length; i++) {
lo = new ListObject();
treeMap.put(new Integer(i), ob[i]);
}
lo.setObMap(treeMap);
list1.add(lo);
}
return list1;
} else {
transaction.commit();
session.flush();
return null;
}
} catch (HibernateException e) {
if (transaction != null)
transaction.rollback();
log.error(e);
return null;
} finally {
if (session != null) {
try {
session.close();
} catch (HibernateException e) {
log.error(e);
throw new RuntimeException(e);
}
}
}
}
..........................................................
}
在Action中獲得 ObjectHibernate類對象的 findObject(int firstResult, int maxResults)方法的返回值,並將該返回值放在HttpServletRequest中,取名爲 ‘objectList’
頁面遍歷 ListObject對象,並遍歷其屬性obMap(我用struts標籤了)
..................................
<logic:empty name="objectList">
沒有記錄!!!
</logic:empty>
<logic:notEmpty name="objectList">
<logic:iterate id="orderList" name="objectList">
<tr>
<logic:iterate id="oValue" name="orderList" property="obMap">
<td>
<bean:write name="oValue" property="key" />
=
<bean:write name="oValue" property="value" />
</td>
</logic:iterate>
</tr>
</logic:iterate>
</logic:notEmpty>