⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basedaoimlp.java

📁 动态实现基于角色的权限管理Acegi+hibernate
💻 JAVA
字号:
package sample.dao;
import java.util.Collection;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.springframework.dao.DataAccessException;
public class BaseDaoImlp extends AbstractManager implements IBaseDao{
	
	private Logger log = Logger.getLogger(this.getClass());


	/**
	 * 
	 * @param entity
	 * @return criteria 
	 * @throws DataAccessException
	 */
	public Criteria getCriteria(Class entity) throws DataAccessException {
		
		return super.getCriteria(entity);
	}

	/**
	 * 
	 * @param entity
	 * @param id as Integer
	 * @return Object
	 * @throws DataAccessException
	 */
	public Object getByPk(Class entity, Integer id) throws DataAccessException {
		
		Object obj = null;
		try {
			obj = super.getByPk(entity,id);
			
		} catch (DataAccessException exception) {
			log.error("查找 " + entity.getName() + " 实例到数据库失败", exception);

		}
		return obj;

	}

	/**
	 * 
	 * @param entity
	 * @param id as Long
	 * @return Object
	 * @throws DataAccessException
	 */
	public Object getByPk(Class entity, Long id) throws DataAccessException {
		
		Object obj = null;
		try {
			obj = super.getByPk(entity,id);
	
		} catch (DataAccessException exception) {
			log.error("查找 " + entity.getName() + " 实例到数据库失败", exception);
            throw exception;
		}
		return obj;
	}

	/**
	 * 
	 * @param entity
	 * @param id as String
	 * @return Object
	 * @throws DataAccessException
	 */
	public Object getByPk(Class entity, String id) throws DataAccessException {
		
		Object obj = null;
		try {
			obj = super.getByPk(entity,id);
			
		} catch (DataAccessException exception) {
			log.error("查找 " + entity.getName() + " 实例到数据库失败", exception);
			throw exception;
		}
		return obj;
	}


	/**
	 * create a date into DB
	 * @param entity
	 * @throws DataAccessException
	 */
	public void create(Object entity) throws DataAccessException {
		try {
			//getHibernateTemplate().save(entity);
			super.save(entity);
		} catch (DataAccessException exception) {
			log.error("创建 " + entity.getClass().getName() + " 实例到数据库失败",
					exception);
			throw exception;
		}

	}
	/**
	 * update a date from DB
	 * @param entity
	 * @throws DataAccessException
	 */
	public void update(Object entity) throws DataAccessException {
		
		try {
			
			super.update(entity);
		} catch (DataAccessException exception) {
			log.error("更新 " + entity.getClass().getName() + " 实例到数据库失败",
					exception);
			throw exception;
		}
	}
	/**
	 * delete a date from DB
	 * @param entity
	 * @throws DataAccessException
	 */
	public void delete(Object entity) throws DataAccessException {
		
		try {
			
			super.delete(entity);
		} catch (DataAccessException exception) {
			log.error("从数据库删除 " + entity.getClass().getName() + " 实例失败",
					exception);
			throw exception;
		}

	}

	/**
	 * delete all dates from DB
	 * @param entity
	 * @throws DataAccessException
	 */
	public void deleteAll(Class entity) throws DataAccessException {
		
		try {
			List result = getHibernateTemplate().loadAll(entity);
			getHibernateTemplate().deleteAll(result);
		
		} catch (DataAccessException exception) {
			log.error("从数据库删除 " + entity.getName() + " 的所有记录失败", exception);
			throw exception;
		}
	}

	/**
	 * delete all Collection from DB
	 * @param Collection entities
	 * @throws DataAccessException
	 */
	public void deleteAll(Collection entities) throws DataAccessException {
		
		try {
			getHibernateTemplate().deleteAll(entities);

		} catch (DataAccessException exception) {
			log.error("从数据库删除  的所有记录失败", exception);
			throw exception;
		}
	}

	/**
	 * 根据keyName,keyValue,查询得到list然后返回第一个值
	 * @param Class entity
	 * @param String keyName
	 * @param Object keyValue
	 * @return Object
	 * @throws DataAccessException
	 */
	public Object loadByKey(Class entity, String keyName, Object keyValue)
			throws DataAccessException {
		
		try {
			List result = getHibernateTemplate().find(
					"from " + entity.getName() + " where " + keyName + " = ?",
					keyValue);
			if (result != null && result.size() > 0) {
				return result.get(0);
			} else {
				return null;
			}
		} catch (DataAccessException exception) {
			log.error("查询数据库 " + entity.getName(), exception);
			throw exception;
		}
	}


	/**
	 * 
	 * @param entity
	 * @return List
	 * @throws DataAccessException
	 */
	public List loadAll(Class entity) throws DataAccessException {
		
		List list = null;
		try {
			list=getHibernateTemplate().loadAll(entity);
		} catch (DataAccessException exception) {
			log.error("从数据库查询" + entity.getName() + "失败");
			throw exception;
		}
		return list;
	}

	public Object loadByPK(Class clazz,String id) throws DataAccessException {
		
		Object object=null;
		try {
			object = super.loadByPriKey(clazz,id);
		} catch (DataAccessException exception) {
			log.error("从数据库查询" + clazz.getName() + "失败");
			throw exception;
		}
		return object;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -