(9) Build dubbo distributed platform - maven builds ant-framework core code Base package

In the last article, we introduced "Building a Dubbo Distributed Platform-maven Builds Ant-framework Core Code Annotation". Today, the focus is on the ant-framework core code Base packaging process.

 

Because it involves the integration of springmvc and mybatis, in order to make the project coding more concise and easy to use, the basic BASE is encapsulated here, including: BaseBean, BaseDao, BaseService, CRUD basic encapsulation, paging component encapsulation, mybatis mapper Basic encapsulation, encapsulation supported by various data sources, etc.

 

1. BaseEntity basic package, the code is as follows:

/**
 * Entity base encapsulation
 */
public abstract class BaseEntity<T> implements Serializable {

	private static final long serialVersionUID = 1234567890987654321L;

	/**
	 * Entity number (unique identifier)
	 */
	protected String id;
	
	/**
	 * Current entity paging object
	 */
	protected Page<T> page;
	

	/**
	 * whether to insert a new record
	 */
	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;
	}

	/**
	 * before data insertion
	 */
	public abstract void preInsert();
	
	/**
	 * before updating data
	 */
	public abstract void preUpdate();
	
       /**
	 * is a new record (default: false)
        */
	public boolean getIsNewRecord() {
        return isNewRecord || StringUtils.isBlank(getId());
    }

	/**
	 * is a new record (default: false)
	 */
	public void setIsNewRecord(boolean isNewRecord) {
		this.isNewRecord = isNewRecord;
	}

	/**
	 * global variable object
	 */
	@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. The basic package of BaseDao (this is very simple, because the mybatis integration scheme is used, and only the interface needs to be reserved), the code is as follows:

public interface BaseDao {
}

 

3. CrudDao's basic package

/**
 * DAO basic package
 */
public interface CrudDao<T> extends BaseDao {

	/**
	 * Get a single piece of data
	 * @param id
	 * @return
	 */
	public T get(String id);
	
	/**
	 * Get a single piece of data
	 * @param entity
	 * @return
	 */
	public T get(T entity);
	
	/**
	 * Query the data list, if you need paging, please set the paging object, such as: entity.setPage(new Page<T>());
	 * @param entity
	 * @return
	 */
	public List<T> findList(T entity);
	
	/**
	 * Query all data list
	 * @param entity
	 * @return
	 */
	public List<T> findAllList(T entity);
	
	/**
	 * Query all data list
	 * @see public List<T> findAllList(T entity)
	 * @return
	 */
	@Deprecated
	public List<T> findAllList();
	
	/**
	 * insert data
	 * @param entity
	 * @return
	 */
	public int insert(T entity);
	
	/**
	 * update data
	 * @param entity
	 * @return
	 */
	public int update(T entity);
	
	/**
	 * delete data
	 * @param id
	 * @see public int delete(T entity)
	 * @return
	 */
	@Deprecated
	public int delete(String id);
	
	/**
	 * delete data
	 * @param entity
	 * @return
	 */
	public int delete(T entity);
	
}

 4. Basic encapsulation of BaseService (which encapsulates basic CRUD operations, including basic get, find, insert, update, etc.)

/**
 * BaseService basic package
 */
@Transactional(readOnly = true)
public abstract class CrudService<D extends CrudDao<T>, T extends DataEntity<T>> extends BaseService {
	
	/**
	 * Persistence layer dao
	 */
	@Autowired
	protected D dao;
	
	/**
	 * Get a single piece of data
	 * @param id
	 * @return
	 */
	public T get(String id) {
		return dao.get(id);
	}
	
	/**
	 * Get a single piece of data
	 * @param entity
	 * @return
	 */
	public T get(T entity) {
		return dao.get(entity);
	}
	
	/**
	 * Query list data
	 * @param entity
	 * @return
	 */
	public List<T> findList(T entity) {
		return dao.findList(entity);
	}
	
	/**
	 * Query paging data
	 * @param page paging object
	 * @param entity
	 * @return
	 */
	public Page<T> findPage(Page<T> page, T entity) {
		entity.setPage(page);
		page.setList(dao.findList(entity));
		return page;
	}

	/**
	 * save data (insert or update)
	 * @param entity
	 */
	@Transactional(readOnly = false)
	public void save(T entity) {
		if (entity.getIsNewRecord()){
			entity.preInsert();
			dao.insert(entity);
		}else{
			entity.preUpdate();
			dao.update(entity);
		}
	}
	
	/**
	 * delete data
	 * @param entity
	 */
	@Transactional(readOnly = false)
	public void delete(T entity) {
		dao.delete(entity);
	}
}

 

The content of the article is not written too much. I hope you can master every knowledge point. The basic CRUD and BASE packaging are almost all here. We will continue to add them later. The specific business and implementation will be explained later.

 

Welcome everyone to learn "Building a Dubbo Distributed Platform" with me, and I hope you will continue to pay attention to the following articles!

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326154740&siteId=291194637