需求:
创建一个工具类,查询计算数据库的某项值,并编写一下测试验证计算结果是否正确。
问题1:
用到xxService.getMap(QueryWrapper<T>)和xxMapper.xx()方法,所以要注入service和mapper,
public class BalanceUtil {
private IChargeService iChargeService;
private PriceMapper priceMapper;
//TODO
}
发现,运行本地测试时,报空指针异常,iChargeServise和priceMapper都为null。
百度获得解决方法如下(未po的具体方法是static的):
@Component
public class BalanceUtil {
private static IChargeService iChargeService;
private static PriceMapper priceMapper;
@Autowired
public void setiChargeService(IChargeService iChargeService) {
BalanceUtil.iChargeService = iChargeService;
}
@Resource
public void setPriceMapper(PriceMapper priceMapper) {
BalanceUtil.priceMapper = priceMapper;
}
}
再运行本地测试,测试通