use of org.mybatis.generator.api.dom.java.FullyQualifiedJavaType in project generator by mybatis.
the class ExampleGenerator method getSetBetweenOrNotBetweenMethod.
/**
* Generates methods that set between and not between conditions
*
* @param introspectedColumn
* @param betweenMethod
* @return a generated method for the between or not between method
*/
private Method getSetBetweenOrNotBetweenMethod(IntrospectedColumn introspectedColumn, boolean betweenMethod) {
Method method = new Method();
method.setVisibility(JavaVisibility.PUBLIC);
FullyQualifiedJavaType type = introspectedColumn.getFullyQualifiedJavaType();
//$NON-NLS-1$
method.addParameter(new Parameter(type, "value1"));
//$NON-NLS-1$
method.addParameter(new Parameter(type, "value2"));
StringBuilder sb = new StringBuilder();
sb.append(introspectedColumn.getJavaProperty());
sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
//$NON-NLS-1$
sb.insert(0, "and");
if (betweenMethod) {
//$NON-NLS-1$
sb.append("Between");
} else {
//$NON-NLS-1$
sb.append("NotBetween");
}
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(Ibatis2FormattingUtilities.getAliasedActualColumnName(introspectedColumn));
if (betweenMethod) {
//$NON-NLS-1$
sb.append(" between");
} else {
//$NON-NLS-1$
sb.append(" not between");
}
//$NON-NLS-1$
sb.append("\", ");
if (introspectedColumn.getFullyQualifiedJavaType().isPrimitive() && !introspectedTable.isJava5Targeted()) {
//$NON-NLS-1$
sb.append("new ");
sb.append(introspectedColumn.getFullyQualifiedJavaType().getPrimitiveTypeWrapper().getShortName());
//$NON-NLS-1$
sb.append("(value1), ");
//$NON-NLS-1$
sb.append("new ");
sb.append(introspectedColumn.getFullyQualifiedJavaType().getPrimitiveTypeWrapper().getShortName());
//$NON-NLS-1$
sb.append("(value2)");
} else {
//$NON-NLS-1$
sb.append("value1, value2");
}
//$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.FullyQualifiedJavaType in project generator by mybatis.
the class ExampleGenerator method getSetInOrNotInMethod.
/**
*
* @param introspectedColumn
* @param inMethod
* if true generates an "in" method, else generates a "not in"
* method
* @return a generated method for the in or not in method
*/
private Method getSetInOrNotInMethod(IntrospectedColumn introspectedColumn, boolean inMethod) {
Method method = new Method();
method.setVisibility(JavaVisibility.PUBLIC);
FullyQualifiedJavaType type = FullyQualifiedJavaType.getNewListInstance();
if (generateForJava5) {
if (introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
type.addTypeArgument(introspectedColumn.getFullyQualifiedJavaType().getPrimitiveTypeWrapper());
} else {
type.addTypeArgument(introspectedColumn.getFullyQualifiedJavaType());
}
}
//$NON-NLS-1$
method.addParameter(new Parameter(type, "values"));
StringBuilder sb = new StringBuilder();
sb.append(introspectedColumn.getJavaProperty());
sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
//$NON-NLS-1$
sb.insert(0, "and");
if (inMethod) {
//$NON-NLS-1$
sb.append("In");
} else {
//$NON-NLS-1$
sb.append("NotIn");
}
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(Ibatis2FormattingUtilities.getAliasedActualColumnName(introspectedColumn));
if (inMethod) {
//$NON-NLS-1$
sb.append(" in");
} else {
//$NON-NLS-1$
sb.append(" not in");
}
//$NON-NLS-1$
sb.append("\", values, \"");
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.FullyQualifiedJavaType 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.FullyQualifiedJavaType 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;
}
use of org.mybatis.generator.api.dom.java.FullyQualifiedJavaType 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;
}
Aggregations