抽象類和介面在專案中使用的例項
阿新 • • 發佈:2018-11-27
抽象類和介面在專案中經常使用如果使用恰當,可以大大精簡程式碼
抽象類:指的是用abstract關鍵字修飾或者類中有抽象方法,那麼這個類就是抽象類
特點:
- 抽象類用關鍵字 abstract修飾
- 抽象類的抽象方法沒有方法體,在子類繼承父類中有抽象方法時,必須實現抽像方法
- 抽象類只能被單繼承
介面:介面只有方法體沒有具體的實現,用inferce修飾
特點:
- 介面中沒有自己的屬性,只能有方法體
- 類在實現介面時,可以實現多個介面
程式碼演示:
使用抽象方法將dao層中,公共的增刪改查提取出來,讓其子類繼承公共的dao方法
package com.empl.mgr.dao.support; import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.criterion.Restrictions; import org.springframework.beans.factory.annotation.Autowired; public abstract class AbstractDao<T> { @Autowired private SessionFactory sessionFactory; public abstract Class<T> getEntityClass(); public Session findSession() { return sessionFactory.getCurrentSession(); } @SuppressWarnings("unchecked") public List<T> findAll() { return findSession().createCriteria(getEntityClass()).list(); } @SuppressWarnings("unchecked") public T findUniqueByProperty(String pro, Object val) { return (T) findSession().createCriteria(getEntityClass()).add(Restrictions.eq(pro, val)).uniqueResult(); } @SuppressWarnings("unchecked") public List<T> findByProperty(String pro, Object val) { return findSession().createCriteria(getEntityClass()).add(Restrictions.eq(pro, val)).list(); } public int findCounnt() { String query = "select count(*) from " + getEntityClass().getName(); return Integer.parseInt(findSession().createQuery(query).uniqueResult().toString()); } public int findCountLike(String pro, String val) { String query = "select count(*) from " + getEntityClass().getName() + " where " + pro + " like '%" + val + "%'"; return Integer.parseInt(findSession().createQuery(query).uniqueResult().toString()); } public int findCountByProperty(String pro, Object searchValue) { String query = "select count(*) from " + getEntityClass().getName() + " where " + pro + " = '" + searchValue + "'"; return Integer.parseInt(findSession().createQuery(query).uniqueResult().toString()); } public void deleteByProperty(String pro, Object value) { String query = "delete from " + getEntityClass().getName() + " where " + pro + "=" + value.toString(); findSession().createQuery(query).executeUpdate(); } public void deleteByPropertyString(String pro, Object val) { String query = "delete from " + getEntityClass().getName() + " where " + pro + "='" + val.toString() + "'"; findSession().createQuery(query).executeUpdate(); } @SuppressWarnings("unchecked") public T findById(long id) { return (T) findSession().get(getEntityClass().getName(), id); } public void save(Object obj) { findSession().save(obj); } public void delete(Object obj) { findSession().delete(obj); } }
package com.empl.mgr.dao; import java.util.List; import org.apache.commons.lang.StringUtils; import org.springframework.stereotype.Repository; import com.empl.mgr.constant.PageConstant; import com.empl.mgr.dao.support.AbstractDao; import com.empl.mgr.dto.RoleListDto; import com.empl.mgr.model.TeAccountRole; import com.empl.mgr.model.TeRole; @Repository public class RoleDao extends AbstractDao<TeRole> { @Override public Class<TeRole> getEntityClass() { // TODO Auto-generated method stub return TeRole.class; } @SuppressWarnings("unchecked") public List<RoleListDto> findRoleList(int page, String searchVal) { // TODO Auto-generated method stub StringBuffer query = new StringBuffer(); query.append("select new com.empl.mgr.dto.RoleListDto (roleId, roleName, roleDescription, createTime, creator) from TeRole "); query.append(StringUtils.isNotBlank(searchVal) ? "where roleName like '%" + searchVal + "%'" : ""); query.append("order by roleId desc "); return findSession().createQuery(query.toString()).setFirstResult((page - 1) * PageConstant.PAGE_LIST) .setMaxResults(PageConstant.PAGE_LIST).list(); } public int findRoleCount(String searchVal) { // TODO Auto-generated method stub StringBuffer query = new StringBuffer(); query.append("select count(roleId) from TeRole "); query.append(StringUtils.isNotBlank(searchVal) ? "where roleName like '%" + searchVal + "%'" : ""); return Integer.parseInt(findSession().createQuery(query.toString()).uniqueResult().toString()); } @SuppressWarnings("unchecked") public List<TeAccountRole> findMyCharacter(String acctName, String roleLabel) { StringBuffer query = new StringBuffer(); query.append("from TeAccountRole where acctName = ? and roleLabel = ?"); return findSession().createQuery(query.toString()).setString(0, acctName).setString(1, roleLabel).list(); } }
package com.empl.mgr.dao; import java.util.List; import org.apache.commons.lang.StringUtils; import org.springframework.stereotype.Repository; import com.empl.mgr.constant.AccountDeleteState; import com.empl.mgr.constant.PageConstant; import com.empl.mgr.dao.support.AbstractDao; import com.empl.mgr.dto.AccountListDto; import com.empl.mgr.model.TeAccount; @Repository public class AccountDao extends AbstractDao<TeAccount> { @Override public Class<TeAccount> getEntityClass() { // TODO Auto-generated method stub return TeAccount.class; } // long acctId, String acctName, String acctNickname, Date createTime, String creator @SuppressWarnings("unchecked") public List<AccountListDto> findAccountList(int page, String val) { // TODO Auto-generated method stub StringBuffer query = new StringBuffer(); query.append("select new com.empl.mgr.dto.AccountListDto "); query.append("(te.acctId, te.acctName, te.acctNickname, te.createTime, te.creator, te.acctSuper) "); query.append("from TeAccount te where te.acctDeleteState = ? "); query.append(StringUtils.isEmpty(val) ? "" : " and (te.acctNickname like '%" + val + "%' or te.acctName like '%" + val + "%')"); query.append("order by te.acctId desc"); return findSession().createQuery(query.toString()).setBoolean(0, AccountDeleteState.NO_DELETE) .setFirstResult((page - 1) * PageConstant.PAGE_LIST).setMaxResults(PageConstant.PAGE_LIST).list(); } public int findAccountPage(String val) { // TODO Auto-generated method stub StringBuffer query = new StringBuffer(); query.append("select count(te.acctId) from TeAccount te where te.acctDeleteState = ? "); query.append(StringUtils.isEmpty(val) ? "" : " and (te.acctNickname like '%" + val + "%' or te.acctName like '%" + val + "%')"); return Integer.parseInt(findSession().createQuery(query.toString()).setBoolean(0, AccountDeleteState.NO_DELETE) .uniqueResult().toString()); } }
可以減少重複程式碼的使用,精簡專案的程式碼