本文的基本DAO是基于hibernate与java的反射机制编写的。- package com.gmxt.baseDao.baseDaoImpl;
- import java.lang.reflect.ParameterizedType;
- import java.util.Collections;
- import java.util.List;
- import javax.annotation.Resource;
- import org.hibernate.Query;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.springframework.transaction.annotation.Transactional;
- import com.gmxt.baseDao.BaseDao;
- import com.gmxt.common.Pagination;
- @Transactional
- @SuppressWarnings("unchecked")
- public abstract class BaseDaoImpl<T> implements BaseDao<T> {
- @Resource
- private SessionFactory sessionFactory;
- protected Class<T> clazz;
- public BaseDaoImpl() {
- ParameterizedType type = (ParameterizedType) this.getClass()
- .getGenericSuperclass();
- this.clazz = (Class) type.getActualTypeArguments()[0];
- }
- public void delete(String id) {
- Object object = (Object) getSession().get(clazz, id);
- getSession().delete(object);
- }
- public T getById(String id) {
- return (T) getSession().get(clazz, id);
- }
- public List<T> getAll() {
- return getSession().createQuery("from " + clazz.getSimpleName()).list();
- }
复制代码 |
|