@Override public List<CompilationUnit> getCompilationUnits() { FullyQualifiedTable table = introspectedTable.getFullyQualifiedTable(); progressCallback.startTask(getString("Progress.9", table.toString())); // $NON-NLS-1$ Plugin plugins = context.getPlugins(); CommentGenerator commentGenerator = context.getCommentGenerator(); TopLevelClass topLevelClass = new TopLevelClass(introspectedTable.getRecordWithBLOBsType()); topLevelClass.setVisibility(JavaVisibility.PUBLIC); commentGenerator.addJavaFileComment(topLevelClass); topLevelClass.addJavaDocLine("/**"); topLevelClass.addJavaDocLine("* " + table.getDomainObjectName()); topLevelClass.addJavaDocLine("* table:" + table.getIntrospectedTableName()); topLevelClass.addJavaDocLine("* "); topLevelClass.addJavaDocLine("* @author 王欣"); topLevelClass.addJavaDocLine("* @version v1.0"); // topLevelClass.addJavaDocLine("* @copy 鸿旭图码"); topLevelClass.addJavaDocLine("* @copy wangxin"); topLevelClass.addJavaDocLine( "* @date " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); topLevelClass.addJavaDocLine("*/"); String rootClass = getRootClass(); if (introspectedTable.getRules().generateBaseRecordClass()) { topLevelClass.setSuperClass(introspectedTable.getBaseRecordType()); } else { topLevelClass.setSuperClass(introspectedTable.getPrimaryKeyType()); } if (introspectedTable.isConstructorBased()) { addParameterizedConstructor(topLevelClass); if (!introspectedTable.isImmutable()) { addDefaultConstructor(topLevelClass); } } for (IntrospectedColumn introspectedColumn : introspectedTable.getBLOBColumns()) { if (RootClassInfo.getInstance(rootClass, warnings).containsProperty(introspectedColumn)) { continue; } Field field = getJavaBeansField(introspectedColumn); if (plugins.modelFieldGenerated( field, topLevelClass, introspectedColumn, introspectedTable, Plugin.ModelClassType.RECORD_WITH_BLOBS)) { topLevelClass.addField(field); topLevelClass.addImportedType(field.getType()); } Method method = getJavaBeansGetter(introspectedColumn); if (plugins.modelGetterMethodGenerated( method, topLevelClass, introspectedColumn, introspectedTable, Plugin.ModelClassType.RECORD_WITH_BLOBS)) { topLevelClass.addMethod(method); } if (!introspectedTable.isImmutable()) { method = getJavaBeansSetter(introspectedColumn); if (plugins.modelSetterMethodGenerated( method, topLevelClass, introspectedColumn, introspectedTable, Plugin.ModelClassType.RECORD_WITH_BLOBS)) { topLevelClass.addMethod(method); } } } List<CompilationUnit> answer = new ArrayList<CompilationUnit>(); if (context.getPlugins().modelRecordWithBLOBsClassGenerated(topLevelClass, introspectedTable)) { answer.add(topLevelClass); } return answer; }
/** * 添加实现类 * * @param introspectedTable * @param tableName * @param files */ protected void addServiceImpl( TopLevelClass topLevelClass, IntrospectedTable introspectedTable, String tableName, List<GeneratedJavaFile> files) { topLevelClass.setVisibility(JavaVisibility.PUBLIC); // 设置实现的接口 topLevelClass.addSuperInterface(interfaceType); if (enableAnnotation) { topLevelClass.addAnnotation("@Service"); topLevelClass.addImportedType(service); } // 添加引用dao addField(topLevelClass, tableName); // 添加方法 topLevelClass.addMethod(countByExample(introspectedTable, tableName)); topLevelClass.addMethod(selectByPrimaryKey(introspectedTable, tableName)); topLevelClass.addMethod(selectByExample(introspectedTable, tableName)); /** type 的意义 pojo 1 ;key 2 ;example 3 ;pojo+example 4 */ if (enableDeleteByPrimaryKey) { topLevelClass.addMethod( getOtherInteger("deleteByPrimaryKey", introspectedTable, tableName, 2)); } if (enableUpdateByPrimaryKeySelective) { topLevelClass.addMethod( getOtherInteger("updateByPrimaryKeySelective", introspectedTable, tableName, 1)); } if (enableUpdateByPrimaryKey) { topLevelClass.addMethod( getOtherInteger("updateByPrimaryKey", introspectedTable, tableName, 1)); } if (enableDeleteByExample) { topLevelClass.addMethod(getOtherInteger("deleteByExample", introspectedTable, tableName, 3)); } if (enableUpdateByExampleSelective) { topLevelClass.addMethod( getOtherInteger("updateByExampleSelective", introspectedTable, tableName, 4)); } if (enableUpdateByExample) { topLevelClass.addMethod(getOtherInteger("updateByExample", introspectedTable, tableName, 4)); } if (enableInsert) { topLevelClass.addMethod(getOtherInsertboolean("insert", introspectedTable, tableName)); } if (enableInsertSelective) { topLevelClass.addMethod( getOtherInsertboolean("insertSelective", introspectedTable, tableName)); } // 生成文件 GeneratedJavaFile file = new GeneratedJavaFile( topLevelClass, project, context.getProperty(PropertyRegistry.CONTEXT_JAVA_FILE_ENCODING), context.getJavaFormatter()); files.add(file); }
@Override public List<CompilationUnit> getCompilationUnits() { FullyQualifiedTable table = introspectedTable.getFullyQualifiedTable(); progressCallback.startTask(getString("Progress.6", table.toString())); // $NON-NLS-1$ 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()); method.addBodyLine("oredCriteria = new ArrayList<Criteria>();"); // $NON-NLS-1$ 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()); field.setName("orderByClause"); // $NON-NLS-1$ commentGenerator.addFieldComment(field, introspectedTable); topLevelClass.addField(field); method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setName("setOrderByClause"); // $NON-NLS-1$ method.addParameter( new Parameter(FullyQualifiedJavaType.getStringInstance(), "orderByClause")); // $NON-NLS-1$ method.addBodyLine("this.orderByClause = orderByClause;"); // $NON-NLS-1$ commentGenerator.addGeneralMethodComment(method, introspectedTable); topLevelClass.addMethod(method); method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setReturnType(FullyQualifiedJavaType.getStringInstance()); method.setName("getOrderByClause"); // $NON-NLS-1$ method.addBodyLine("return orderByClause;"); // $NON-NLS-1$ commentGenerator.addGeneralMethodComment(method, introspectedTable); topLevelClass.addMethod(method); // add field, getter, setter for distinct field = new Field(); field.setVisibility(JavaVisibility.PROTECTED); field.setType(FullyQualifiedJavaType.getBooleanPrimitiveInstance()); field.setName("distinct"); // $NON-NLS-1$ commentGenerator.addFieldComment(field, introspectedTable); topLevelClass.addField(field); method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setName("setDistinct"); // $NON-NLS-1$ method.addParameter( new Parameter( FullyQualifiedJavaType.getBooleanPrimitiveInstance(), "distinct")); // $NON-NLS-1$ method.addBodyLine("this.distinct = distinct;"); // $NON-NLS-1$ commentGenerator.addGeneralMethodComment(method, introspectedTable); topLevelClass.addMethod(method); method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setReturnType(FullyQualifiedJavaType.getBooleanPrimitiveInstance()); method.setName("isDistinct"); // $NON-NLS-1$ method.addBodyLine("return distinct;"); // $NON-NLS-1$ 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("java.util.List<Criteria>"); // $NON-NLS-1$ field.setType(fqjt); field.setName("oredCriteria"); // $NON-NLS-1$ commentGenerator.addFieldComment(field, introspectedTable); topLevelClass.addField(field); method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setReturnType(fqjt); method.setName("getOredCriteria"); // $NON-NLS-1$ method.addBodyLine("return oredCriteria;"); // $NON-NLS-1$ commentGenerator.addGeneralMethodComment(method, introspectedTable); topLevelClass.addMethod(method); method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setName("or"); // $NON-NLS-1$ method.addParameter( new Parameter(FullyQualifiedJavaType.getCriteriaInstance(), "criteria")); // $NON-NLS-1$ method.addBodyLine("oredCriteria.add(criteria);"); // $NON-NLS-1$ commentGenerator.addGeneralMethodComment(method, introspectedTable); topLevelClass.addMethod(method); method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setName("or"); // $NON-NLS-1$ method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance()); method.addBodyLine("Criteria criteria = createCriteriaInternal();"); // $NON-NLS-1$ method.addBodyLine("oredCriteria.add(criteria);"); // $NON-NLS-1$ method.addBodyLine("return criteria;"); // $NON-NLS-1$ commentGenerator.addGeneralMethodComment(method, introspectedTable); topLevelClass.addMethod(method); method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setName("createCriteria"); // $NON-NLS-1$ method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance()); 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;"); // $NON-NLS-1$ commentGenerator.addGeneralMethodComment(method, introspectedTable); topLevelClass.addMethod(method); method = new Method(); method.setVisibility(JavaVisibility.PROTECTED); method.setName("createCriteriaInternal"); // $NON-NLS-1$ method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance()); method.addBodyLine("Criteria criteria = new Criteria();"); // $NON-NLS-1$ method.addBodyLine("return criteria;"); // $NON-NLS-1$ commentGenerator.addGeneralMethodComment(method, introspectedTable); topLevelClass.addMethod(method); method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setName("clear"); // $NON-NLS-1$ method.addBodyLine("oredCriteria.clear();"); // $NON-NLS-1$ method.addBodyLine("orderByClause = null;"); // $NON-NLS-1$ method.addBodyLine("distinct = false;"); // $NON-NLS-1$ 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)); topLevelClass.addInnerClass(getCriterionInnerClass(topLevelClass)); List<CompilationUnit> answer = new ArrayList<CompilationUnit>(); if (context.getPlugins().modelExampleClassGenerated(topLevelClass, introspectedTable)) { answer.add(topLevelClass); } return answer; }