use of org.mybatis.generator.api.dom.java.Parameter 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 a list of the names of all Lists added to the class by this
* method
*/
private List<String> addtypeHandledObjectsAndMethods(IntrospectedColumn introspectedColumn, Method constructor, InnerClass innerClass) {
List<String> answer = new ArrayList<String>();
StringBuilder sb = new StringBuilder();
// add new private fields and public accessors in the class
FullyQualifiedJavaType listOfMaps;
if (generateForJava5) {
listOfMaps = new FullyQualifiedJavaType(//$NON-NLS-1$
"java.util.List<java.util.Map<java.lang.String, java.lang.Object>>");
} else {
//$NON-NLS-1$
listOfMaps = new FullyQualifiedJavaType("java.util.List");
}
sb.setLength(0);
sb.append(introspectedColumn.getJavaProperty());
//$NON-NLS-1$
sb.append("CriteriaWithSingleValue");
answer.add(sb.toString());
Field field = new Field();
field.setVisibility(JavaVisibility.PROTECTED);
field.setType(listOfMaps);
field.setName(sb.toString());
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);
sb.setLength(0);
sb.append(introspectedColumn.getJavaProperty());
//$NON-NLS-1$
sb.append("CriteriaWithListValue");
answer.add(sb.toString());
field = new Field();
field.setVisibility(JavaVisibility.PROTECTED);
field.setType(listOfMaps);
field.setName(sb.toString());
innerClass.addField(field);
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);
sb.setLength(0);
sb.append(introspectedColumn.getJavaProperty());
//$NON-NLS-1$
sb.append("CriteriaWithBetweenValue");
answer.add(sb.toString());
field = new Field();
field.setVisibility(JavaVisibility.PROTECTED);
field.setType(listOfMaps);
field.setName(sb.toString());
innerClass.addField(field);
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(introspectedColumn.getJavaProperty());
if (generateForJava5) {
sb.append(//$NON-NLS-1$;
"CriteriaWithSingleValue = new ArrayList<Map<String, Object>>();");
} else {
//$NON-NLS-1$;
sb.append("CriteriaWithSingleValue = new ArrayList();");
}
constructor.addBodyLine(sb.toString());
sb.setLength(0);
sb.append(introspectedColumn.getJavaProperty());
if (generateForJava5) {
sb.append(//$NON-NLS-1$
"CriteriaWithListValue = new ArrayList<Map<String, Object>>();");
} else {
//$NON-NLS-1$
sb.append("CriteriaWithListValue = new ArrayList();");
}
constructor.addBodyLine(sb.toString());
sb.setLength(0);
sb.append(introspectedColumn.getJavaProperty());
if (generateForJava5) {
sb.append(//$NON-NLS-1$
"CriteriaWithBetweenValue = new ArrayList<Map<String, Object>>();");
} else {
//$NON-NLS-1$
sb.append("CriteriaWithBetweenValue = new ArrayList();");
}
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"));
if (introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
method.addParameter(new Parameter(introspectedColumn.getFullyQualifiedJavaType().getPrimitiveTypeWrapper(), //$NON-NLS-1$
"value"));
} else {
method.addParameter(new Parameter(introspectedColumn.getFullyQualifiedJavaType(), //$NON-NLS-1$
"value"));
}
method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
"property"));
if (!introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
//$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("}");
}
if (generateForJava5) {
method.addBodyLine(//$NON-NLS-1$
"Map<String, Object> map = new HashMap<String, Object>();");
} else {
//$NON-NLS-1$
method.addBodyLine("Map map = new HashMap();");
}
//$NON-NLS-1$
method.addBodyLine("map.put(\"condition\", condition);");
//$NON-NLS-1$
method.addBodyLine("map.put(\"value\", value);");
sb.setLength(0);
sb.append(introspectedColumn.getJavaProperty());
//$NON-NLS-1$
sb.append("CriteriaWithSingleValue.add(map);");
method.addBodyLine(sb.toString());
innerClass.addMethod(method);
FullyQualifiedJavaType listOfObjects = FullyQualifiedJavaType.getNewListInstance();
if (generateForJava5) {
if (introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
listOfObjects.addTypeArgument(introspectedColumn.getFullyQualifiedJavaType().getPrimitiveTypeWrapper());
} else {
listOfObjects.addTypeArgument(introspectedColumn.getFullyQualifiedJavaType());
}
}
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"));
//$NON-NLS-1$
method.addParameter(new Parameter(listOfObjects, "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("}");
if (generateForJava5) {
method.addBodyLine(//$NON-NLS-1$
"Map<String, Object> map = new HashMap<String, Object>();");
} else {
//$NON-NLS-1$
method.addBodyLine("Map map = new HashMap();");
}
//$NON-NLS-1$
method.addBodyLine("map.put(\"condition\", condition);");
//$NON-NLS-1$
method.addBodyLine("map.put(\"values\", values);");
sb.setLength(0);
sb.append(introspectedColumn.getJavaProperty());
//$NON-NLS-1$
sb.append("CriteriaWithListValue.add(map);");
method.addBodyLine(sb.toString());
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"));
if (introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
method.addParameter(new Parameter(introspectedColumn.getFullyQualifiedJavaType().getPrimitiveTypeWrapper(), //$NON-NLS-1$
"value1"));
method.addParameter(new Parameter(introspectedColumn.getFullyQualifiedJavaType().getPrimitiveTypeWrapper(), //$NON-NLS-1$
"value2"));
} else {
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"));
//$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("}");
if (generateForJava5) {
String shortName;
if (introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
shortName = introspectedColumn.getFullyQualifiedJavaType().getPrimitiveTypeWrapper().getShortName();
} else {
shortName = introspectedColumn.getFullyQualifiedJavaType().getShortName();
}
sb.setLength(0);
//$NON-NLS-1$
sb.append("List<");
sb.append(shortName);
//$NON-NLS-1$
sb.append("> list = new ArrayList<");
sb.append(shortName);
//$NON-NLS-1$
sb.append(">();");
method.addBodyLine(sb.toString());
} else {
//$NON-NLS-1$
method.addBodyLine("List list = new ArrayList();");
}
//$NON-NLS-1$
method.addBodyLine("list.add(value1);");
//$NON-NLS-1$
method.addBodyLine("list.add(value2);");
if (generateForJava5) {
method.addBodyLine(//$NON-NLS-1$
"Map<String, Object> map = new HashMap<String, Object>();");
} else {
//$NON-NLS-1$
method.addBodyLine("Map map = new HashMap();");
}
//$NON-NLS-1$
method.addBodyLine("map.put(\"condition\", condition);");
//$NON-NLS-1$
method.addBodyLine("map.put(\"values\", list);");
sb.setLength(0);
sb.append(introspectedColumn.getJavaProperty());
//$NON-NLS-1$
sb.append("CriteriaWithBetweenValue.add(map);");
method.addBodyLine(sb.toString());
innerClass.addMethod(method);
return answer;
}
use of org.mybatis.generator.api.dom.java.Parameter in project generator by mybatis.
the class BaseRecordGenerator method addParameterizedConstructor.
private void addParameterizedConstructor(TopLevelClass topLevelClass) {
Method method = new Method();
method.setVisibility(JavaVisibility.PUBLIC);
method.setConstructor(true);
method.setName(topLevelClass.getType().getShortName());
context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);
List<IntrospectedColumn> constructorColumns = includeBLOBColumns() ? introspectedTable.getAllColumns() : introspectedTable.getNonBLOBColumns();
for (IntrospectedColumn introspectedColumn : constructorColumns) {
method.addParameter(new Parameter(introspectedColumn.getFullyQualifiedJavaType(), introspectedColumn.getJavaProperty()));
topLevelClass.addImportedType(introspectedColumn.getFullyQualifiedJavaType());
}
StringBuilder sb = new StringBuilder();
if (introspectedTable.getRules().generatePrimaryKeyClass()) {
boolean comma = false;
//$NON-NLS-1$
sb.append("super(");
for (IntrospectedColumn introspectedColumn : introspectedTable.getPrimaryKeyColumns()) {
if (comma) {
//$NON-NLS-1$
sb.append(", ");
} else {
comma = true;
}
sb.append(introspectedColumn.getJavaProperty());
}
//$NON-NLS-1$
sb.append(");");
method.addBodyLine(sb.toString());
}
List<IntrospectedColumn> introspectedColumns = getColumnsInThisClass();
for (IntrospectedColumn introspectedColumn : introspectedColumns) {
sb.setLength(0);
//$NON-NLS-1$
sb.append("this.");
sb.append(introspectedColumn.getJavaProperty());
//$NON-NLS-1$
sb.append(" = ");
sb.append(introspectedColumn.getJavaProperty());
sb.append(';');
method.addBodyLine(sb.toString());
}
topLevelClass.addMethod(method);
}
use of org.mybatis.generator.api.dom.java.Parameter 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.Parameter 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.Parameter 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;
}
Aggregations