抽象类和接口在项目中使用的实例

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jiangwudidebaba/article/details/82953089

抽象类和接口在项目中经常使用如果使用恰当,可以大大精简代码

抽象类:指的是用abstract关键字修饰或者类中有抽象方法,那么这个类就是抽象类
特点:

  1. 抽象类用关键字 abstract修饰
  2. 抽象类的抽象方法没有方法体,在子类继承父类中有抽象方法时,必须实现抽像方法
  3. 抽象类只能被单继承

接口:接口只有方法体没有具体的实现,用inferce修饰
特点:

  1. 接口中没有自己的属性,只能有方法体
  2. 类在实现接口时,可以实现多个接口

代码演示:

使用抽象方法将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());
	}
}

可以减少重复代码的使用,精简项目的代码

猜你喜欢

转载自blog.csdn.net/jiangwudidebaba/article/details/82953089
今日推荐