上一篇咱们介绍《构建dubbo分布式平台-maven构建ant-framework核心代码annotation》,今天重点讲解的是ant-framework核心代码Base封装过程。java
由于涉及到springmvc、mybatis的集成,为了使项目编码更简洁易用,这边将基础的BASE进行封装,其中包括:BaseBean、BaseDao、BaseService、CRUD的基础封装、分页组件的封装、mybatis的mapper的基础封装,各类数据源支持的封装等。spring
1. BaseEntity基础封装,代码以下:mybatis
/** * Entity基础封装 */ public abstract class BaseEntity<T> implements Serializable { private static final long serialVersionUID = 1234567890987654321L; /** * 实体编号(惟一标识) */ protected String id; /** * 当前实体分页对象 */ protected Page<T> page; /** * 是否插入新纪录 */ protected boolean isNewRecord = false; public BaseEntity() { } public BaseEntity(String id) { this(); this.id = id; } public String getId() { return id; } public void setId(String id) { this.id = id; } /** * 数据插入以前 */ public abstract void preInsert(); /** * 更新数据以前 */ public abstract void preUpdate(); /** * 是不是新记录(默认:false) */ public boolean getIsNewRecord() { return isNewRecord || StringUtils.isBlank(getId()); } /** * 是不是新记录(默认:false) */ public void setIsNewRecord(boolean isNewRecord) { this.isNewRecord = isNewRecord; } /** * 全局变量对象 */ @JsonIgnore public Global getGlobal() { return Global.getInstance(); } @Override public boolean equals(Object obj) { if (null == obj) { return false; } if (this == obj) { return true; } if (!getClass().equals(obj.getClass())) { return false; } BaseEntity<?> that = (BaseEntity<?>) obj; return null == this.getId() ? false : this.getId().equals(that.getId()); } }
2. BaseDao的基础封装(这个很简单,由于使用的是mybatis集成方案,只须要保留接口便可),代码以下:mvc
public interface BaseDao { }
3. CrudDao的基础封装app
/** * DAO基础封装 */ public interface CrudDao<T> extends BaseDao { /** * 获取单条数据 * @param id * @return */ public T get(String id); /** * 获取单条数据 * @param entity * @return */ public T get(T entity); /** * 查询数据列表,若是须要分页,请设置分页对象,如:entity.setPage(new Page<T>()); * @param entity * @return */ public List<T> findList(T entity); /** * 查询全部数据列表 * @param entity * @return */ public List<T> findAllList(T entity); /** * 查询全部数据列表 * @see public List<T> findAllList(T entity) * @return */ @Deprecated public List<T> findAllList(); /** * 插入数据 * @param entity * @return */ public int insert(T entity); /** * 更新数据 * @param entity * @return */ public int update(T entity); /** * 删除数据 * @param id * @see public int delete(T entity) * @return */ @Deprecated public int delete(String id); /** * 删除数据 * @param entity * @return */ public int delete(T entity); }
4. BaseService的基础封装(里面封装了基础的CRUD操做,包括基础get,find,insert,update等)框架
/** * BaseService基础封装 */ @Transactional(readOnly = true) public abstract class CrudService<D extends CrudDao<T>, T extends DataEntity<T>> extends BaseService { /** * 持久层dao */ @Autowired protected D dao; /** * 获取单条数据 * @param id * @return */ public T get(String id) { return dao.get(id); } /** * 获取单条数据 * @param entity * @return */ public T get(T entity) { return dao.get(entity); } /** * 查询列表数据 * @param entity * @return */ public List<T> findList(T entity) { return dao.findList(entity); } /** * 查询分页数据 * @param page 分页对象 * @param entity * @return */ public Page<T> findPage(Page<T> page, T entity) { entity.setPage(page); page.setList(dao.findList(entity)); return page; } /** * 保存数据(插入或更新) * @param entity */ @Transactional(readOnly = false) public void save(T entity) { if (entity.getIsNewRecord()){ entity.preInsert(); dao.insert(entity); }else{ entity.preUpdate(); dao.update(entity); } } /** * 删除数据 * @param entity */ @Transactional(readOnly = false) public void delete(T entity) { dao.delete(entity); } }
文章内容不写太多,但愿你们可以掌握每个知识点,基础的CRUD,BASE的封装差很少都在这里,后面会继续补充,具体的业务和实现后面会讲解到。maven
愿意了解框架技术或者源码的朋友直接求求交流分享技术2042849237分布式
欢迎你们跟我一块儿学习《构建dubbo分布式平台》,但愿你们持续关注后面的文章!ide