0
我通過setMaxResults知道& Hibernate中Criteria的setFristResults方法我可以做分頁..但是我的問題是我如何將它與DAO分開,我的意思是我在那裏實現了DAO,只能在那裏指定setMaxResults ()& setFristResults()值,我想使它作爲單獨的層,因爲如果假設是周圍有500 DAO的各種模塊則其難以發生相應的變化,Hibernate尋呼
下面是代碼: 域:
public class Demo implements java.io.Serializable {
private int id;
private Date date;
private String weekday;
private Double cost;
public Demo()
{
}
public Demo(Date date, String weekday, Double cost)
{
this.date = date;
this.weekday = weekday;
this.cost = cost;
}
/***Setter & getter***/
}
DAO:
public interface DemoDAO {
public void setPage(Page page);
public List<Demo> findAll();
}
public class DemoDAOImpl implements DemoDAO {
private HibernateDAO hibernateDAO;
private Session session;
private Page page;
public DemoDAOImpl()
{
hibernateDAO = new HibernateDAOImpl();
session = hibernateDAO.getSession();
}
public void setPage(Page page)
{
this.page = page;
}
public List<Demo> findAll()
{
session.beginTransaction();
Criteria criteria = session.createCriteria(Demo.class);
criteria.setFirstResult(page.getPageNumber() * page.getPageSize());
criteria.setMaxResults(page.getPageSize());
List<Demo> demoList = criteria.list();
session.getTransaction().commit();
return demoList;
}
服務:
public interface DemoService {
public List<Demo> showDetails(Page page);
}
public class DemoServiceImpl implements DemoService {
private DemoDAO demoDAO;
public DemoServiceImpl()
{
demoDAO = new DemoDAOImpl();
}
public List<Demo> showDetails(Page page)
{
if(page != null)
demoDAO.setPage(page);
else
page = new Page(0,50);
return demoDAO.findAll();
}
}
現在
從前端我打電話此showDetails ...
請,如果我使用的是正確的事情指正......
請您詳細說明...... :) – Amit 2011-04-14 09:26:02
我的意思是我怎樣才能使它成爲無狀態的.. – Amit 2011-04-14 09:35:49
@Amit - 通過將'Page'對象作爲參數傳遞給方法。 – Bozho 2011-04-14 17:41:22