首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我可以动态生成mybaits Mapper吗?

我可以动态生成mybaits Mapper吗?
EN

Stack Overflow用户
提问于 2016-05-25 12:16:47
回答 1查看 95关注 0票数 1
代码语言:javascript
复制
<javaClientGenerator type="XMLMAPPER" targetPackage="com.aaa.${module}.domain.mapper"  targetProject="src/main/resources">
  <property name="enableSubPackages" value="true" />
</javaClientGenerator>

变量${module}可以是表配置中domainObjectName的值。

代码语言:javascript
复制
    <table schema="test" tableName="account" domainObjectName="Account" >
      <property name="useActualColumnNames" value="true"/>
    </table>
EN

回答 1

Stack Overflow用户

发布于 2017-05-08 18:36:24

可以,但您很可能需要创建自定义javaClientGenerator。我不认为enableSubPackages属性是以这种方式工作的。在您的配置文件中,您将拥有:

代码语言:javascript
复制
<javaClientGenerator type="com.mydomain.MyJavaMapperGenerator" targetPackage="com.aaa.${module}.domain.mapper"  targetProject="src/main/java">
</javaClientGenerator>

然后,您需要使用自己的版本对现有的JavaMapperGenerator进行子类化。类似于下面的选项1或2。考虑到选项1的意想不到的复杂性,我可能会选择选项2。

选项1 -困难的是映射器类型存储在私有变量中,因此您必须创建一个全新的接口对象,从原始对象中复制这些值:

代码语言:javascript
复制
public class MyJavaMapperGenerator extends JavaMapperGenerator {

    @Override
    public List<CompilationUnit> getCompilationUnits() {
        List<CompilationUnit> compliationUnits = super.getCompilationUnits();
        List<CompilationUnit> newCompliationUnits = new ArrayList<>();
        Interface mapper = (Interface)compliationUnits.get(0);
        String mapperType = mapper.getType().getFullyQualifiedName();
        Interface newMapper = new Interface(mapperType.replace("${module}",
            introspectedTable.getFullyQualifiedTable().getDomainObjectName().toLowerCase()));

        newMapper.getJavaDocLines().addAll(mapper.getJavaDocLines());
        newMapper.setVisibility(mapper.getVisibility());
        newMapper.setStatic(mapper.isStatic());
        newMapper.setFinal(mapper.isFinal());
        newMapper.getAnnotations().addAll(mapper.getAnnotations());

        newMapper.addImportedTypes(mapper.getImportedTypes());
        newMapper.getStaticImports().addAll(mapper.getStaticImports());
        newMapper.getSuperInterfaceTypes().addAll(mapper.getSuperInterfaceTypes());
        newMapper.getMethods().addAll(mapper.getMethods());
        newMapper.getFileCommentLines().addAll(mapper.getFileCommentLines());

        newCompliationUnits.add(newMapper);

        return newCompliationUnits;
    }

}

选项2 -虽然有点凌乱,但我可能会从JavaMapperGenerator复制粘贴整个getCompilationsUnits()方法,并修改设置类型的单行:

代码语言:javascript
复制
public class MyJavaMapperGenerator extends JavaMapperGenerator {

    @Override
    public List<CompilationUnit> getCompilationUnits() {
        ...
        //FullyQualifiedJavaType type = new FullyQualifiedJavaType(
        //        introspectedTable.getMyBatis3JavaMapperType());
        FullyQualifiedJavaType type = new FullyQualifiedJavaType(
                introspectedTable.getMyBatis3JavaMapperType().replace("${module}",
                introspectedTable.getFullyQualifiedTable().getDomainObjectName()
                                 .toLowerCase()));
        ...
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37427739

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档