基於DAO、Service、Action代碼結構,常常有一些通用的代碼段。
泛型DAO模板:BaseDAO:java
package com.baizhi.commons.dao; import java.util.List; import com.baizhi.commons.query.BaseQuery; /** * 時間:2017年4月10日 下午3:16:44 * TODO: 通用泛型DAO */ public interface BaseDAO<T> { Integer insert(T t); void update(T t); T getById(Long id); void deleteById(Long id); void delete(BaseQuery baseQuery); List<T> getList(BaseQuery baseQuery); Integer getCount(BaseQuery baseQuery); void deleteAll(); T getByGuId(String guid); }
通用分頁實體:ui
package com.baizhi.commons.page; import java.util.List; /** * 時間: 2017年4月10日 下午3:10:29 * TODO: 通用分頁 */ public class Page<T> { public static final int LIMIT = 15; private List<T> result; private int totalPage; private int total; private int currentPage; private int limit = LIMIT; public List<T> getResult() { return result; } public void setResult(List<T> result) { this.result = result; } public int getTotalPage() { if(limit == 0 || total == 0){ return 1; } totalPage = total % limit == 0 ? (total / limit): (total / limit) + 1; return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public int getTotal() { return total; } public void setTotal(int total) { this.total = total; } public int getLimit() { return limit; } public void setLimit(int limit) { this.limit = limit; } public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } }
通用的實體:BaseDOthis
import java.io.Serializable; import java.util.Date; /** * 時間:2017年4月10日 下午3:10:19 * TODO: 通用實體 */ public class BaseDO implements Serializable { private static final long serialVersionUID = -636162491382184010L; private Long id; private Date gmtCreate; private Date gmtModified; private Boolean isDelete= false; public Long getId(){ return id; } public void setId(Long id) { this.id = id; } public Date getGmtCreate() { return gmtCreate; } public void setGmtCreate(Date gmtCreate) { this.gmtCreate = gmtCreate; } public Date getGmtModified() { return gmtModified; } public void setGmtModified(Date gmtModified) { this.gmtModified = gmtModified; } public Boolean getIsDelete() { return isDelete; } public void setIsDelete(Boolean isDelete) { this.isDelete = isDelete; } }
通用查詢對象:code
/** * 時間:2017年4月10日 下午3:11:05 * TODO: 查詢的基本DO */ public class BaseQuery implements Serializable { private static final long serialVersionUID = 5412114202257664115L; private Integer currentPage; private Integer startRow; private Integer limit = 20; private Date startDate; private Date endDate; private boolean isQuery; private String orgNo; private Boolean isDelete = false; private Long id; private Boolean notCancle; public Integer getStartRow() { if (currentPage != null && currentPage > 0) { startRow = (currentPage - 1) * limit; } return startRow; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public void setStartRow(Integer startRow) { this.startRow = startRow; } public Integer getLimit() { return limit; } public void setLimit(Integer limit) { this.limit = limit; } public Date getStartDate() { return startDate; } public void setStartDate(Date startDate) { this.startDate = startDate; } public Date getEndDate() { return endDate; } public void setEndDate(Date endDate) { this.endDate = endDate; } public Integer getCurrentPage() { return currentPage; } public void setCurrentPage(Integer currentPage) { this.currentPage = currentPage; if (currentPage != null && currentPage > 0) { this.startRow = (currentPage - 1) * limit; } } public boolean isQuery() { return isQuery; } public void setQuery(boolean isQuery) { this.isQuery = isQuery; } public Boolean getIsDelete() { return isDelete; } public void setIsDelete(Boolean isDelete) { this.isDelete = isDelete; } public String getOrgNo() { return orgNo; } public void setOrgNo(String orgNo) { this.orgNo = orgNo; } public Boolean getNotCancle() { return notCancle; } public void setNotCancle(Boolean notCancle) { this.notCancle = notCancle; } }