MyBatis-Plus封装抽取多个xxxMapper类作为方法参数
抽取出来的方法:
public void testBaseMapper(String fix, Object reachGoalMapper) {
int count = ((BaseMapper<Object>)reachGoalMapper).selectCount(new QueryWrapper<Object>().eq("reach_id", 1));
ReachGoalTemp reachGoalTemp = new ReachGoalTemp;
if (count < 1) {
Object rg = null;
switch (fix) {
case "2":
reachGoalTemp.setDm("2222222");
rg = new ReachGoal2();
break;
case "3":
reachGoalTemp.setDm("33333");
rg = new ReachGoal3();
break;
case "4":
reachGoalTemp.setDm("444444");
rg = new ReachGoal4();
break;
}
BeanUtils.copyProperties(reachGoalTemp, rg);
((BaseMapper<Object>)reachGoalMapper).insert(rg);
}
}
BeanUtils是:org.springframework.beans.BeanUtils,用来复制属性值
Controller层调用:
@Resource
ReachGoalService reachGoalService;
@Resource
ReachGoal2Mapper reachGoal2Mapper;
@Resource
ReachGoal3Mapper reachGoal3Mapper;
@GetMapping("/test")
public void test() {
reachGoalService.testBaseMapper("2", reachGoal2Mapper);
reachGoalService.testBaseMapper("3", reachGoal3Mapper);
}