设计IBseDao接口,定义公共的CRUD方法。
// IBaseDao 接口,定义公共的CRUD方法 public interface IBaseDao<T> { public void add(T t); public void update(T t); public void delete(T t); public T getById(int id); public List<T> getList(int startIndex,int pageSize); public int getTotalCount(); }
// BaseDaoImpl接口,实现公共的CRUD方法 public class BaseDaoImpl<T> extends HibernateDaoSupport implements IBaseDao<T> { private Class<T> entityClass; // 子类的实例化需调用父类的无参构造方法,通过构造方法获取父类的泛型类型 public BaseDaoImpl() { // this.getClass() --> 子类的class对象, getGenericSuperclass() ---> 获取父类的Type ParameterizedType pType = (ParameterizedType) this.getClass().getGenericSuperclass(); // 获取第一个类型参数的真实类型,只有一个泛型参数,所以写0 // ParameterizedType 参数化类型, getActualTypeArguments(),返回表示此类型实际类型参数的Type对象的数组 entityClass = (Class<T>) pType.getActualTypeArguments()[0]; } @Override public void add(T t) { // 增加 this.getHibernateTemplate().save(t); } @Override public void update(T t) {// 修改 this.getHibernateTemplate().update(t); } @Override public void delete(T t) {// 删除 this.getHibernateTemplate().delete(t); } @Override public T getById(int id) { // 查询单个对象 return this.getHibernateTemplate().get(entityClass, id); } @Override public List<T> getList(int startIndex, int pageSize) { // 分页查询 return this.getHibernateTemplate().execute(new HibernateCallback<List<T>>() { @Override public List<T> doInHibernate(Session session) throws HibernateException { Query query = session.createQuery("from " + entityClass.getName()); query.setFirstResult(startIndex); query.setMaxResults(pageSize); return query.list(); } }); } @Override public int getTotalCount() { // 查询总记录数 return this.getHibernateTemplate().execute(new HibernateCallback<Integer>() { @Override public Integer doInHibernate(Session session) throws HibernateException { Query query = session.createQuery("select count(*) from " + entityClass.getName()); Long count = (Long) query.uniqueResult(); return count.intValue(); } }); } }
// IUserDao 继承 IBaseDao接口 public interface IUserDao extends IBaseDao<User> { }
// UserDaoImpl 继承BaseDaoImpl类,实现 IUserDao接口 public class UserDaoImpl extends BaseDaoImpl<User> implements IUserDao { }
==========================================================================
Service层同理,设计IBseService接口,定义公共的CRUD方法。
// IBaseService接口,定义CRUD方法 public interface IBaseService<T> { public void add(T t); public void update(T t); public void delete(T t); public T getById(int id); public List<T> getList(int startIndex,int pageSize); public Page<T> getPage(String currentPage); }
// BaseServiceImpl类,实现IBaseService接口 public abstract class BaseServiceImpl<T> implements IBaseService<T> { //提供一个抽象方法,让子类返回对应的子类Dao public abstract IBaseDao<T> getBaseDao(); @Override public void add(T t) { getBaseDao().add(t); } @Override public void update(T t) { getBaseDao().update(t); } @Override public void delete(T t) { getBaseDao().delete(t); } @Override public T getById(int id) { return getBaseDao().getById(id); } @Override public List<T> getList(int startIndex, int pageSize) { return getBaseDao().getList(startIndex, pageSize); } @Override public Page<T> getPage(String currentPage) { Page<T> page = new Page<>(1, 3); if(currentPage!=null){ page.setCurrentPage(Integer.parseInt(currentPage)); } int totalCount = getBaseDao().getTotalCount(); int totalPage = totalCount%page.getPageSize()==0?totalCount/page.getPageSize():totalCount/page.getPageSize()+1; List<T> list = getList((page.getCurrentPage()-1)*page.getPageSize(),page.getPageSize()); page.setTotalPage(totalPage); page.setList(list); return page; } }
// IUserService 继承 IBaseService接口 public interface IUserService extends IBaseService<User> { }
// UserServiceImpl 继承 BaseServiceImpl类,实现IUserService 接口 public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService { private IUserDao userDao; public IUserDao getUserDao() { return userDao; } public void setUserDao(IUserDao userDao) { this.userDao = userDao; } @Override public IBaseDao<User> getBaseDao() { return userDao; } }