private void addUserDefinedMethods(TopLevelClass exampleClass, Interface mapperClass, IntrospectedTable introspectedTable, MyBatisClasses cls) { for (Method action : mapperClass.getMethods()) { if (!userDefinedMethods.matcher(action.getName()).matches()) continue; StringBuilder args = new StringBuilder(); List<Parameter> params = new ArrayList<Parameter>(); boolean example = false; if (action.getParameters() != null) for (Parameter param : action.getParameters()) { String name; if (Objects.equals(param.getType(), exampleClass.getType())) { example = true; name = "this"; } else { name = param.getName(); params.add(new Parameter(param.getType(), name)); } if (args.length() > 0) args.append(", "); args.append(name); } if (!example) { //System.err.println("Invalid user-defined mapper method: "+action.getName()); continue; } exampleClass.addMethod(method( PUBLIC, INT, action.getName(), _(sqlSession, "sql"), params.toArray(new Parameter[params.size()]), __( "return sql.getMapper(" + cls.names.mapper + ".class)."+action.getName()+"("+args+");" ))); exampleClass.addMethod(method( PUBLIC, INT, action.getName(), _(cls.types.mapper, "mapper"), params.toArray(new Parameter[params.size()]), __( "return mapper."+action.getName()+"("+args+");" ))); } }
/** * xml中的注释 * * @param xmlElement */ public void addComment(XmlElement xmlElement) { xmlElement.addElement(new TextElement("<!--")); StringBuilder sb = new StringBuilder(); sb.append(" WARNING - "); sb.append(MergeConstants.NEW_ELEMENT_TAG); xmlElement.addElement(new TextElement(sb.toString())); xmlElement.addElement(new TextElement("-->")); }
/** * 删除标记 * * @param javaElement * @param markAsDoNotDelete */ protected void addJavadocTag(JavaElement javaElement, boolean markAsDoNotDelete) { StringBuilder sb = new StringBuilder(); sb.append(" * "); sb.append(MergeConstants.NEW_ELEMENT_TAG); if (markAsDoNotDelete) { sb.append(" do_not_delete_during_merge"); } javaElement.addJavaDocLine(sb.toString()); }
/** * Adds a suitable comment to warn users that the element was generated, and when it was * generated. */ public void addComment(XmlElement xmlElement) { if (suppressAllComments) { return; } xmlElement.addElement(new TextElement("<!--")); // $NON-NLS-1$ StringBuilder sb = new StringBuilder(); sb.append(" WARNING - "); // $NON-NLS-1$ sb.append(MergeConstants.NEW_ELEMENT_TAG); xmlElement.addElement(new TextElement(sb.toString())); xmlElement.addElement( new TextElement( " This element is automatically generated by MyBatis Generator, do not modify.")); //$NON-NLS-1$ String s = getDateString(); if (s != null) { sb.setLength(0); sb.append(" This element was generated on "); // $NON-NLS-1$ sb.append(s); sb.append('.'); xmlElement.addElement(new TextElement(sb.toString())); } xmlElement.addElement(new TextElement("-->")); // $NON-NLS-1$ }
/** * 给字段添加数据库备注 * * @param field * @param introspectedTable * @param introspectedColumn */ public void addFieldComment( Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { if (StringUtility.stringHasValue(introspectedColumn.getRemarks())) { field.addJavaDocLine("/**"); StringBuilder sb = new StringBuilder(); sb.append(" * "); sb.append(introspectedColumn.getRemarks()); field.addJavaDocLine(sb.toString()); field.addJavaDocLine(" */"); } // 添加注解 if (field.isTransient()) { // @Column field.addAnnotation("@Transient"); } for (IntrospectedColumn column : introspectedTable.getPrimaryKeyColumns()) { if (introspectedColumn == column) { field.addAnnotation("@Id"); break; } } String column = introspectedColumn.getActualColumnName(); if (StringUtility.stringContainsSpace(column) || introspectedTable.getTableConfiguration().isAllColumnDelimitingEnabled()) { column = introspectedColumn.getContext().getBeginningDelimiter() + column + introspectedColumn.getContext().getEndingDelimiter(); } if (!column.equals(introspectedColumn.getJavaProperty())) { // @Column field.addAnnotation("@Column(name = \"" + column + "\")"); } if (introspectedColumn.isIdentity()) { if (introspectedTable .getTableConfiguration() .getGeneratedKey() .getRuntimeSqlStatement() .equals("JDBC")) { field.addAnnotation("@GeneratedValue(generator = \"JDBC\")"); } else { field.addAnnotation("@GeneratedValue(strategy = GenerationType.IDENTITY)"); } } else if (introspectedColumn.isSequenceColumn()) { field.addAnnotation( "@SequenceGenerator(name=\"\",sequenceName=\"" + introspectedTable.getTableConfiguration().getGeneratedKey().getRuntimeSqlStatement() + "\")"); } }
public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) { if (suppressAllComments) { return; } StringBuilder sb = new StringBuilder(); method.addJavaDocLine("/**"); // $NON-NLS-1$ method.addJavaDocLine(" * This method was generated by MyBatis Generator."); // $NON-NLS-1$ sb.append(" * This method corresponds to the database table "); // $NON-NLS-1$ sb.append(introspectedTable.getFullyQualifiedTable()); method.addJavaDocLine(sb.toString()); addJavadocTag(method, false); method.addJavaDocLine(" */"); // $NON-NLS-1$ }
public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) { if (suppressAllComments) { return; } StringBuilder sb = new StringBuilder(); innerEnum.addJavaDocLine("/**"); // $NON-NLS-1$ innerEnum.addJavaDocLine(" * This enum was generated by MyBatis Generator."); // $NON-NLS-1$ sb.append(" * This enum corresponds to the database table "); // $NON-NLS-1$ sb.append(introspectedTable.getFullyQualifiedTable()); innerEnum.addJavaDocLine(sb.toString()); addJavadocTag(innerEnum, false); innerEnum.addJavaDocLine(" */"); // $NON-NLS-1$ }
public void addClassComment( InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete) { if (suppressAllComments) { return; } StringBuilder sb = new StringBuilder(); innerClass.addJavaDocLine("/**"); // $NON-NLS-1$ innerClass.addJavaDocLine(" * This class was generated by MyBatis Generator."); // $NON-NLS-1$ sb.append(" * This class corresponds to the database table "); // $NON-NLS-1$ sb.append(introspectedTable.getFullyQualifiedTable()); innerClass.addJavaDocLine(sb.toString()); addJavadocTag(innerClass, markAsDoNotDelete); innerClass.addJavaDocLine(" */"); // $NON-NLS-1$ }
/** * getter方法注释 * * @param method * @param introspectedTable * @param introspectedColumn */ public void addGetterComment( Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { StringBuilder sb = new StringBuilder(); method.addJavaDocLine("/**"); if (StringUtility.stringHasValue(introspectedColumn.getRemarks())) { sb.append(" * 获取"); sb.append(introspectedColumn.getRemarks()); method.addJavaDocLine(sb.toString()); method.addJavaDocLine(" *"); } sb.setLength(0); sb.append(" * @return "); sb.append(introspectedColumn.getActualColumnName()); if (StringUtility.stringHasValue(introspectedColumn.getRemarks())) { sb.append(" - "); sb.append(introspectedColumn.getRemarks()); } method.addJavaDocLine(sb.toString()); method.addJavaDocLine(" */"); }
/** * setter方法注释 * * @param method * @param introspectedTable * @param introspectedColumn */ public void addSetterComment( Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { StringBuilder sb = new StringBuilder(); method.addJavaDocLine("/**"); if (StringUtility.stringHasValue(introspectedColumn.getRemarks())) { sb.append(" * 设置"); sb.append(introspectedColumn.getRemarks()); method.addJavaDocLine(sb.toString()); method.addJavaDocLine(" *"); } Parameter parm = method.getParameters().get(0); sb.setLength(0); sb.append(" * @param "); sb.append(parm.getName()); if (StringUtility.stringHasValue(introspectedColumn.getRemarks())) { sb.append(" "); sb.append(introspectedColumn.getRemarks()); } method.addJavaDocLine(sb.toString()); method.addJavaDocLine(" */"); }
private void addCriteriaMethods(TopLevelClass topLevelClass, int newMethodsStart) { if (!generateCriteriaMethods) return; InnerClass criteria = null; for (InnerClass c : topLevelClass.getInnerClasses()) { if (c.getType().getShortName().equals("Criteria")) criteria = c; } if (criteria == null) return; boolean owner = false; for (Field f : criteria.getFields()) if (ExampleMethodsChainPlugin.OWNER.equals(f.getName())) owner = true; if (!owner) return; for (ListIterator<Method> methods = topLevelClass.getMethods().listIterator(newMethodsStart); methods.hasNext(); ) { Method base = methods.next(); if (base.getVisibility() != PUBLIC || base.isStatic() || base.isConstructor()) continue; Method m = method(PUBLIC, base.getReturnType(), base.getName()); StringBuilder sb = new StringBuilder(); sb.append("return ").append(ExampleMethodsChainPlugin.OWNER).append(".").append(base.getName()).append("("); for (ListIterator<Parameter> params = base.getParameters().listIterator(); params.hasNext(); ) { if (params.hasPrevious()) sb.append(", "); Parameter p = params.next(); m.addParameter(new Parameter(p.getType(), p.getName())); sb.append(p.getName()); } sb.append(");"); m.addBodyLine(sb.toString()); criteria.addMethod(m); } }
/** * This method adds the custom javadoc tag for. You may do nothing if you do not wish to include * the Javadoc tag - however, if you do not include the Javadoc tag then the Java merge capability * of the eclipse plugin will break. * * @param javaElement the java element */ protected void addJavadocTag(JavaElement javaElement, boolean markAsDoNotDelete) { javaElement.addJavaDocLine(" *"); // $NON-NLS-1$ StringBuilder sb = new StringBuilder(); sb.append(" * "); // $NON-NLS-1$ sb.append(MergeConstants.NEW_ELEMENT_TAG); if (markAsDoNotDelete) { sb.append(" do_not_delete_during_merge"); // $NON-NLS-1$ } String s = getDateString(); if (s != null) { sb.append(' '); sb.append(s); } javaElement.addJavaDocLine(sb.toString()); }
public void addSetterComment( Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { if (suppressAllComments) { return; } StringBuilder sb = new StringBuilder(); method.addJavaDocLine("/**"); // $NON-NLS-1$ method.addJavaDocLine(" * This method was generated by MyBatis Generator."); // $NON-NLS-1$ sb.append(" * This method sets the value of the database column "); // $NON-NLS-1$ sb.append(introspectedTable.getFullyQualifiedTable()); sb.append('.'); sb.append(introspectedColumn.getActualColumnName()); method.addJavaDocLine(sb.toString()); method.addJavaDocLine(" *"); // $NON-NLS-1$ Parameter parm = method.getParameters().get(0); sb.setLength(0); sb.append(" * @param "); // $NON-NLS-1$ sb.append(parm.getName()); sb.append(" the value for "); // $NON-NLS-1$ sb.append(introspectedTable.getFullyQualifiedTable()); sb.append('.'); sb.append(introspectedColumn.getActualColumnName()); method.addJavaDocLine(sb.toString()); addJavadocTag(method, false); method.addJavaDocLine(" */"); // $NON-NLS-1$ }