package com.yangbo.oa.base; import java.util.List; public interface BaseDao<T> { /** * 保存实体 * @param entity */ void save(T entity); /** * 删除实体 * @param id */ void delete(Long id); /** * 更新实体 * @param entity */ void update(T entity); /** * 按id查询 * @param id * @return */ T getById(Long id); /** * 按id查询 * @param ids * @return */ List<T> getByIds(Long[] ids); /** * 查询所有 * @return */ List<T> findAll(); }
package com.yangbo.oa.dao; import com.yangbo.oa.base.*; import com.yangbo.oa.domain.*; public interface UserDao extends BaseDao<User> { }
package com.yangbo.oa.dao; import com.yangbo.oa.base.*; import com.yangbo.oa.domain.*; public interface RoleDao extends BaseDao<Role>{ }
package com.yangbo.oa.base; import java.lang.reflect.ParameterizedType; import java.util.List; import javax.annotation.Resource; import org.hibernate.Session; import org.hibernate.SessionFactory; public class BaseDaoImpl<T> implements BaseDao<T> { @Resource private SessionFactory sessionFactory; private Class<T> clazz; public BaseDaoImpl(){ //使用反射技术得到T的真实类型 //获取当前new的对象的泛型的父类 ParameterizedType pt =(ParameterizedType) this.getClass().getGenericSuperclass(); //获取第一个类型参数的真实类型 this.clazz = (Class<T>) pt.getActualTypeArguments()[0]; System.out.println("clazz--->"+clazz); } /** * 获取当前可用的Session对象 * @return */ protected Session getSession(){ return sessionFactory.getCurrentSession(); } public void delete(Long id) { Object obj = getById(id); if(obj!=null){ getSession().delete(obj); } } @SuppressWarnings("unchecked") public List<T> findAll() { return getSession().createQuery( "FROM "+clazz.getSimpleName()) .list(); } @SuppressWarnings("unchecked") public T getById(Long id) { return (T)getSession().get(clazz, id); } @SuppressWarnings("unchecked") public List<T> getByIds(Long[] ids) { return getSession().createQuery( "FROM User WHERE id IN (:ids)") .setParameterList("ids",ids) .list(); } public void save(T entity) { getSession().save(entity); } public void update(T entity) { // TODO Auto-generated method stub } }
package com.yangbo.oa.dao.impl; import java.util.List; import com.yangbo.oa.base.BaseDaoImpl; import com.yangbo.oa.dao.UserDao; import com.yangbo.oa.domain.User; public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao{ }
package com.yangbo.oa.dao.impl; import java.util.List; import com.yangbo.oa.base.BaseDaoImpl; import com.yangbo.oa.dao.RoleDao; import com.yangbo.oa.domain.Role; public class RoleDaoImpl extends BaseDaoImpl<Role> implements RoleDao{ }