SSM框架Dao层基础封装文件

SSM框架基础封装BaseDao(IBaseDao)、BaseAction

IBaseDao--BaseDao

package com.hfxt.dao.impl;

import java.io.Serializable;

import java.math.BigInteger;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import org.hibernate.Query;

import org.hibernate.Session;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.dao.DataAccessException;

import org.springframework.orm.hibernate3.HibernateCallback;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import org.springframework.transaction.annotation.Transactional;

import com.hfxt.common.Pager;

import com.hfxt.common.Validity;

import com.hfxt.dao.IBaseDao;

import com.hfxt.exception.HibernateDaoSupportException;

public class BaseDao<T, PK extends Serializable> extends HibernateDaoSupport implements IBaseDao<T, PK> {

         protected Logger log = LoggerFactory.getLogger(this.getClass());

         private boolean isCacheQueries = true;

public boolean isCacheQueries() {

                   return isCacheQueries;

         }

         public void setCacheQueries(boolean isCacheQueries) {

                   this.isCacheQueries = isCacheQueries;

         }

         /**

          * The <code>add(T entity)</code> method is add object to database.

          * @param entity

          *            if you want to add entity.

          * @throws HibernateDaoSupportException

          *             Throws HibernateDaoSupportException when accessing and

          *             manipulating database happen exception.

          */

         public PK add(T entity) throws HibernateDaoSupportException {

                   if (null == entity) {

                            throw new HibernateDaoSupportException("Param(#"

                                               + this.getClass().getName() + ") with value null");

                   }

                   try {

                            PK pk = (PK) this.getHibernateTemplate().save(entity);

                            log.debug("Executing save method is successful!");

                            return pk;

                   } catch (DataAccessException e) {

                            log.error("Error saving entity:{}", e);

                            throw new HibernateDaoSupportException("Error saving (#"

                                               + this.getClass().getName() + "#):" + e);

                   }

         }

         /**

          * 更新或保存实体

          * @param entity

          */

         public void saveOrUpdateEntity(T entity) {

                   this.getHibernateTemplate().saveOrUpdate(entity);

         }

         /**

          * The <code>delete(T entity)</code> method is delete object to database.

          * @param entity

          *            if you want to delete entity.

          * @throws HibernateDaoSupportException

          *             Throws HibernateDaoSupportException when accessing and

          *             manipulating database happen exception.

          */

         public void delete(T entity) throws HibernateDaoSupportException {

                   if (null == entity) {

                            throw new HibernateDaoSupportException("Param(#"

                                               + this.getClass().getName() + ") with value null");

                   }

                   try {

                            this.getHibernateTemplate().delete(entity);

                            log.debug("Execute delete method is successful!");

                   } catch (DataAccessException e) {

                            log.error("Error deleting entity:{}", e);

                            throw new HibernateDaoSupportException("Error deleting (#"

                                               + this.getClass().getName() + "#):" + e);

                   }

         }

         /**

          * The <code>find(T entity)</code> method is find object according object

          * type.

          * @param entity

          *            if you want to find class condition.

          * @return List<T> according entity to find object's connection.

          * @throws HibernateDaoSupportException

          *             Throws HibernateDaoSupportException when accessing and

          *             manipulating database happen exception.

          */

         @SuppressWarnings("unchecked")

         public List<T> find(T entity) throws HibernateDaoSupportException {

                   if (null == entity) {

                            throw new HibernateDaoSupportException("Param(#"

                                               + this.getClass().getName() + ") with value null");

                   }

                   List lists = null;

                   try {

                            if (isCacheQueries) {

                                     this.getHibernateTemplate().setCacheQueries(true);

                            } else {

                                     isCacheQueries = true;

                                     this.getHibernateTemplate().setCacheQueries(false);

                            }

                            lists = (List<T>) this.getHibernateTemplate().findByExample(entity);

                            log.debug("Execute find method is successful!");

                   } catch (DataAccessException e) {

                            log.error("Error finding entity: {}", e);

                            throw new HibernateDaoSupportException("Error finding (#"

                                               + this.getClass().getName() + "#):" + e);

                   }

                  return lists;

         }

         /**

          * find object's collection with class

          * @param clazz

          *            according class

          * @return Object's connection

          * @throws HibernateDaoSupportException

          *             when accessing and manipulating database happen exception

          */

         @SuppressWarnings("unchecked")

         public List<T> find(Class<T> clazz) throws HibernateDaoSupportException {

                   if (null == clazz) {

                            throw new HibernateDaoSupportException(

                                               "Param(#clazz) with value null");

                   }

                   String hql = "FROM " + clazz.getName();

                   List<T> lists = null;

                   try {

                            if (isCacheQueries) {

                                     this.getHibernateTemplate().setCacheQueries(true);

                            } else {

                                     isCacheQueries = true;

                                     this.getHibernateTemplate().setCacheQueries(false);

                            }

                            lists = (List<T>) this.getHibernateTemplate().find(hql);

                            log.debug("Execute find method is successful!");

                   } catch (DataAccessException e) {

                            log.error("Error finding entity:{}", e);

                            throw new HibernateDaoSupportException("Error finding (#"

                                               + this.getClass().getName() + "#):" + e);

                   }

                   return lists;

         }

         /**

          * The <code>findById(PK id)</code> method is find object according primary

          * key.

          * @param id

          *            if you want to find object's primary key

          * @return T insject object

          * @throws HibernateDaoSupportException

          *             Throws HibernateDaoSupportException when accessing and

          *             manipulating database happen exception.

          */

         @SuppressWarnings("unchecked")

         public T findById(PK id, Class<T> clazz)

                            throws HibernateDaoSupportException {

                   if (null == id) {

                            throw new HibernateDaoSupportException("PK with value null");

                   }

                   T entity = null;

                   String hql = "FROM " + clazz.getName() + " n where n.id = ";

                   if (id instanceof String) {

                            hql += "'" + id + "'";

                   } else {

                            hql += id;

                   }

                   try {

                            // use 2 leave cache

                            entity = (T) this.getUniqueBeanResult(hql);

                            log.debug("Execute findById method is successful!");

                   } catch (DataAccessException e) {

                            log.error("Error finding entity:{}", e);

                            throw new HibernateDaoSupportException("Error finding ("

                                               + this.getClass().getName() + "):" + e);

                   }

                   return entity;

         }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值