use of org.mybatis.generator.api.dom.java.Method in project generator by mybatis.
the class ExampleGenerator method getCriteriaInnerClass.
private InnerClass getCriteriaInnerClass() {
Method method;
InnerClass answer = new InnerClass(FullyQualifiedJavaType.getCriteriaInstance());
answer.setVisibility(JavaVisibility.PUBLIC);
answer.setStatic(true);
answer.setSuperClass(FullyQualifiedJavaType.getGeneratedCriteriaInstance());
context.getCommentGenerator().addClassComment(answer, introspectedTable, true);
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
//$NON-NLS-1$
method.setName("Criteria");
method.setConstructor(true);
//$NON-NLS-1$
method.addBodyLine("super();");
answer.addMethod(method);
return answer;
}
use of org.mybatis.generator.api.dom.java.Method in project generator by mybatis.
the class ExampleGenerator method getGeneratedCriteriaInnerClass.
private InnerClass getGeneratedCriteriaInnerClass(TopLevelClass topLevelClass) {
Field field;
Method method;
InnerClass answer = new InnerClass(FullyQualifiedJavaType.getGeneratedCriteriaInstance());
answer.setVisibility(JavaVisibility.PROTECTED);
answer.setStatic(true);
answer.setAbstract(true);
context.getCommentGenerator().addClassComment(answer, introspectedTable);
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
//$NON-NLS-1$
method.setName("GeneratedCriteria");
method.setConstructor(true);
//$NON-NLS-1$
method.addBodyLine("super();");
//$NON-NLS-1$
method.addBodyLine("criteria = new ArrayList<Criterion>();");
answer.addMethod(method);
List<String> criteriaLists = new ArrayList<String>();
//$NON-NLS-1$
criteriaLists.add("criteria");
for (IntrospectedColumn introspectedColumn : introspectedTable.getNonBLOBColumns()) {
if (stringHasValue(introspectedColumn.getTypeHandler())) {
String name = addtypeHandledObjectsAndMethods(introspectedColumn, method, answer);
criteriaLists.add(name);
}
}
// now generate the isValid method
method = new Method();
method.setVisibility(JavaVisibility.PUBLIC);
//$NON-NLS-1$
method.setName("isValid");
method.setReturnType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
StringBuilder sb = new StringBuilder();
Iterator<String> strIter = criteriaLists.iterator();
//$NON-NLS-1$
sb.append("return ");
sb.append(strIter.next());
//$NON-NLS-1$
sb.append(".size() > 0");
if (!strIter.hasNext()) {
sb.append(';');
}
method.addBodyLine(sb.toString());
while (strIter.hasNext()) {
sb.setLength(0);
OutputUtilities.javaIndent(sb, 1);
//$NON-NLS-1$
sb.append("|| ");
sb.append(strIter.next());
//$NON-NLS-1$
sb.append(".size() > 0");
if (!strIter.hasNext()) {
sb.append(';');
}
method.addBodyLine(sb.toString());
}
answer.addMethod(method);
// now generate the getAllCriteria method
if (criteriaLists.size() > 1) {
field = new Field();
//$NON-NLS-1$
field.setName("allCriteria");
//$NON-NLS-1$
field.setType(new FullyQualifiedJavaType("List<Criterion>"));
field.setVisibility(JavaVisibility.PROTECTED);
answer.addField(field);
}
method = new Method();
method.setVisibility(JavaVisibility.PUBLIC);
//$NON-NLS-1$
method.setName("getAllCriteria");
//$NON-NLS-1$
method.setReturnType(new FullyQualifiedJavaType("List<Criterion>"));
if (criteriaLists.size() < 2) {
//$NON-NLS-1$
method.addBodyLine("return criteria;");
} else {
//$NON-NLS-1$
method.addBodyLine("if (allCriteria == null) {");
//$NON-NLS-1$
method.addBodyLine("allCriteria = new ArrayList<Criterion>();");
strIter = criteriaLists.iterator();
while (strIter.hasNext()) {
//$NON-NLS-1$
method.addBodyLine(String.format("allCriteria.addAll(%s);", strIter.next()));
}
//$NON-NLS-1$
method.addBodyLine("}");
//$NON-NLS-1$
method.addBodyLine("return allCriteria;");
}
answer.addMethod(method);
// now we need to generate the methods that will be used in the SqlMap
// to generate the dynamic where clause
topLevelClass.addImportedType(FullyQualifiedJavaType.getNewListInstance());
topLevelClass.addImportedType(FullyQualifiedJavaType.getNewArrayListInstance());
field = new Field();
field.setVisibility(JavaVisibility.PROTECTED);
FullyQualifiedJavaType listOfCriterion = new FullyQualifiedJavaType(//$NON-NLS-1$
"java.util.List<Criterion>");
field.setType(listOfCriterion);
//$NON-NLS-1$
field.setName("criteria");
answer.addField(field);
method = new Method();
method.setVisibility(JavaVisibility.PUBLIC);
method.setReturnType(field.getType());
method.setName(getGetterMethodName(field.getName(), field.getType()));
//$NON-NLS-1$
method.addBodyLine("return criteria;");
answer.addMethod(method);
// now add the methods for simplifying the individual field set methods
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
//$NON-NLS-1$
method.setName("addCriterion");
method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
"condition"));
//$NON-NLS-1$
method.addBodyLine("if (condition == null) {");
method.addBodyLine(//$NON-NLS-1$
"throw new RuntimeException(\"Value for condition cannot be null\");");
//$NON-NLS-1$
method.addBodyLine("}");
//$NON-NLS-1$
method.addBodyLine("criteria.add(new Criterion(condition));");
if (criteriaLists.size() > 1) {
//$NON-NLS-1$
method.addBodyLine("allCriteria = null;");
}
answer.addMethod(method);
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
//$NON-NLS-1$
method.setName("addCriterion");
method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
"condition"));
method.addParameter(new Parameter(FullyQualifiedJavaType.getObjectInstance(), //$NON-NLS-1$
"value"));
method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
"property"));
//$NON-NLS-1$
method.addBodyLine("if (value == null) {");
method.addBodyLine(//$NON-NLS-1$
"throw new RuntimeException(\"Value for \" + property + \" cannot be null\");");
//$NON-NLS-1$
method.addBodyLine("}");
//$NON-NLS-1$
method.addBodyLine("criteria.add(new Criterion(condition, value));");
if (criteriaLists.size() > 1) {
//$NON-NLS-1$
method.addBodyLine("allCriteria = null;");
}
answer.addMethod(method);
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
//$NON-NLS-1$
method.setName("addCriterion");
method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
"condition"));
method.addParameter(new Parameter(FullyQualifiedJavaType.getObjectInstance(), //$NON-NLS-1$
"value1"));
method.addParameter(new Parameter(FullyQualifiedJavaType.getObjectInstance(), //$NON-NLS-1$
"value2"));
method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
"property"));
//$NON-NLS-1$
method.addBodyLine("if (value1 == null || value2 == null) {");
method.addBodyLine(//$NON-NLS-1$
"throw new RuntimeException(\"Between values for \" + property + \" cannot be null\");");
//$NON-NLS-1$
method.addBodyLine("}");
method.addBodyLine(//$NON-NLS-1$
"criteria.add(new Criterion(condition, value1, value2));");
if (criteriaLists.size() > 1) {
//$NON-NLS-1$
method.addBodyLine("allCriteria = null;");
}
answer.addMethod(method);
FullyQualifiedJavaType listOfDates = new FullyQualifiedJavaType(//$NON-NLS-1$
"java.util.List<java.util.Date>");
if (introspectedTable.hasJDBCDateColumns()) {
topLevelClass.addImportedType(FullyQualifiedJavaType.getDateInstance());
topLevelClass.addImportedType(FullyQualifiedJavaType.getNewIteratorInstance());
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
//$NON-NLS-1$
method.setName("addCriterionForJDBCDate");
method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
"condition"));
method.addParameter(new Parameter(FullyQualifiedJavaType.getDateInstance(), //$NON-NLS-1$
"value"));
method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
"property"));
//$NON-NLS-1$
method.addBodyLine("if (value == null) {");
method.addBodyLine(//$NON-NLS-1$
"throw new RuntimeException(\"Value for \" + property + \" cannot be null\");");
//$NON-NLS-1$
method.addBodyLine("}");
method.addBodyLine(//$NON-NLS-1$
"addCriterion(condition, new java.sql.Date(value.getTime()), property);");
answer.addMethod(method);
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
//$NON-NLS-1$
method.setName("addCriterionForJDBCDate");
method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
"condition"));
//$NON-NLS-1$
method.addParameter(new Parameter(listOfDates, "values"));
method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
"property"));
//$NON-NLS-1$
method.addBodyLine("if (values == null || values.size() == 0) {");
method.addBodyLine(//$NON-NLS-1$
"throw new RuntimeException(\"Value list for \" + property + \" cannot be null or empty\");");
//$NON-NLS-1$
method.addBodyLine("}");
method.addBodyLine(//$NON-NLS-1$
"List<java.sql.Date> dateList = new ArrayList<java.sql.Date>();");
//$NON-NLS-1$
method.addBodyLine("Iterator<Date> iter = values.iterator();");
//$NON-NLS-1$
method.addBodyLine("while (iter.hasNext()) {");
method.addBodyLine(//$NON-NLS-1$
"dateList.add(new java.sql.Date(iter.next().getTime()));");
//$NON-NLS-1$
method.addBodyLine("}");
//$NON-NLS-1$
method.addBodyLine("addCriterion(condition, dateList, property);");
answer.addMethod(method);
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
//$NON-NLS-1$
method.setName("addCriterionForJDBCDate");
method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
"condition"));
method.addParameter(new Parameter(FullyQualifiedJavaType.getDateInstance(), //$NON-NLS-1$
"value1"));
method.addParameter(new Parameter(FullyQualifiedJavaType.getDateInstance(), //$NON-NLS-1$
"value2"));
method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
"property"));
//$NON-NLS-1$
method.addBodyLine("if (value1 == null || value2 == null) {");
method.addBodyLine(//$NON-NLS-1$
"throw new RuntimeException(\"Between values for \" + property + \" cannot be null\");");
//$NON-NLS-1$
method.addBodyLine("}");
method.addBodyLine(//$NON-NLS-1$
"addCriterion(condition, new java.sql.Date(value1.getTime()), new java.sql.Date(value2.getTime()), property);");
answer.addMethod(method);
}
if (introspectedTable.hasJDBCTimeColumns()) {
topLevelClass.addImportedType(FullyQualifiedJavaType.getDateInstance());
topLevelClass.addImportedType(FullyQualifiedJavaType.getNewIteratorInstance());
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
//$NON-NLS-1$
method.setName("addCriterionForJDBCTime");
method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
"condition"));
method.addParameter(new Parameter(FullyQualifiedJavaType.getDateInstance(), //$NON-NLS-1$
"value"));
method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
"property"));
//$NON-NLS-1$
method.addBodyLine("if (value == null) {");
method.addBodyLine(//$NON-NLS-1$
"throw new RuntimeException(\"Value for \" + property + \" cannot be null\");");
//$NON-NLS-1$
method.addBodyLine("}");
method.addBodyLine(//$NON-NLS-1$
"addCriterion(condition, new java.sql.Time(value.getTime()), property);");
answer.addMethod(method);
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
//$NON-NLS-1$
method.setName("addCriterionForJDBCTime");
method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
"condition"));
//$NON-NLS-1$
method.addParameter(new Parameter(listOfDates, "values"));
method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
"property"));
//$NON-NLS-1$
method.addBodyLine("if (values == null || values.size() == 0) {");
method.addBodyLine(//$NON-NLS-1$
"throw new RuntimeException(\"Value list for \" + property + \" cannot be null or empty\");");
//$NON-NLS-1$
method.addBodyLine("}");
method.addBodyLine(//$NON-NLS-1$
"List<java.sql.Time> timeList = new ArrayList<java.sql.Time>();");
//$NON-NLS-1$
method.addBodyLine("Iterator<Date> iter = values.iterator();");
//$NON-NLS-1$
method.addBodyLine("while (iter.hasNext()) {");
method.addBodyLine(//$NON-NLS-1$
"timeList.add(new java.sql.Time(iter.next().getTime()));");
//$NON-NLS-1$
method.addBodyLine("}");
//$NON-NLS-1$
method.addBodyLine("addCriterion(condition, timeList, property);");
answer.addMethod(method);
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
//$NON-NLS-1$
method.setName("addCriterionForJDBCTime");
method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
"condition"));
method.addParameter(new Parameter(FullyQualifiedJavaType.getDateInstance(), //$NON-NLS-1$
"value1"));
method.addParameter(new Parameter(FullyQualifiedJavaType.getDateInstance(), //$NON-NLS-1$
"value2"));
method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
"property"));
//$NON-NLS-1$
method.addBodyLine("if (value1 == null || value2 == null) {");
method.addBodyLine(//$NON-NLS-1$
"throw new RuntimeException(\"Between values for \" + property + \" cannot be null\");");
//$NON-NLS-1$
method.addBodyLine("}");
method.addBodyLine(//$NON-NLS-1$
"addCriterion(condition, new java.sql.Time(value1.getTime()), new java.sql.Time(value2.getTime()), property);");
answer.addMethod(method);
}
for (IntrospectedColumn introspectedColumn : introspectedTable.getNonBLOBColumns()) {
topLevelClass.addImportedType(introspectedColumn.getFullyQualifiedJavaType());
// here we need to add the individual methods for setting the
// conditions for a field
answer.addMethod(getSetNullMethod(introspectedColumn));
answer.addMethod(getSetNotNullMethod(introspectedColumn));
answer.addMethod(getSetEqualMethod(introspectedColumn));
answer.addMethod(getSetNotEqualMethod(introspectedColumn));
answer.addMethod(getSetGreaterThanMethod(introspectedColumn));
answer.addMethod(getSetGreaterThenOrEqualMethod(introspectedColumn));
answer.addMethod(getSetLessThanMethod(introspectedColumn));
answer.addMethod(getSetLessThanOrEqualMethod(introspectedColumn));
if (introspectedColumn.isJdbcCharacterColumn()) {
answer.addMethod(getSetLikeMethod(introspectedColumn));
answer.addMethod(getSetNotLikeMethod(introspectedColumn));
}
answer.addMethod(getSetInOrNotInMethod(introspectedColumn, true));
answer.addMethod(getSetInOrNotInMethod(introspectedColumn, false));
answer.addMethod(getSetBetweenOrNotBetweenMethod(introspectedColumn, true));
answer.addMethod(getSetBetweenOrNotBetweenMethod(introspectedColumn, false));
}
return answer;
}
use of org.mybatis.generator.api.dom.java.Method in project generator by mybatis.
the class ExampleGenerator method getSingleValueMethod.
private Method getSingleValueMethod(IntrospectedColumn introspectedColumn, String nameFragment, String operator) {
Method method = new Method();
method.setVisibility(JavaVisibility.PUBLIC);
method.addParameter(new Parameter(introspectedColumn.getFullyQualifiedJavaType(), //$NON-NLS-1$
"value"));
StringBuilder sb = new StringBuilder();
sb.append(introspectedColumn.getJavaProperty());
sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
//$NON-NLS-1$
sb.insert(0, "and");
sb.append(nameFragment);
method.setName(sb.toString());
method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance());
sb.setLength(0);
if (introspectedColumn.isJDBCDateColumn()) {
//$NON-NLS-1$
sb.append("addCriterionForJDBCDate(\"");
} else if (introspectedColumn.isJDBCTimeColumn()) {
//$NON-NLS-1$
sb.append("addCriterionForJDBCTime(\"");
} else if (stringHasValue(introspectedColumn.getTypeHandler())) {
//$NON-NLS-1$
sb.append("add");
sb.append(introspectedColumn.getJavaProperty());
sb.setCharAt(3, Character.toUpperCase(sb.charAt(3)));
//$NON-NLS-1$
sb.append("Criterion(\"");
} else {
//$NON-NLS-1$
sb.append("addCriterion(\"");
}
sb.append(MyBatis3FormattingUtilities.getAliasedActualColumnName(introspectedColumn));
sb.append(' ');
sb.append(operator);
//$NON-NLS-1$
sb.append("\", ");
//$NON-NLS-1$
sb.append("value");
//$NON-NLS-1$
sb.append(", \"");
sb.append(introspectedColumn.getJavaProperty());
//$NON-NLS-1$
sb.append("\");");
method.addBodyLine(sb.toString());
//$NON-NLS-1$
method.addBodyLine("return (Criteria) this;");
return method;
}
use of org.mybatis.generator.api.dom.java.Method 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;
}
use of org.mybatis.generator.api.dom.java.Method in project generator by mybatis.
the class ExampleGenerator method addtypeHandledObjectsAndMethods.
/**
* This method adds all the extra methods and fields required to support a
* user defined type handler on some column.
*
* @param introspectedColumn
* @param constructor
* @param innerClass
* @return the name of the List added to the class by this method
*/
private String addtypeHandledObjectsAndMethods(IntrospectedColumn introspectedColumn, Method constructor, InnerClass innerClass) {
String answer;
StringBuilder sb = new StringBuilder();
// add new private field and public accessor in the class
sb.setLength(0);
sb.append(introspectedColumn.getJavaProperty());
//$NON-NLS-1$
sb.append("Criteria");
answer = sb.toString();
Field field = new Field();
field.setVisibility(JavaVisibility.PROTECTED);
//$NON-NLS-1$
field.setType(new FullyQualifiedJavaType("java.util.List<Criterion>"));
field.setName(answer);
innerClass.addField(field);
Method method = new Method();
method.setVisibility(JavaVisibility.PUBLIC);
method.setReturnType(field.getType());
method.setName(getGetterMethodName(field.getName(), field.getType()));
//$NON-NLS-1$
sb.insert(0, "return ");
sb.append(';');
method.addBodyLine(sb.toString());
innerClass.addMethod(method);
// add constructor initialization
sb.setLength(0);
sb.append(field.getName());
//$NON-NLS-1$;
sb.append(" = new ArrayList<Criterion>();");
constructor.addBodyLine(sb.toString());
// now add the methods for simplifying the individual field set methods
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
sb.setLength(0);
//$NON-NLS-1$
sb.append("add");
sb.append(introspectedColumn.getJavaProperty());
sb.setCharAt(3, Character.toUpperCase(sb.charAt(3)));
//$NON-NLS-1$
sb.append("Criterion");
method.setName(sb.toString());
method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
"condition"));
method.addParameter(new Parameter(FullyQualifiedJavaType.getObjectInstance(), //$NON-NLS-1$
"value"));
method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
"property"));
//$NON-NLS-1$
method.addBodyLine("if (value == null) {");
method.addBodyLine(//$NON-NLS-1$
"throw new RuntimeException(\"Value for \" + property + \" cannot be null\");");
//$NON-NLS-1$
method.addBodyLine("}");
method.addBodyLine(//$NON-NLS-1$
String.format(//$NON-NLS-1$
"%s.add(new Criterion(condition, value, \"%s\"));", field.getName(), introspectedColumn.getTypeHandler()));
//$NON-NLS-1$
method.addBodyLine("allCriteria = null;");
innerClass.addMethod(method);
sb.setLength(0);
//$NON-NLS-1$
sb.append("add");
sb.append(introspectedColumn.getJavaProperty());
sb.setCharAt(3, Character.toUpperCase(sb.charAt(3)));
//$NON-NLS-1$
sb.append("Criterion");
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
method.setName(sb.toString());
method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
"condition"));
method.addParameter(new Parameter(introspectedColumn.getFullyQualifiedJavaType(), //$NON-NLS-1$
"value1"));
method.addParameter(new Parameter(introspectedColumn.getFullyQualifiedJavaType(), //$NON-NLS-1$
"value2"));
method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
"property"));
if (!introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
//$NON-NLS-1$
method.addBodyLine("if (value1 == null || value2 == null) {");
method.addBodyLine(//$NON-NLS-1$
"throw new RuntimeException(\"Between values for \" + property + \" cannot be null\");");
//$NON-NLS-1$
method.addBodyLine("}");
}
method.addBodyLine(//$NON-NLS-1$
String.format(//$NON-NLS-1$
"%s.add(new Criterion(condition, value1, value2, \"%s\"));", field.getName(), introspectedColumn.getTypeHandler()));
//$NON-NLS-1$
method.addBodyLine("allCriteria = null;");
innerClass.addMethod(method);
return answer;
}
Aggregations