标签:实现 rac ase out eric ide outline curd amp
第 1 步:编写接口 BaseDaopublic 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(); }
@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); } }
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; } }
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(); }
@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; } }
标签:实现 rac ase out eric ide outline curd amp
原文地址:http://blog.51cto.com/13665344/2153555