java - 如何反射 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)
}
然后我可以使用一个函数并传递不同的条目和映射器以避免复制代码。我应该怎么做才能存档?有什么建议吗?
解决方案
这样做:
Method method = mapper.getClass().getMethod("updateByPrimaryKeySelective", envelopeAppList.getClass());
method.invoke(mapper, envelopeAppList);
推荐阅读
- javascript - 获取 JavaScript 文件的 URL
- image - 图像网络扑web
- reactjs - 通过 props 传递的 useState 函数不会改变值(在 React-Router 中)
- ubuntu-20.04 - Ubuntu 20.04 - OpenVPN 服务器 - 创建受密码保护的用户配置文件
- python - Python - 创建一个新列,该列从 Pandas 的右边开始不是 NaN 的第一列
- android - 有没有办法以编程方式禁用三星键盘工具栏?
- python - 使用多处理的 Python 进程字典[Python 3.7]
- python - 没有名为 manimlib.stream_starter 的模块错误
- javascript - 从php文件中获取数据并解析成JS文件
- macos - 在 Apple MBP w 上将本机显示器置于睡眠状态。外接显示器 wo。合上盖子