baseService 和baseDao

mapper 和service写大量的重复的代码,今天通过反射和泛型简化代码

public interface BaseMapper {
   List getListByPage(T param);
   int countTotalNum(T param);
}

 



public abstract class BaseService {

    public abstract  BaseMapper getBaseDao();

    public List getListByPage(T param) {
        return getBaseDao().getListByPage(param);
    }
    public int countTotalNum(T param) {
        return getBaseDao().countTotalNum(param);
    }

}

service实现类只需要注入mapper 就可以

public class TCgSettleDfTxnLogService extends BaseService{
@Resource
private TCgSettleDfTxnLogMapper tCgSettleDfTxnLogMapper;

    @Override
    public BaseMapper getBaseDao() {
        return tCgSettleDfTxnLogMapper;
    }
}

现在的mapper文件这样写+mapper.xml 中的sql就可以实现分页

public interface TCgSettleDfTxnLogMapper extends BaseMapper{

}

 

其他通用的方法也可以封装到baseService 和baseDao中

你可能感兴趣的:(springmvc,mybitis,baseDao,baseService,mybitis)