Search in sources :

Example 6 with CommentGenerator

use of org.mybatis.generator.api.CommentGenerator in project generator by mybatis.

the class SimpleJavaClientGenerator method getCompilationUnits.

@Override
public List<CompilationUnit> getCompilationUnits() {
    progressCallback.startTask(getString(//$NON-NLS-1$
    "Progress.17", introspectedTable.getFullyQualifiedTable().toString()));
    CommentGenerator commentGenerator = context.getCommentGenerator();
    FullyQualifiedJavaType type = new FullyQualifiedJavaType(introspectedTable.getMyBatis3JavaMapperType());
    Interface interfaze = new Interface(type);
    interfaze.setVisibility(JavaVisibility.PUBLIC);
    commentGenerator.addJavaFileComment(interfaze);
    String rootInterface = introspectedTable.getTableConfigurationProperty(PropertyRegistry.ANY_ROOT_INTERFACE);
    if (!stringHasValue(rootInterface)) {
        rootInterface = context.getJavaClientGeneratorConfiguration().getProperty(PropertyRegistry.ANY_ROOT_INTERFACE);
    }
    if (stringHasValue(rootInterface)) {
        FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(rootInterface);
        interfaze.addSuperInterface(fqjt);
        interfaze.addImportedType(fqjt);
    }
    addDeleteByPrimaryKeyMethod(interfaze);
    addInsertMethod(interfaze);
    addSelectByPrimaryKeyMethod(interfaze);
    addSelectAllMethod(interfaze);
    addUpdateByPrimaryKeyMethod(interfaze);
    List<CompilationUnit> answer = new ArrayList<CompilationUnit>();
    if (context.getPlugins().clientGenerated(interfaze, null, introspectedTable)) {
        answer.add(interfaze);
    }
    List<CompilationUnit> extraCompilationUnits = getExtraCompilationUnits();
    if (extraCompilationUnits != null) {
        answer.addAll(extraCompilationUnits);
    }
    return answer;
}
Also used : CompilationUnit(org.mybatis.generator.api.dom.java.CompilationUnit) CommentGenerator(org.mybatis.generator.api.CommentGenerator) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) ArrayList(java.util.ArrayList) Messages.getString(org.mybatis.generator.internal.util.messages.Messages.getString) Interface(org.mybatis.generator.api.dom.java.Interface)

Example 7 with CommentGenerator

use of org.mybatis.generator.api.CommentGenerator in project generator by mybatis.

the class SqlProviderGenerator method getCompilationUnits.

@Override
public List<CompilationUnit> getCompilationUnits() {
    progressCallback.startTask(getString(//$NON-NLS-1$
    "Progress.18", introspectedTable.getFullyQualifiedTable().toString()));
    CommentGenerator commentGenerator = context.getCommentGenerator();
    FullyQualifiedJavaType type = new FullyQualifiedJavaType(introspectedTable.getMyBatis3SqlProviderType());
    TopLevelClass topLevelClass = new TopLevelClass(type);
    topLevelClass.setVisibility(JavaVisibility.PUBLIC);
    commentGenerator.addJavaFileComment(topLevelClass);
    boolean addApplyWhereMethod = false;
    addApplyWhereMethod |= addCountByExampleMethod(topLevelClass);
    addApplyWhereMethod |= addDeleteByExampleMethod(topLevelClass);
    addInsertSelectiveMethod(topLevelClass);
    addApplyWhereMethod |= addSelectByExampleWithBLOBsMethod(topLevelClass);
    addApplyWhereMethod |= addSelectByExampleWithoutBLOBsMethod(topLevelClass);
    addApplyWhereMethod |= addUpdateByExampleSelectiveMethod(topLevelClass);
    addApplyWhereMethod |= addUpdateByExampleWithBLOBsMethod(topLevelClass);
    addApplyWhereMethod |= addUpdateByExampleWithoutBLOBsMethod(topLevelClass);
    addUpdateByPrimaryKeySelectiveMethod(topLevelClass);
    if (addApplyWhereMethod) {
        addApplyWhereMethod(topLevelClass);
    }
    List<CompilationUnit> answer = new ArrayList<CompilationUnit>();
    if (topLevelClass.getMethods().size() > 0 && context.getPlugins().providerGenerated(topLevelClass, introspectedTable)) {
        answer.add(topLevelClass);
    }
    return answer;
}
Also used : CompilationUnit(org.mybatis.generator.api.dom.java.CompilationUnit) CommentGenerator(org.mybatis.generator.api.CommentGenerator) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) TopLevelClass(org.mybatis.generator.api.dom.java.TopLevelClass) ArrayList(java.util.ArrayList)

Example 8 with CommentGenerator

use of org.mybatis.generator.api.CommentGenerator in project generator by mybatis.

the class ExampleGenerator method getCompilationUnits.

@Override
public List<CompilationUnit> getCompilationUnits() {
    FullyQualifiedTable table = introspectedTable.getFullyQualifiedTable();
    progressCallback.startTask(getString("Progress.6", //$NON-NLS-1$
    table.toString()));
    CommentGenerator commentGenerator = context.getCommentGenerator();
    FullyQualifiedJavaType type = new FullyQualifiedJavaType(introspectedTable.getExampleType());
    TopLevelClass topLevelClass = new TopLevelClass(type);
    topLevelClass.setVisibility(JavaVisibility.PUBLIC);
    commentGenerator.addJavaFileComment(topLevelClass);
    // add default constructor
    Method method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setConstructor(true);
    method.setName(type.getShortName());
    //$NON-NLS-1$
    method.addBodyLine("oredCriteria = new ArrayList<Criteria>();");
    commentGenerator.addGeneralMethodComment(method, introspectedTable);
    topLevelClass.addMethod(method);
    // add field, getter, setter for orderby clause
    Field field = new Field();
    field.setVisibility(JavaVisibility.PROTECTED);
    field.setType(FullyQualifiedJavaType.getStringInstance());
    //$NON-NLS-1$
    field.setName("orderByClause");
    commentGenerator.addFieldComment(field, introspectedTable);
    topLevelClass.addField(field);
    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    //$NON-NLS-1$
    method.setName("setOrderByClause");
    method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
    "orderByClause"));
    //$NON-NLS-1$
    method.addBodyLine("this.orderByClause = orderByClause;");
    commentGenerator.addGeneralMethodComment(method, introspectedTable);
    topLevelClass.addMethod(method);
    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(FullyQualifiedJavaType.getStringInstance());
    //$NON-NLS-1$
    method.setName("getOrderByClause");
    //$NON-NLS-1$
    method.addBodyLine("return orderByClause;");
    commentGenerator.addGeneralMethodComment(method, introspectedTable);
    topLevelClass.addMethod(method);
    // add field, getter, setter for distinct
    field = new Field();
    field.setVisibility(JavaVisibility.PROTECTED);
    field.setType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
    //$NON-NLS-1$
    field.setName("distinct");
    commentGenerator.addFieldComment(field, introspectedTable);
    topLevelClass.addField(field);
    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    //$NON-NLS-1$
    method.setName("setDistinct");
    method.addParameter(new Parameter(FullyQualifiedJavaType.getBooleanPrimitiveInstance(), //$NON-NLS-1$
    "distinct"));
    //$NON-NLS-1$
    method.addBodyLine("this.distinct = distinct;");
    commentGenerator.addGeneralMethodComment(method, introspectedTable);
    topLevelClass.addMethod(method);
    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
    //$NON-NLS-1$
    method.setName("isDistinct");
    //$NON-NLS-1$
    method.addBodyLine("return distinct;");
    commentGenerator.addGeneralMethodComment(method, introspectedTable);
    topLevelClass.addMethod(method);
    // add field and methods for the list of ored criteria
    field = new Field();
    field.setVisibility(JavaVisibility.PROTECTED);
    FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(//$NON-NLS-1$
    "java.util.List<Criteria>");
    field.setType(fqjt);
    //$NON-NLS-1$
    field.setName("oredCriteria");
    commentGenerator.addFieldComment(field, introspectedTable);
    topLevelClass.addField(field);
    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(fqjt);
    //$NON-NLS-1$
    method.setName("getOredCriteria");
    //$NON-NLS-1$
    method.addBodyLine("return oredCriteria;");
    commentGenerator.addGeneralMethodComment(method, introspectedTable);
    topLevelClass.addMethod(method);
    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    //$NON-NLS-1$
    method.setName("or");
    method.addParameter(new Parameter(FullyQualifiedJavaType.getCriteriaInstance(), //$NON-NLS-1$
    "criteria"));
    //$NON-NLS-1$
    method.addBodyLine("oredCriteria.add(criteria);");
    commentGenerator.addGeneralMethodComment(method, introspectedTable);
    topLevelClass.addMethod(method);
    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    //$NON-NLS-1$
    method.setName("or");
    method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance());
    //$NON-NLS-1$
    method.addBodyLine("Criteria criteria = createCriteriaInternal();");
    //$NON-NLS-1$
    method.addBodyLine("oredCriteria.add(criteria);");
    //$NON-NLS-1$
    method.addBodyLine("return criteria;");
    commentGenerator.addGeneralMethodComment(method, introspectedTable);
    topLevelClass.addMethod(method);
    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    //$NON-NLS-1$
    method.setName("createCriteria");
    method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance());
    //$NON-NLS-1$
    method.addBodyLine("Criteria criteria = createCriteriaInternal();");
    //$NON-NLS-1$
    method.addBodyLine("if (oredCriteria.size() == 0) {");
    //$NON-NLS-1$
    method.addBodyLine("oredCriteria.add(criteria);");
    //$NON-NLS-1$
    method.addBodyLine("}");
    //$NON-NLS-1$
    method.addBodyLine("return criteria;");
    commentGenerator.addGeneralMethodComment(method, introspectedTable);
    topLevelClass.addMethod(method);
    method = new Method();
    method.setVisibility(JavaVisibility.PROTECTED);
    //$NON-NLS-1$
    method.setName("createCriteriaInternal");
    method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance());
    //$NON-NLS-1$
    method.addBodyLine("Criteria criteria = new Criteria();");
    //$NON-NLS-1$
    method.addBodyLine("return criteria;");
    commentGenerator.addGeneralMethodComment(method, introspectedTable);
    topLevelClass.addMethod(method);
    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    //$NON-NLS-1$
    method.setName("clear");
    //$NON-NLS-1$
    method.addBodyLine("oredCriteria.clear();");
    //$NON-NLS-1$
    method.addBodyLine("orderByClause = null;");
    //$NON-NLS-1$
    method.addBodyLine("distinct = false;");
    commentGenerator.addGeneralMethodComment(method, introspectedTable);
    topLevelClass.addMethod(method);
    // now generate the inner class that holds the AND conditions
    topLevelClass.addInnerClass(getGeneratedCriteriaInnerClass(topLevelClass));
    topLevelClass.addInnerClass(getCriteriaInnerClass());
    topLevelClass.addInnerClass(getCriterionInnerClass());
    List<CompilationUnit> answer = new ArrayList<CompilationUnit>();
    if (context.getPlugins().modelExampleClassGenerated(topLevelClass, introspectedTable)) {
        answer.add(topLevelClass);
    }
    return answer;
}
Also used : CompilationUnit(org.mybatis.generator.api.dom.java.CompilationUnit) Field(org.mybatis.generator.api.dom.java.Field) CommentGenerator(org.mybatis.generator.api.CommentGenerator) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) FullyQualifiedTable(org.mybatis.generator.api.FullyQualifiedTable) TopLevelClass(org.mybatis.generator.api.dom.java.TopLevelClass) ArrayList(java.util.ArrayList) Parameter(org.mybatis.generator.api.dom.java.Parameter) Method(org.mybatis.generator.api.dom.java.Method)

Example 9 with CommentGenerator

use of org.mybatis.generator.api.CommentGenerator in project generator by mybatis.

the class PrimaryKeyGenerator method getCompilationUnits.

@Override
public List<CompilationUnit> getCompilationUnits() {
    FullyQualifiedTable table = introspectedTable.getFullyQualifiedTable();
    progressCallback.startTask(getString("Progress.7", //$NON-NLS-1$
    table.toString()));
    Plugin plugins = context.getPlugins();
    CommentGenerator commentGenerator = context.getCommentGenerator();
    TopLevelClass topLevelClass = new TopLevelClass(introspectedTable.getPrimaryKeyType());
    topLevelClass.setVisibility(JavaVisibility.PUBLIC);
    commentGenerator.addJavaFileComment(topLevelClass);
    String rootClass = getRootClass();
    if (rootClass != null) {
        topLevelClass.setSuperClass(new FullyQualifiedJavaType(rootClass));
        topLevelClass.addImportedType(topLevelClass.getSuperClass());
    }
    if (introspectedTable.isConstructorBased()) {
        addParameterizedConstructor(topLevelClass);
        if (!introspectedTable.isImmutable()) {
            addDefaultConstructor(topLevelClass);
        }
    }
    commentGenerator.addModelClassComment(topLevelClass, introspectedTable);
    for (IntrospectedColumn introspectedColumn : introspectedTable.getPrimaryKeyColumns()) {
        if (RootClassInfo.getInstance(rootClass, warnings).containsProperty(introspectedColumn)) {
            continue;
        }
        Field field = getJavaBeansField(introspectedColumn, context, introspectedTable);
        if (plugins.modelFieldGenerated(field, topLevelClass, introspectedColumn, introspectedTable, Plugin.ModelClassType.PRIMARY_KEY)) {
            topLevelClass.addField(field);
            topLevelClass.addImportedType(field.getType());
        }
        Method method = getJavaBeansGetter(introspectedColumn, context, introspectedTable);
        if (plugins.modelGetterMethodGenerated(method, topLevelClass, introspectedColumn, introspectedTable, Plugin.ModelClassType.PRIMARY_KEY)) {
            topLevelClass.addMethod(method);
        }
        if (!introspectedTable.isImmutable()) {
            method = getJavaBeansSetter(introspectedColumn, context, introspectedTable);
            if (plugins.modelSetterMethodGenerated(method, topLevelClass, introspectedColumn, introspectedTable, Plugin.ModelClassType.PRIMARY_KEY)) {
                topLevelClass.addMethod(method);
            }
        }
    }
    List<CompilationUnit> answer = new ArrayList<CompilationUnit>();
    if (context.getPlugins().modelPrimaryKeyClassGenerated(topLevelClass, introspectedTable)) {
        answer.add(topLevelClass);
    }
    return answer;
}
Also used : CompilationUnit(org.mybatis.generator.api.dom.java.CompilationUnit) Field(org.mybatis.generator.api.dom.java.Field) JavaBeansUtil.getJavaBeansField(org.mybatis.generator.internal.util.JavaBeansUtil.getJavaBeansField) IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) CommentGenerator(org.mybatis.generator.api.CommentGenerator) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) FullyQualifiedTable(org.mybatis.generator.api.FullyQualifiedTable) TopLevelClass(org.mybatis.generator.api.dom.java.TopLevelClass) ArrayList(java.util.ArrayList) Messages.getString(org.mybatis.generator.internal.util.messages.Messages.getString) Method(org.mybatis.generator.api.dom.java.Method) Plugin(org.mybatis.generator.api.Plugin)

Example 10 with CommentGenerator

use of org.mybatis.generator.api.CommentGenerator in project generator by mybatis.

the class XmlFileMergerTest method testThatFilesAreTheSameAfterMerge.

@Test
public void testThatFilesAreTheSameAfterMerge() throws Exception {
    DefaultXmlFormatter xmlFormatter = new DefaultXmlFormatter();
    Properties p = new Properties();
    p.setProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_DATE, "true");
    CommentGenerator commentGenerator = new DefaultCommentGenerator();
    commentGenerator.addConfigurationProperties(p);
    Document document = new Document(XmlConstants.MYBATIS3_MAPPER_PUBLIC_ID, XmlConstants.MYBATIS3_MAPPER_SYSTEM_ID);
    document.setRootElement(getSqlMapElement(commentGenerator));
    GeneratedXmlFile generatedFile1 = new GeneratedXmlFile(document, "TestMapper.xml", "org.mybatis.test", "src", true, xmlFormatter);
    InputSource is1 = new InputSource(new StringReader(generatedFile1.getFormattedContent()));
    GeneratedXmlFile generatedFile2 = new GeneratedXmlFile(document, "TestMapper.xml", "org.mybatis.test", "src", true, xmlFormatter);
    InputSource is2 = new InputSource(new StringReader(generatedFile2.getFormattedContent()));
    String mergedSource = XmlFileMergerJaxp.getMergedSource(is1, is2, "TestMapper.xml");
    assertEquals(generatedFile1.getFormattedContent(), mergedSource);
}
Also used : DefaultXmlFormatter(org.mybatis.generator.api.dom.DefaultXmlFormatter) InputSource(org.xml.sax.InputSource) CommentGenerator(org.mybatis.generator.api.CommentGenerator) StringReader(java.io.StringReader) Properties(java.util.Properties) Document(org.mybatis.generator.api.dom.xml.Document) GeneratedXmlFile(org.mybatis.generator.api.GeneratedXmlFile) Test(org.junit.Test)

Aggregations

CommentGenerator (org.mybatis.generator.api.CommentGenerator)15 ArrayList (java.util.ArrayList)12 CompilationUnit (org.mybatis.generator.api.dom.java.CompilationUnit)12 FullyQualifiedJavaType (org.mybatis.generator.api.dom.java.FullyQualifiedJavaType)11 TopLevelClass (org.mybatis.generator.api.dom.java.TopLevelClass)11 Field (org.mybatis.generator.api.dom.java.Field)10 Method (org.mybatis.generator.api.dom.java.Method)10 Messages.getString (org.mybatis.generator.internal.util.messages.Messages.getString)10 FullyQualifiedTable (org.mybatis.generator.api.FullyQualifiedTable)9 IntrospectedColumn (org.mybatis.generator.api.IntrospectedColumn)7 Plugin (org.mybatis.generator.api.Plugin)7 JavaBeansUtil.getJavaBeansField (org.mybatis.generator.internal.util.JavaBeansUtil.getJavaBeansField)7 Interface (org.mybatis.generator.api.dom.java.Interface)2 Parameter (org.mybatis.generator.api.dom.java.Parameter)2 StringReader (java.io.StringReader)1 Properties (java.util.Properties)1 Test (org.junit.Test)1 GeneratedXmlFile (org.mybatis.generator.api.GeneratedXmlFile)1 DefaultXmlFormatter (org.mybatis.generator.api.dom.DefaultXmlFormatter)1 Document (org.mybatis.generator.api.dom.xml.Document)1