@Override public void addInterfaceElements(Interface interfaze) { Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>(); Method method = new Method(); FullyQualifiedJavaType returnType = FullyQualifiedJavaType.getNewListInstance(); importedTypes.add(returnType); FullyQualifiedJavaType listType; listType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType()); importedTypes.add(listType); returnType.addTypeArgument(listType); method.setReturnType(returnType); method.setVisibility(JavaVisibility.PUBLIC); method.setName(introspectedTable.getSelectSelectiveStatementId()); FullyQualifiedJavaType parameterType = introspectedTable.getRules().calculateAllFieldsClass(); importedTypes.add(parameterType); method.addParameter(new Parameter(parameterType, "record")); // $NON-NLS-1$ context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable); addMapperAnnotations(interfaze, method); if (context .getPlugins() .clientSelectSelectiveMethodGenerated(method, interfaze, introspectedTable)) { interfaze.addImportedTypes(importedTypes); interfaze.addMethod(method); } }
@Override public void addInterfaceElements(Interface interfaze) { Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>(); Method method = getMethodShell(importedTypes); if (context .getPlugins() .clientUpdateByPrimaryKeyWithBLOBsMethodGenerated(method, interfaze, introspectedTable)) { interfaze.addImportedTypes(importedTypes); interfaze.addMethod(method); } }
@Override public void addInterfaceElements(Interface interfaze) { if (getExampleMethodVisibility() == JavaVisibility.PUBLIC) { Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>(); Method method = getMethodShell(importedTypes); if (context .getPlugins() .clientSelectByExampleWithoutBLOBsMethodGenerated(method, interfaze, introspectedTable)) { interfaze.addImportedTypes(importedTypes); interfaze.addMethod(method); } } }
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+");" ))); } }
/** 导入需要的类 */ private void addImport(Interface interfaces, TopLevelClass topLevelClass) { interfaces.addImportedType(pojoType); interfaces.addImportedType(pojoCriteriaType); interfaces.addImportedType(listType); topLevelClass.addImportedType(daoType); topLevelClass.addImportedType(interfaceType); topLevelClass.addImportedType(pojoType); topLevelClass.addImportedType(pojoCriteriaType); topLevelClass.addImportedType(listType); topLevelClass.addImportedType(slf4jLogger); topLevelClass.addImportedType(slf4jLoggerFactory); if (enableAnnotation) { topLevelClass.addImportedType(service); topLevelClass.addImportedType(autowired); } }
@Override public void addInterfaceElements(Interface interfaze) { Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>(); FullyQualifiedJavaType type = new FullyQualifiedJavaType(introspectedTable.getExampleType()); importedTypes.add(type); importedTypes.add(FullyQualifiedJavaType.getNewListInstance()); Method method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); FullyQualifiedJavaType returnType = FullyQualifiedJavaType.getNewListInstance(); FullyQualifiedJavaType listType; if (introspectedTable.getRules().generateBaseRecordClass()) { listType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType()); } else if (introspectedTable.getRules().generatePrimaryKeyClass()) { listType = new FullyQualifiedJavaType(introspectedTable.getPrimaryKeyType()); } else { throw new RuntimeException(getString("RuntimeError.12")); // $NON-NLS-1$ } importedTypes.add(listType); returnType.addTypeArgument(listType); method.setReturnType(returnType); method.setName(introspectedTable.getSelectByExampleStatementId()); method.addParameter(new Parameter(type, "example")); // $NON-NLS-1$ context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable); addMapperAnnotations(interfaze, method); if (context .getPlugins() .clientSelectByExampleWithoutBLOBsMethodGenerated(method, interfaze, introspectedTable)) { interfaze.addImportedTypes(importedTypes); interfaze.addMethod(method); } }
@Override public void addInterfaceElements(Interface interfaze) { Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>(); FullyQualifiedJavaType type = new FullyQualifiedJavaType(introspectedTable.getExampleType()); importedTypes.add(type); Method method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setReturnType(FullyQualifiedJavaType.getIntInstance()); method.setName(introspectedTable.getDeleteByExampleStatementId()); method.addParameter(new Parameter(type, "condition")); // $NON-NLS-1$ context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable); addMapperAnnotations(interfaze, method); if (context .getPlugins() .clientDeleteByExampleMethodGenerated(method, interfaze, introspectedTable)) { interfaze.addImportedTypes(importedTypes); interfaze.addMethod(method); } }
@Override public void addMapperAnnotations(Interface interfaze, Method method) { FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(introspectedTable.getMyBatis3SqlProviderType()); interfaze.addImportedType( new FullyQualifiedJavaType("org.apache.ibatis.annotations.DeleteProvider")); // $NON-NLS-1$ StringBuilder sb = new StringBuilder(); sb.append("@DeleteProvider(type="); // $NON-NLS-1$ sb.append(fqjt.getShortName()); sb.append(".class, method=\""); // $NON-NLS-1$ sb.append(introspectedTable.getDeleteByExampleStatementId()); sb.append("\")"); // $NON-NLS-1$ method.addAnnotation(sb.toString()); }
@Override public void addMapperAnnotations(Interface interfaze, Method method) { interfaze.addImportedType( new FullyQualifiedJavaType("org.apache.ibatis.annotations.Delete")); // $NON-NLS-1$ method.addAnnotation("@Delete({"); // $NON-NLS-1$ StringBuilder sb = new StringBuilder(); javaIndent(sb, 1); sb.append("\"delete from "); // $NON-NLS-1$ sb.append(escapeStringForJava(introspectedTable.getFullyQualifiedTableNameAtRuntime())); sb.append("\","); // $NON-NLS-1$ method.addAnnotation(sb.toString()); boolean and = false; Iterator<IntrospectedColumn> iter = introspectedTable.getPrimaryKeyColumns().iterator(); while (iter.hasNext()) { IntrospectedColumn introspectedColumn = iter.next(); sb.setLength(0); javaIndent(sb, 1); if (and) { sb.append(" \"and "); // $NON-NLS-1$ } else { sb.append("\"where "); // $NON-NLS-1$ and = true; } sb.append(escapeStringForJava(getEscapedColumnName(introspectedColumn))); sb.append(" = "); // $NON-NLS-1$ sb.append(getParameterClause(introspectedColumn)); sb.append('\"'); if (iter.hasNext()) { sb.append(','); } method.addAnnotation(sb.toString()); } method.addAnnotation("})"); // $NON-NLS-1$ }
/** * 添加接口 * * @param tableName * @param files */ protected void addService( Interface interface1, IntrospectedTable introspectedTable, String tableName, List<GeneratedJavaFile> files) { interface1.setVisibility(JavaVisibility.PUBLIC); // 添加方法 Method method = countByExample(introspectedTable, tableName); method.removeAllBodyLines(); interface1.addMethod(method); method = selectByPrimaryKey(introspectedTable, tableName); method.removeAllBodyLines(); interface1.addMethod(method); method = selectByExample(introspectedTable, tableName); method.removeAllBodyLines(); interface1.addMethod(method); if (enableDeleteByPrimaryKey) { method = getOtherInteger("deleteByPrimaryKey", introspectedTable, tableName, 2); method.removeAllBodyLines(); interface1.addMethod(method); } if (enableUpdateByPrimaryKeySelective) { method = getOtherInteger("updateByPrimaryKeySelective", introspectedTable, tableName, 1); method.removeAllBodyLines(); interface1.addMethod(method); } if (enableUpdateByPrimaryKey) { method = getOtherInteger("updateByPrimaryKey", introspectedTable, tableName, 1); method.removeAllBodyLines(); interface1.addMethod(method); } if (enableDeleteByExample) { method = getOtherInteger("deleteByExample", introspectedTable, tableName, 3); method.removeAllBodyLines(); interface1.addMethod(method); } if (enableUpdateByExampleSelective) { method = getOtherInteger("updateByExampleSelective", introspectedTable, tableName, 4); method.removeAllBodyLines(); interface1.addMethod(method); } if (enableUpdateByExample) { method = getOtherInteger("updateByExample", introspectedTable, tableName, 4); method.removeAllBodyLines(); interface1.addMethod(method); } if (enableInsert) { method = getOtherInsertboolean("insert", introspectedTable, tableName); method.removeAllBodyLines(); interface1.addMethod(method); } if (enableInsertSelective) { method = getOtherInsertboolean("insertSelective", introspectedTable, tableName); method.removeAllBodyLines(); interface1.addMethod(method); } GeneratedJavaFile file = new GeneratedJavaFile( interface1, project, context.getProperty(PropertyRegistry.CONTEXT_JAVA_FILE_ENCODING), context.getJavaFormatter()); files.add(file); }