国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

Hibernate之單表類型數(shù)據(jù)處理泛型類

2019-11-15 01:02:43
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
Hibernate之單表類型數(shù)據(jù)處理泛型類

package chengfei.hibernate.Dao;

import java.util.List;import org.hibernate.HibernateException;import org.hibernate.session;import org.hibernate.Transaction;import chengfei.hibernate.tool.HibernateUtils;

/**hibernate數(shù)據(jù)庫(kù)操作泛型類 * @author Administrator * @param <T> */public class service<T> {

PRivate Class<T> c;

public service(Class<T> c) {this.c = c;}

/**保存 * @param t */public void save(T t) { Session session = HibernateUtils.openSession();Transaction tx = null;try {tx = session.beginTransaction();session.save(t);tx.commit();

} catch (HibernateException e) {tx.rollback();throw e;} finally {session.close();}}

/**刪除 * @param id */public void delete(Integer id) {Session session = HibernateUtils.openSession();Transaction tx = null;try {tx = session.beginTransaction();T t = this.getById(id);session.delete(t);tx.commit();} catch (HibernateException e) {tx.rollback();throw e;} finally {session.close();}

}

/**更新 * @param t */public void update(T t) {Session session = HibernateUtils.openSession();Transaction tx = null;try {tx = session.beginTransaction();session.update(t);tx.commit();} catch (HibernateException e) {tx.rollback();throw e;} finally {session.close();}

}

/**得到唯一對(duì)象 * @param id * @return */@SuppressWarnings("unchecked")public T getById(Integer id) { Session session = HibernateUtils.openSession();Transaction tx = null;try {tx = session.beginTransaction();T t = (T) session.get(c, id);tx.commit();return t;} catch (HibernateException e) {tx.rollback();throw e;} finally {session.close();}}

/**獲得所有 * @return */@SuppressWarnings("unchecked")public List<T> FindAll() {Session session = HibernateUtils.openSession();Transaction tx = null;try {tx = session.beginTransaction();List<T> list = session.createQuery("from " + c.getSimpleName()).list();tx.commit();return list;} catch (HibernateException e) {tx.rollback();throw e;} finally {session.close();}}

/**獲得分頁(yè)的數(shù)據(jù) * @param firstResult * @param maxresult * @return */@SuppressWarnings({ "unchecked", "rawtypes" })public QueryResult FindAll(int firstResult, int maxresult) {Session session = HibernateUtils.openSession();Transaction tx = null;try {tx = session.beginTransaction();List<T> list = session.createQuery(//"from " + c.getSimpleName())//.setFirstResult(firstResult)//.setMaxResults(maxresult).list();tx.commit();Long count = (Long) session.createQuery("select count(*) from " + c.getSimpleName())//.uniqueResult();return new QueryResult(list, count.intValue());} catch (HibernateException e) {tx.rollback();throw e;} finally {session.close();}

}

}


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 阿克苏市| 县级市| 隆安县| 周口市| 贺兰县| 金阳县| 南宁市| 肥乡县| 遵义县| 仁布县| 浦东新区| 买车| 雷波县| 鄢陵县| 华安县| 滦南县| 渭源县| 璧山县| 泰来县| 石家庄市| 湘潭县| 晋江市| 马公市| 石城县| 炉霍县| 西吉县| 香格里拉县| 临洮县| 临潭县| 同心县| 华池县| 来宾市| 商水县| 长治县| 巴青县| 南和县| 黄冈市| 禄劝| 乌鲁木齐市| 德兴市| 华容县|