基于泛型的BaseDao,BaseService实现增强版
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于泛型的BaseDao,BaseService实现增强版相关的知识,希望对你有一定的参考价值。
第 1 步:编写接口 BaseDao
public interface BaseDao<ID extends Serializable, T extends AbstractEntity, I extends BaseDao<?, ?, ?>> { boolean save(T u); boolean deleteById(ID id); boolean update(T u); T getById(ID id); List<T> getAll(); }
第 2 步:编写基本实现类 BaseDaoImpl
@SuppressWarnings("all") public abstract class BaseDaoImpl<ID extends Serializable, T extends AbstractEntity, I extends BaseDao<ID, T, I>> implements BaseDao<ID, T, I> { @Resource(name = "sqlSessionFactory") protected SqlSessionFactory sqlSessionFactory; /** 当前接口类型 */ private Class<I> curDao; public BaseDaoImpl() { ParameterizedType pt = (ParameterizedType)getClass().getGenericSuperclass(); this.curDao = ((Class)pt.getActualTypeArguments()[2]); } @Override public boolean save(T u) { return getMapper().save(u); } @Override public boolean deleteById(ID id) { return getMapper().deleteById(id); } @Override public boolean update(T u) { return getMapper().update(u); } @Override public T getById(ID id) { return getMapper().getById(id); } @Override public List<T> getAll() { return getMapper().getAll(); } protected SqlSession getSession() { return sqlSessionFactory.openSession(); } protected I getMapper() { return getSession().getMapper(curDao); } }
注:AbstractEntity为通用实体抽象父类,可用于定义通用的字段,例如
public abstract class AbstractEntity implements Serializable { private static final long serialVersionUID = 66532814011080848L; private Long id; //创建时间 private Date createDate; //修改时间 private Date modifyDate; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public Date getCreateDate() { return createDate; } public void setCreateDate(Date createDate) { this.createDate = createDate; } public Date getModifyDate() { return modifyDate; } public void setModifyDate(Date modifyDate) { this.modifyDate = modifyDate; } }
第 3 步:编写 BaseService
public interface BaseService<ID extends Serializable, T extends AbstractEntity, I extends BaseDao<?, ?, ?>> { boolean save(T u); boolean deleteById(ID id); boolean update(T u); T getById(ID id); List<T> getAll(); }
第 4 步:编写 BaseServiceImpl
@Transactional public abstract class BaseServiceImpl<ID extends Serializable, T extends AbstractEntity, I extends BaseDao<ID, T, I>> implements BaseService<ID, T, I> { /** 基础DAO */ private BaseDao<ID, T, I> baseDao; @Override public boolean save(T u) { if (u != null && u.getCreateDate() == null) { u.setCreateDate(new Date()); } if (u != null && u.getModifyDate() == null) { u.setModifyDate(new Date()); } return baseDao.save(u); } @Override public boolean deleteById(ID id) { return baseDao.deleteById(id); } @Override public boolean update(T u) { if (u != null && u.getModifyDate() != null) { u.setModifyDate(new Date()); } return baseDao.update(u); } @Override public T getById(ID id) { return baseDao.getById(id); } @Override public List<T> getAll() { return baseDao.getAll(); } public void setBaseDao(BaseDao<ID, T, I> baseDao) { this.baseDao = baseDao; } }
以上是关于基于泛型的BaseDao,BaseService实现增强版的主要内容,如果未能解决你的问题,请参考以下文章