2016-05-25 7 views
1
<javaClientGenerator type="XMLMAPPER" targetPackage="com.aaa.${module}.domain.mapper" targetProject="src/main/resources"> 
    <property name="enableSubPackages" value="true" /> 
</javaClientGenerator> 

var $ {module}は、テーブルのconfigのdomainObjectNameの値になります。私はmybaits Mapperを動的に生成できますか?

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

答えて

0

はい、ほとんどの場合、カスタムjavaClientGeneratorを作成する必要があります。私は、enableSubPackagesプロパティがかなりうまくいくとは思わない。

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

をそしてあなた自身のバージョンと、既存のJavaMapperGeneratorをサブクラス化する必要があります:あなたの設定ファイルでは、持っているでしょう。以下のオプション1または2のようなもの。 オプション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; 
    } 

} 

オプション: -

オプション1あなたは、元からオーバー値をコピー全く新しいインターフェース・オブジェクトを作成する必要がありますので、これは困難に何はマッパーのタイプはプライベート変数に格納されています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())); 
     ... 
    } 

} 
関連する問題