1. 程式人生 > >利用BDF2-hibernateDao實現資料的持久化操作

利用BDF2-hibernateDao實現資料的持久化操作

詳細內容請參考 http://wiki.bsdn.org/pages/viewpage.action?pageId=29392984

需要在自己的dao中繼承com.bstek.bdf2.core.orm.hibernate.HibernateDao這個類在ORM jar包當中。

1.資料的持久化範例(儲存、刪除和修改)---------------------------------------------------------

@DataResolver
publicvoid saveDemoUsers(Collection<DemoUser> users){
    Session session = this.getSessionFactory().openSession();
    try{
        for(DemoUser user : users) {
            EntityState state=EntityUtils.getState(user);
            if(state.equals(EntityState.NEW)) {
                session.save(user);
            }elseif (state.equals(EntityState.MODIFIED)) {
                session.update(user);
            }elseif (state.equals(EntityState.DELETED)) {
                session.delete(user);
            }
        }
    }finally{
        session.flush();
        session.close();
    }
}

2.查詢--------------------------------------------------------------------------------------

2.1無條件不帶分頁的查詢

@DataProvider
publicCollection<DefaultPosition> query1(){
    returnthis.query("from "+DemoUser.class.getName());
}


2.2有條件不帶分頁的查詢
@DataProvider
publicCollection<DefaultPosition> query2(String username){
    Map<String,Object> map =newHashMap<String,Object>();
    String sql="from "+DomeUser.class.getName()+" du ";
    if(username!=null&&!"".equals(username)){
        map.put("username", username);
        sql+=" where du.username=:username";
    }
    returnthis.query(sql, map);
}
@DataProvider
publicCollection<DomeUser> query3(String username){
    Map<String,Object> map =newHashMap<String,Object>();
    String sql="from "+DomeUser.class.getName()+" du ";
    if(username!=null&&!"".equals(username)){
        map.put("username","%"+username+"%");
        sql+="where du.username like :username";
    }
    returnthis.query(sql, map);
}
@DataProvider
publicCollection<DemoUser> query4(String username){
    DetachedCriteria detachedCriteria=DetachedCriteria.forClass(DemoUser.class);
    if(username!=null&&!"".equals(username)){
        detachedCriteria.add(Restrictions.eq("username", username));
    }
    return(Collection<DemoUser>)this.query(detachedCriteria);
}

2.3無條件帶分頁的查詢-------------------------------------------------------------------
@DataProvider
publicvoid query5(Page<DemoUser> page) throwsException{
    this.pagingQuery(page,"from "+DemoUser.class.getName(),"select count(*) from "+DemoUser.class.getName());
}

#.這個pagingQuery方法的第三個引數需要統計表中所有的記錄數

2.3有條件帶分頁的查詢-------------------------------------------------------------------

@DataProvider
publicvoid query6(Page<DemoUser> page,String username) throwsException{
    Map<String,Object> map =newHashMap<String,Object>();
    String sql="from "+DomeUser.class.getName()+" du ";
    String sqlCount="select count(*) from "+DomeUser.class.getName()+" du ";
    if(username!=null&&!"".equals(username)){
        map.put("username", username);
        sql+=" where du.username=:username";
        sqlCount+=" where du.username=:username";
    }
    this.pagingQuery(page, sql,sqlCount,map);
}

可以看到這裡的查詢方法有四個引數:第二個和第三個引數分別是帶條件的HQL語句以及統計執行帶條件的HQL語句後的記錄數。
@DataProvider
publicvoid query7(Page<DemoUser> page,String username) throwsException{
    DetachedCriteria detachedCriteria=DetachedCriteria.forClass(DemoUser.class);
    if(username!=null&&!"".equals(username)){
        detachedCriteria.add(Restrictions.eq("username", username));
    }
    this.pagingQuery(page, detachedCriteria);
}