设计IBseDao接口,定义公共的CRUD方法。
// IBaseDao 接口,定义公共的CRUD方法public interface IBaseDao{ public void add(T t); public void update(T t); public void delete(T t); public T getById(int id); public List getList(int startIndex,int pageSize); public int getTotalCount();}
// BaseDaoImpl接口,实现公共的CRUD方法public class BaseDaoImplextends HibernateDaoSupport implements IBaseDao { private Class entityClass; // 子类的实例化需调用父类的无参构造方法,通过构造方法获取父类的泛型类型 public BaseDaoImpl() { // this.getClass() --> 子类的class对象, getGenericSuperclass() ---> 获取父类的Type ParameterizedType pType = (ParameterizedType) this.getClass().getGenericSuperclass(); // 获取第一个类型参数的真实类型,只有一个泛型参数,所以写0 // ParameterizedType 参数化类型, getActualTypeArguments(),返回表示此类型实际类型参数的Type对象的数组 entityClass = (Class ) 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 getList(int startIndex, int pageSize) { // 分页查询 return this.getHibernateTemplate().execute(new HibernateCallback
>() { @Override public List 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 () { @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{}
// UserDaoImpl 继承BaseDaoImpl类,实现 IUserDao接口public class UserDaoImpl extends BaseDaoImplimplements IUserDao { }
==========================================================================
Service层同理,设计IBseService接口,定义公共的CRUD方法。
// IBaseService接口,定义CRUD方法public interface IBaseService{ public void add(T t); public void update(T t); public void delete(T t); public T getById(int id); public List getList(int startIndex,int pageSize); public Page getPage(String currentPage);}
// BaseServiceImpl类,实现IBaseService接口public abstract class BaseServiceImplimplements IBaseService { //提供一个抽象方法,让子类返回对应的子类Dao public abstract IBaseDao 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 getList(int startIndex, int pageSize) { return getBaseDao().getList(startIndex, pageSize); } @Override public Page getPage(String currentPage) { Page 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 list = getList((page.getCurrentPage()-1)*page.getPageSize(),page.getPageSize()); page.setTotalPage(totalPage); page.setList(list); return page; }}
// IUserService 继承 IBaseService接口public interface IUserService extends IBaseService{}
// UserServiceImpl 继承 BaseServiceImpl类,实现IUserService 接口public class UserServiceImpl extends BaseServiceImplimplements IUserService { private IUserDao userDao; public IUserDao getUserDao() { return userDao; } public void setUserDao(IUserDao userDao) { this.userDao = userDao; } @Override public IBaseDao getBaseDao() { return userDao; }}
posted on 2019-03-02 10:40 阅读( ...) 评论( ...)