- http://www.javaeye.com/topic/179668?page=1
- public class BaseDaoHibernate extends HibernateDaoSupport implements BaseDao {
- private static Log log = LogFactory.getLog(BaseDaoHibernate.class);
- public void save(final Object obj) {
- getHibernateTemplate().save(obj);
- // save2(obj);
- }
- private Serializable save(Session session, Object p) {
- Transaction t = session.beginTransaction();
- Serializable o = session.save(p);
- t.commit();
- session.flush();
- session.close();
- return o;
- }
- public Serializable save2(final Object entity) throws DataAccessException {
- Object o = getHibernateTemplate().execute(new HibernateCallback() {
- public Object doInHibernate(final Session session)
- throws HibernateException {
- return save(session, entity);
- }
- });
- return null;
- }
- public void deleteAll(final Object o) {
- String hql = " delete from " + o.getClass().getName() + " ";
- executeUpdate(hql);
- }
- /**
- * 根据查询语句,返回对象列表
- *
- * @param hql
- * 查询语句
- * @return 符合查询语句的对象列表;最多返回 Constants.maxResults 条
- */
- public List find(final String hql) {
- final int firstResult = 0;
- return find(hql, firstResult, Constants.MAXRESULTS);
- }
- /**
- * 根据查询语句,返回对象列表
- *
- * @param hql
- * 查询语句
- * @return 符合查询语句的对象列表;最多返回 Constants.maxResults 条
- */
- public List find(final String hql, List tablesName) {
- final int firstResult = 0;
- return find(hql, firstResult, Constants.MAXRESULTS);
- }
- /**
- * 返回指定起始位置,指定条数的对象
- *
- * @param hql
- * 查询语句
- * @param firstResult
- * 起始位置
- * @param maxResults
- * 最多纪录数
- * @return list 结果列表
- */
- public List find(final String hql, final int firstResult,
- final int maxResults) {
- if (log.isDebugEnabled()) {
- log.debug("hql=" + hql);
- }
- return getHibernateTemplate().executeFind(new HibernateCallback() {
- public Object doInHibernate(final Session session)
- throws HibernateException {
- try {
- session.connection().setReadOnly(true);
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- Query queryObject = session.createQuery(hql);
- queryObject.setReadOnly(true);
- queryObject.setFirstResult(firstResult);
- queryObject.setMaxResults(maxResults);
- return queryObject.list();
- }
- });
- }
- /**
- * 返回指定起始位置,指定条数的对象
- *
- * @param hql
- * 查询语句
- * @param firstResult
- * 起始位置
- * @param maxResults
- * 最多纪录数
- * @return list 结果列表
- */
- public List findByIterate(final String hql) {
- return getHibernateTemplate().executeFind(new HibernateCallback() {
- public Object doInHibernate(final Session session)
- throws HibernateException {
- Query queryObject = session.createQuery(hql);
- queryObject.setCacheable(true);
- Iterator iter = queryObject.iterate();
- List ls = new ArrayList();
- while (iter.hasNext()) {
- ls.add(iter.next());
- }
- return ls;
- }
- });
- }
- /**
- * 查询语句需要的条件参数。通过map传递
- *
- * @param hql
- * 查询语句
- * @param map
- * 参数
- * @param firstResult
- * 起始位置
- * @param maxResults
- * 最多纪录数
- * @return list 结果列表
- */
- public List find(final String hql, final Map map, final int firstResult,
- final int maxResults) {
- return getHibernateTemplate().executeFind(new HibernateCallback() {
- public Object doInHibernate(final Session session)
- throws HibernateException {
- Query queryObject = session.createQuery(hql);
- String[] params = queryObject.getNamedParameters();
- for (int i = 0, max = params.length; i < max; i++) {
- queryObject.setParameter(params[i], map.get(params[i]));
- }
- queryObject.setFirstResult(firstResult);
- queryObject.setMaxResults(maxResults);
- return queryObject.list();
- }
- });
- }
- /**
- * 根据查询语句,返回对象列表
- *
- * @param hql
- * 查询语句
- * @return 符合查询语句的对象列表;最多返回 Constants.maxResults 条
- */
- public List find(final String hql, final Map map) {
- final int firstResult = 0;
- return find(hql, map, firstResult, Constants.MAXRESULTS);
- }
- /**
- * 通过Hql 执行update/delete操作
- *
- * @param hql
- * @return
- */
- public int executeUpdate(final String hql) {
- int result = 0;
- Object o = getHibernateTemplate().execute(new HibernateCallback() {
- public Object doInHibernate(final Session session)
- throws HibernateException {
- int result = 0;
- Query queryObject = session.createQuery(hql);
- result = queryObject.executeUpdate();
- return result;
- }
- });
- Integer i = (Integer) o;
- result = i.intValue();
- return result;
- }
- public void deleteAllByObject(Object obj) {
- List l = null;
- String hql = " from " + obj.getClass().getName() + " ";
- if (log.isDebugEnabled()) {
- log.debug("hql=" + hql);
- }
- l = find(hql);
- Iterator ait = l.iterator();
- while (ait != null && ait.hasNext()) {
- getHibernateTemplate().delete(ait.next());
- }
- }
- /**
- * 通过 DetachedCriteria 进行查询
- *
- * @param dc
- * @return
- */
- public List findByCriteria(final DetachedCriteria dc) {
- return (List) getHibernateTemplate().execute(new HibernateCallback() {
- public Object doInHibernate(Session session)
- throws HibernateException {
- if (log.isDebugEnabled()) {
- log.debug("dc=" + dc);
- }
- Criteria criteria = dc.getExecutableCriteria(session);
- return criteria.list();
- }
- }, true);
- }
- /**
- * 通过 DetachedCriteria 进行查询
- *
- * @param dc
- * @return
- */
- public List findByCriteria(final DetachedCriteria dc,
- final int firstResult, final int maxResults) {
- return (List) getHibernateTemplate().execute(new HibernateCallback() {
- public Object doInHibernate(final Session session)
- throws HibernateException {
- if (log.isDebugEnabled()) {
- log.debug("findByCriteria dc=" + dc);
- }
- Criteria criteria = dc.getExecutableCriteria(session);
- criteria.setFirstResult(firstResult);
- criteria.setMaxResults(maxResults);
- return criteria.list();
- }
- }, true);
- }
- public int countByCriteria(final DetachedCriteria dc) {
- Integer count = (Integer) getHibernateTemplate().execute(
- new HibernateCallback() {
- public Object doInHibernate(Session session)
- throws HibernateException {
- if (log.isDebugEnabled()) {
- log.debug("countByCriteria dc=" + dc);
- }
- Criteria criteria = dc.getExecutableCriteria(session);
- return criteria.setProjection(Projections.rowCount())
- .uniqueResult();
- }
- }, true);
- if (count != null) {
- return count.intValue();
- } else {
- return 0;
- }
- }
- /**
- * 通过 DetachedCriteria 进行查询
- *
- * @param dc
- * @return
- */
- public List findByCriteriaCache(final DetachedCriteria dc) {
- return (List) getHibernateTemplate().execute(new HibernateCallback() {
- public Object doInHibernate(Session session)
- throws HibernateException {
- if (log.isDebugEnabled()) {
- log.debug("dc=" + dc);
- }
- Criteria criteria = dc.getExecutableCriteria(session);
- criteria.setCacheable(true);
- return criteria.list();
- }
- }, true);
- }
- /**
- * 通过 DetachedCriteria 进行查询
- *
- * @param dc
- * @return
- */
- public List findByCriteriaCache(final DetachedCriteria dc,
- final int firstResult, final int maxResults) {
- return (List) getHibernateTemplate().execute(new HibernateCallback() {
- public Object doInHibernate(final Session session)
- throws HibernateException {
- if (log.isDebugEnabled()) {
- log.debug("dc=" + dc);
- }
- Criteria criteria = dc.getExecutableCriteria(session);
- criteria.setCacheable(true);
- criteria.setFirstResult(firstResult);
- criteria.setMaxResults(maxResults);
- return criteria.list();
- }
- }, true);
- }
- /**
- * count search cache
- */
- public int countByCriteriaCache(final DetachedCriteria dc) {
- Integer count = (Integer) getHibernateTemplate().execute(
- new HibernateCallback() {
- public Object doInHibernate(Session session)
- throws HibernateException {
- if (log.isDebugEnabled()) {
- log.debug("dc=" + dc);
- }
- Criteria criteria = dc.getExecutableCriteria(session);
- criteria.setCacheable(true);
- return criteria.setProjection(Projections.rowCount())
- .uniqueResult();
- }
- }, true);
- if (count != null) {
- return count.intValue();
- } else {
- return 0;
- }
- }
- public int executeUpdate(final String hql, final Map pMap) {
- int result = 0;
- Object o = getHibernateTemplate().execute(new HibernateCallback() {
- public Object doInHibernate(final Session session)
- throws HibernateException {
- int result = 0;
- Query queryObject = session.createQuery(hql);
- String[] params = queryObject.getNamedParameters();
- for (int i = 0, max = params.length; i < max; i++) {
- queryObject.setParameter(params[i], pMap.get(params[i]));
- }
- result = queryObject.executeUpdate();
- return result;
- }
- });
- Integer i = (Integer) o;
- result = i.intValue();
- return result;
- }
- public void delete(Object obj) {
- getHibernateTemplate().delete(obj);
- }
- public Object load(Class aclass, Serializable id)
- throws DataAccessException {
- Object obj = getHibernateTemplate().load(aclass, id);
- return obj;
- }
- public Object get(Class aclass, Serializable id) {
- Object obj = getHibernateTemplate().get(aclass, id);
- return obj;
- }
- public void saveorUpdate(Object obj) {
- getHibernateTemplate().saveOrUpdate(obj);
- getHibernateTemplate().flush();
- }
- public void update(Object o) {
- getHibernateTemplate().update(o);
- }
- /**
- * count hql 方法 .
- */
- public int count(String hql, List ls) {
- String countQueryString = " select count (*) " + hql;
- List countlist = getHibernateTemplate().find(countQueryString);
- Long count = (Long) countlist.get(0);
- return count.intValue();
- }
- /**
- * count hql 方法 .
- */
- public int count(String hql, Map params) {
- String countQueryString = " select count (*) " + hql;
- List countlist = find(countQueryString, params);
- Object co = countlist.get(0);
- if (co instanceof Integer) {
- Integer count = (Integer) countlist.get(0);
- return count.intValue();
- } else {
- Long count = (Long) countlist.get(0);
- return count.intValue();
- }
- }
- /**
- * TODO count hql 方法 .
- */
- public int countCache(String hql, Map params) {
- String countQueryString = " select count (*) " + hql;
- List countlist = findCache(countQueryString, params);
- Object co = countlist.get(0);
- if (co instanceof Integer) {
- Integer count = (Integer) countlist.get(0);
- return count.intValue();
- } else {
- Long count = (Long) countlist.get(0);
- return count.intValue();
- }
- }
- /**
- * 根据查询语句,返回对象列表 TODO
- *
- * @param hql
- * 查询语句
- * @return 符合查询语句的对象列表;最多返回 Constants.maxResults 条
- */
- public List findCache(final String hql, final Map map) {
- final int firstResult = 0;
- return findCache(hql, map, firstResult, Constants.MAXRESULTS);
- }
- /**
- * 查询语句需要的条件参数。通过map传递 TODO
- *
- * @param hql
- * 查询语句
- * @param map
- * 参数
- * @param firstResult
- * 起始位置
- * @param maxResults
- * 最多纪录数
- * @return list 结果列表
- */
- public List findCache(final String hql, final Map map,
- final int firstResult, final int maxResults) {
- return getHibernateTemplate().executeFind(new HibernateCallback() {
- public Object doInHibernate(final Session session)
- throws HibernateException {
- Query queryObject = session.createQuery(hql);
- // 设置 查询cache
- queryObject.setCacheable(true);
- String[] params = queryObject.getNamedParameters();
- for (int i = 0, max = params.length; i < max; i++) {
- queryObject.setParameter(params[i], map.get(params[i]));
- }
- queryObject.setFirstResult(firstResult);
- queryObject.setMaxResults(maxResults);
- return queryObject.list();
- }
- });
- }
- public int count(String hql) {
- String countQueryString = " select count (*) " + hql;
- List countlist = find(countQueryString);
- Object co = countlist.get(0);
- if (co instanceof Integer) {
- Integer count = (Integer) countlist.get(0);
- return count.intValue();
- } else {
- Long count = (Long) countlist.get(0);
- return count.intValue();
- }
- }
- @Override
- public HibernateTemplate getTemplate() {
- return getHibernateTemplate();
- }
转载通用dao
最新推荐文章于 2021-02-27 02:36:38 发布
2227

被折叠的 条评论
为什么被折叠?



