首页 > 解决方案 > 如何反射 mybatis 映射器以在 java 8 中将不同的实体保存在一个地方

问题描述

现在我想在 Java 8 中使用 Mybatis 将一些公共字段写入不同的表中,我不想使用反射是这样的:

private void calcSingle(Entity appListRecord,AppListMapper mapper){
                appListMapper.updateByPrimaryKeySelective(envelopeAppList);
    }

但是我已经写了很多这样的重复代码,现在我想这样写一次:

    private void calcSingle(T appListRecord,E mapper){

   // pass different entity and using reflection to invoke the methond updateByPrimaryKeySelective method(each mapper invoke the same method)
        }

然后我可以使用一个函数并传递不同的条目和映射器以避免复制代码。我应该怎么做才能存档?有什么建议吗?

标签: javajava-8mybatis

解决方案


这样做:

Method method = mapper.getClass().getMethod("updateByPrimaryKeySelective", envelopeAppList.getClass());
method.invoke(mapper, envelopeAppList);

推荐阅读