use of org.mybatis.generator.api.dom.java.Method 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 (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(MyBatis3FormattingUtilities.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.Method in project generator by mybatis.
the class PrimaryKeyGenerator 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);
StringBuilder sb = new StringBuilder();
for (IntrospectedColumn introspectedColumn : introspectedTable.getPrimaryKeyColumns()) {
method.addParameter(new Parameter(introspectedColumn.getFullyQualifiedJavaType(), introspectedColumn.getJavaProperty()));
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.Method 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;
}
use of org.mybatis.generator.api.dom.java.Method in project generator by mybatis.
the class Test1Generator method generate.
public List<CompilationUnit> generate() {
FullyQualifiedJavaType cls = new FullyQualifiedJavaType(BASE_PACKAGE + ".SomeClass");
List<CompilationUnit> answer = new ArrayList<CompilationUnit>();
TopLevelClass tlcMain = generateFieldTypeMain();
TopLevelClass tlcSub1 = generateFieldTypeSub1();
TopLevelClass tlcTcSub1 = generateTestClassSub1();
TopLevelClass tlcSub2 = generateFieldTypeSub2();
answer.add(tlcMain);
answer.add(tlcSub1);
answer.add(tlcTcSub1);
answer.add(tlcSub2);
TopLevelClass topLvlClass = new TopLevelClass(cls);
topLvlClass.setVisibility(JavaVisibility.PUBLIC);
topLvlClass.addImportedType(tlcTcSub1.getType());
Field field = new Field("main", tlcMain.getType());
field.setVisibility(JavaVisibility.PRIVATE);
topLvlClass.addField(field);
field = new Field("tcSub1", tlcTcSub1.getType());
field.setVisibility(JavaVisibility.PRIVATE);
topLvlClass.addField(field);
field = new Field("sub1", tlcSub1.getType());
field.setVisibility(JavaVisibility.PRIVATE);
topLvlClass.addField(field);
field = new Field("sub2", tlcSub2.getType());
field.setVisibility(JavaVisibility.PRIVATE);
topLvlClass.addField(field);
Method m = new Method();
m.setVisibility(JavaVisibility.PUBLIC);
m.setName("executeMain");
m.addBodyLine("main.mainMethod();");
topLvlClass.addMethod(m);
m = new Method();
m.setVisibility(JavaVisibility.PUBLIC);
m.setName("setMain");
m.addParameter(new Parameter(tlcMain.getType(), "main"));
m.addBodyLine("this.main = main;");
topLvlClass.addMethod(m);
m = new Method();
m.setVisibility(JavaVisibility.PUBLIC);
m.setName("getMain");
m.setReturnType(tlcMain.getType());
m.addBodyLine("return main;");
topLvlClass.addMethod(m);
m = new Method();
m.setVisibility(JavaVisibility.PUBLIC);
m.setName("executeSub1");
m.addBodyLine("sub1.sub1Method();");
topLvlClass.addMethod(m);
m = new Method();
m.setVisibility(JavaVisibility.PUBLIC);
m.setName("setSub1");
m.addParameter(new Parameter(tlcSub1.getType(), "sub1"));
m.addBodyLine("this.sub1 = sub1;");
topLvlClass.addMethod(m);
m = new Method();
m.setVisibility(JavaVisibility.PUBLIC);
m.setName("getSub1");
m.setReturnType(tlcSub1.getType());
m.addBodyLine("return sub1;");
topLvlClass.addMethod(m);
m = new Method();
m.setVisibility(JavaVisibility.PUBLIC);
m.setName("executeSub2");
m.addBodyLine("sub2.sub2Method();");
topLvlClass.addMethod(m);
m = new Method();
m.setVisibility(JavaVisibility.PUBLIC);
m.setName("setSub2");
m.addParameter(new Parameter(tlcSub2.getType(), "sub2"));
m.addBodyLine("this.sub2 = sub2;");
topLvlClass.addMethod(m);
m = new Method();
m.setVisibility(JavaVisibility.PUBLIC);
m.setName("getSub2");
m.setReturnType(tlcSub2.getType());
m.addBodyLine("return sub2;");
topLvlClass.addMethod(m);
answer.add(topLvlClass);
return answer;
}
use of org.mybatis.generator.api.dom.java.Method in project generator by mybatis.
the class UpdateByPrimaryKeySelectiveMethodGenerator method getMethodShell.
private Method getMethodShell(Set<FullyQualifiedJavaType> importedTypes) {
FullyQualifiedJavaType parameterType;
if (introspectedTable.getRules().generateRecordWithBLOBsClass()) {
parameterType = new FullyQualifiedJavaType(introspectedTable.getRecordWithBLOBsType());
} else {
parameterType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());
}
importedTypes.add(parameterType);
Method method = new Method();
method.setVisibility(JavaVisibility.PUBLIC);
method.setReturnType(FullyQualifiedJavaType.getIntInstance());
method.setName(getDAOMethodNameCalculator().getUpdateByPrimaryKeySelectiveMethodName(introspectedTable));
//$NON-NLS-1$
method.addParameter(new Parameter(parameterType, "record"));
for (FullyQualifiedJavaType fqjt : daoTemplate.getCheckedExceptions()) {
method.addException(fqjt);
importedTypes.add(fqjt);
}
context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);
return method;
}
Aggregations