Hibernate中的web/dao層程式碼
阿新 • • 發佈:2019-01-05
Web層
//表現層通用實現 public class BaseAction<T> extends ActionSupport implements ModelDriven<T> { protected PageBean pageBean = new PageBean(); //建立離線提交查詢物件 DetachedCriteria detachedCriteria = null; public void setPage(int page) { pageBean.setCurrentPage(page); } public void setRows(int rows) { pageBean.setPageSize(rows); } //將指定java物件轉為json,並響應到客戶端頁面 public void java2Json(Object o,String[] excludes){ JsonConfig jsonConfig = new JsonConfig(); //指定哪些屬性不需要轉json jsonConfig.setExcludes(excludes); String json = JSONObject.fromObject(o,jsonConfig).toString(); ServletActionContext.getResponse().setContentType("text/json;charset=UTF-8"); try { ServletActionContext.getResponse().getWriter().print(json); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //將指定java物件轉為json,並響應到客戶端頁面 public void java2Json(List o,String[] excludes){ JsonConfig jsonConfig = new JsonConfig(); //指定哪些屬性不需要轉json jsonConfig.setExcludes(excludes); String json = JSONArray.fromObject(o,jsonConfig).toString(); ServletActionContext.getResponse().setContentType("text/json;charset=UTF-8"); try { ServletActionContext.getResponse().getWriter().print(json); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static final String HOME = "home"; public static final String LIST = "list"; //模型物件 protected T model; public T getModel() { return model; } //在構造方法中動態獲取實體型別,通過反射建立model物件 public BaseAction() { ParameterizedType genericSuperclass = (ParameterizedType) this.getClass().getGenericSuperclass(); //獲得BaseAction上宣告的泛型陣列 Type[] actualTypeArguments = genericSuperclass.getActualTypeArguments(); Class<T> entityClass = (Class<T>) actualTypeArguments[0]; detachedCriteria = DetachedCriteria.forClass(entityClass); pageBean.setDetachedCriteria(detachedCriteria); try { model = entityClass.newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } }
Dao層
重點
1.獲得父類泛型的型別
2.將sessionFactory注入dao,給HibernateDaoSupport呼叫,生成HibernateTemplate
//持久層通用實現 public class BaseDaoImpl<T> extends HibernateDaoSupport implements IBaseDao<T> { //代表某個實體的型別 private Class<T> entityClass; @Resource//根據型別注入spring工廠中的sessionFactory物件 public void setMySessionFactory(SessionFactory sessionFactory){ super.setSessionFactory(sessionFactory); } //在父類BaseDaoImpl的構造方法中動態獲得entityClass public BaseDaoImpl() { ParameterizedType superclass = (ParameterizedType) this.getClass().getGenericSuperclass(); //獲得父類上宣告的泛型陣列 Type[] actualTypeArguments = superclass.getActualTypeArguments(); entityClass = (Class<T>) actualTypeArguments[0]; } public void save(T entity) { this.getHibernateTemplate().save(entity); } public void delete(T entity) { this.getHibernateTemplate().delete(entity); } public void update(T entity) { this.getHibernateTemplate().update(entity); } public T findById(Serializable id) { return this.getHibernateTemplate().get(entityClass, id); } public List<T> findAll() { String hql = "FROM " + entityClass.getSimpleName(); return (List<T>) this.getHibernateTemplate().find(hql); } }