baseService 和baseDao

相德宇
2023-12-01

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

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

 



public abstract class BaseService<T> {

    public abstract  BaseMapper<T> getBaseDao();

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

}

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

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

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

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

public interface TCgSettleDfTxnLogMapper extends BaseMapper<TCgSettleDfTxnLog>{

}

 

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

 类似资料: