今天重点讲解的是ant-framework核心代码Base封装过程。
因为涉及到springmvc、mybatis的集成,为了使项目编码更简洁易用,这边将基础的BASE进行封装,其中包括:BaseBean、BaseDao、BaseService、CRUD的基础封装、分页组件的封装、mybatis的mapper的基础封装,各种数据源支持的封装等。
- BaseEntity基础封装,代码如下:
/**
``* Entity基础封装
``*/
publicabstractclassBaseEntity<T> ``implementsSerializable {
``privatestaticfinallongserialVersionUID = 1234567890987654321L;
``/**
``* 实体编号(唯一标识)
``*/
``protectedString id;
``/**
``* 当前实体分页对象
``*/
``protectedPage<T> page;
``/**
``* 是否插入新纪录
``*/
``protectedbooleanisNewRecord = ``false``;
``publicBaseEntity() {
``}
``publicBaseEntity(String id) {
``this``();
``this``.id = id;
``}
``publicString getId() {
``returnid;
``}
``publicvoidsetId(String id) {
``this``.id = id;
``}
``/**
``* 数据插入之前
``*/
``publicabstractvoidpreInsert();
``/**
``* 更新数据之前
``*/
``publicabstractvoidpreUpdate();
``/**
``* 是否是新记录(默认:false)
``*/
``publicbooleangetIsNewRecord() {
``returnisNewRecord || StringUtils.isBlank(getId());
``}
``/**
``* 是否是新记录(默认:false)
``*/
``publicvoidsetIsNewRecord(``booleanisNewRecord) {
``this``.isNewRecord = isNewRecord;
``}
``/**
``* 全局变量对象
``*/
``@JsonIgnore
``publicGlobal getGlobal() {
``returnGlobal.getInstance();
``}
``@Override
``publicbooleanequals(Object obj) {
``if(``null== obj) {
``returnfalse``;
``}
``if(``this== obj) {
``returntrue``;
``}
``if(!getClass().equals(obj.getClass())) {
``returnfalse``;
``}
``BaseEntity<?> that = (BaseEntity<?>) obj;
``returnnull== ``this``.getId() ? ``false: ``this``.getId().equals(that.getId());
``}
}
2. BaseDao的基础封装(这个很简单,因为使用的是mybatis集成方案,只需要保留接口即可),代码如下:
publicinterfaceBaseDao {
}
- CrudDao的基础封装
/**
``* DAO基础封装
``*/
publicinterfaceCrudDao<T> ``extendsBaseDao {
``/**
``* 获取单条数据
``* @param id
``* @return
``*/
``publicT get(String id);
``/**
``* 获取单条数据
``* @param entity
``* @return
``*/
``publicT get(T entity);
``/**
``* 查询数据列表,如果需要分页,请设置分页对象,如:entity.setPage(new Page<T>());
``* @param entity
``* @return
``*/
``publicList<T> findList(T entity);
``/**
``* 查询所有数据列表
``* @param entity
``* @return
``*/
``publicList<T> findAllList(T entity);
``/**
``* 查询所有数据列表
``* @see public List<T> findAllList(T entity)
``* @return
``*/
``@Deprecated
``publicList<T> findAllList();
``/**
``* 插入数据
``* @param entity
``* @return
``*/
``publicintinsert(T entity);
``/**
``* 更新数据
``* @param entity
``* @return
``*/
``publicintupdate(T entity);
``/**
``* 删除数据
``* @param id
``* @see public int delete(T entity)
``* @return
``*/
``@Deprecated
``publicintdelete(String id);
``/**
``* 删除数据
``* @param entity
``* @return
``*/
``publicintdelete(T entity);
}
- BaseService的基础封装(里面封装了基础的CRUD操作,包括基础get,find,insert,update等)
/**
``* BaseService基础封装
``*/
@Transactional``(readOnly = ``true``)
publicabstractclassCrudService<D ``extendsCrudDao<T>, T ``extendsDataEntity<T>> ``extendsBaseService {
``/**
``* 持久层dao
``*/
``@Autowired
``protectedD dao;
``/**
``* 获取单条数据
``* @param id
``* @return
``*/
``publicT get(String id) {
``returndao.get(id);
``}
``/**
``* 获取单条数据
``* @param entity
``* @return
``*/
``publicT get(T entity) {
``returndao.get(entity);
``}
``/**
``* 查询列表数据
``* @param entity
``* @return
``*/
``publicList<T> findList(T entity) {
``returndao.findList(entity);
``}
``/**
``* 查询分页数据
``* @param page 分页对象
``* @param entity
``* @return
``*/
``publicPage<T> findPage(Page<T> page, T entity) {
``entity.setPage(page);
``page.setList(dao.findList(entity));
``returnpage;
``}
``/**
``* 保存数据(插入或更新)
``* @param entity
``*/
``@Transactional``(readOnly = ``false``)
``publicvoidsave(T entity) {
``if(entity.getIsNewRecord()){
``entity.preInsert();
``dao.insert(entity);
``}``else``{
``entity.preUpdate();
``dao.update(entity);
``}
``}
``/**
``* 删除数据
``* @param entity
``*/
``@Transactional``(readOnly = ``false``)
``publicvoiddelete(T entity) {
``dao.delete(entity);
``}
}
5.架构代码如下:
