【问题标题】:could I generate mybaits Mapper dynamically?我可以动态生成 mybaits Mapper 吗?
【发布时间】:2016-05-25 04:16:46
【问题描述】:
<javaClientGenerator type="XMLMAPPER" targetPackage="com.aaa.${module}.domain.mapper"  targetProject="src/main/resources">
  <property name="enableSubPackages" value="true" />
</javaClientGenerator>

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

    <table schema="test" tableName="account" domainObjectName="Account" >
      <property name="useActualColumnNames" value="true"/>
    </table>

【问题讨论】:

    标签: code-generation mybatis mybatis-generator


    【解决方案1】:

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

    <javaClientGenerator type="com.mydomain.MyJavaMapperGenerator" targetPackage="com.aaa.${module}.domain.mapper"  targetProject="src/main/java">
    </javaClientGenerator>
    

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

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

    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() 方法并修改设置类型的单行:

    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()));
            ...
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多