多态中父类不能调用子类独有的属性和方法,如何间接使用?
1.BaseService基本接口
public interface BaseService<T> {
//根据条件分页查询
PageInfo<T> findPage(Map<String,String> filters);
//增
boolean insert(T t);
//改
boolean update(T t);
//删
boolean delete(Integer id);
//查
T findById(Integer id);
}
2.BaseServiceImpl实现类基类【父类】
public abstract class BaseServiceImpl<T> {
/**
*父类给子类安排一个必须实现的抽象方法,子类实现的时候把子类的属性带过来
*/
public abstract BaseMapper<T> getBaseMapper();
//分页查询所有
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public PageInfo<T> findPage(Map<String, String> filters) {
PageHelper.startPage(CastUtil.castInt(filters.get("pageNum"),1),CastUtil.castInt(filters.get("pageSize"),10));
List<T> list = getBaseMapper().findPageList(filters);
return new PageInfo<>(list,10);
}
//增
@Transactional
public boolean insert(T admin) {
return getBaseMapper().insert(admin);
}
//改
@Transactional
public boolean update(T admin) {
return getBaseMapper().update(admin);
}
//删
@Transactional
public boolean delete(Integer id) {
return getBaseMapper().delete(id);
}
//查
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public T findById(Integer id) {
return getBaseMapper().findById(id);
}
}
3.具体Service实现类
@Service
public class AdminServiceImpl extends BaseServiceImpl<Admin> implements AdminService {
//子类属性
@Autowired
private AdminMapper adminMapper;
//实现父类安排的抽象方法,把属性传过去
@Override
public BaseMapper getBaseMapper() {
return adminMapper;
}
}
标签:getBaseMapper,return,Service,id,SSM,boolean,filters,基类,public
From: https://www.cnblogs.com/newleesoo/p/17444323.html