Java Code Examples for org.mybatis.generator.api.dom.java.Field#addAnnotation()

The following examples show how to use org.mybatis.generator.api.dom.java.Field#addAnnotation() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultCommentGenerator.java    From mapper-generator-javafx with Apache License 2.0 6 votes vote down vote up
@Override
public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable,
        IntrospectedColumn introspectedColumn, Set<FullyQualifiedJavaType> imports) {
    imports.add(new FullyQualifiedJavaType("javax.annotation.Generated"));
    String comment = "Source field: "
            + introspectedTable.getFullyQualifiedTable().toString()
            + "."
            + introspectedColumn.getActualColumnName();
    field.addAnnotation(getGeneratedAnnotation(comment));
    
    if (!suppressAllComments && addRemarkComments) {
        String remarks = introspectedColumn.getRemarks();
        if (addRemarkComments && StringUtility.stringHasValue(remarks)) {
            field.addJavaDocLine("/**");
            field.addJavaDocLine(" * Database Column Remarks:");
            String[] remarkLines = remarks.split(System.getProperty("line.separator")); 
            for (String remarkLine : remarkLines) {
                field.addJavaDocLine(" *   " + remarkLine); 
            }
            field.addJavaDocLine(" */");
        }
    }
}
 
Example 2
Source File: HySwaggerMapperPlugin.java    From jvue-admin with MIT License 6 votes vote down vote up
private void columnTypeAnnotation(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
	if (this.columnTypeEnabled) {
		String columnType = "tk.mybatis.mapper.annotation.ColumnType";
		String jdbcType = "org.apache.ibatis.type.JdbcType";
		topLevelClass.addImportedType(columnType);
		topLevelClass.addImportedType(jdbcType);
		List<Field> fields = topLevelClass.getFields();
		for (Field field : fields) {
			List<IntrospectedColumn> allColumns = introspectedTable.getAllColumns();
			for (IntrospectedColumn introspectedColumn : allColumns)
				if (field.getName().equals(introspectedColumn.getJavaProperty())) {
					String jdbcTypeName = introspectedColumn.getJdbcTypeName();
					if(StringUtil.isEmpty(jdbcTypeName)){
						continue;
					}else{
						jdbcTypeName="JdbcType."+jdbcTypeName;
					}
					field.addAnnotation("@ColumnType(jdbcType=" + jdbcTypeName + ")");
				}
		}
	}
}
 
Example 3
Source File: GeneratorSwagger2Doc.java    From mybatis-generator-plugins with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public boolean modelFieldGenerated(Field field, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
    String classAnnotation = "@ApiModel(value=\"" + topLevelClass.getType()  + "\")";
    if(!topLevelClass.getAnnotations().contains(classAnnotation)) {
        topLevelClass.addAnnotation(classAnnotation);
    }

    String apiModelAnnotationPackage =  properties.getProperty("apiModelAnnotationPackage");
    String apiModelPropertyAnnotationPackage = properties.getProperty("apiModelPropertyAnnotationPackage");
    if(null == apiModelAnnotationPackage) apiModelAnnotationPackage = "io.swagger.annotations.ApiModel";
    if(null == apiModelPropertyAnnotationPackage) apiModelPropertyAnnotationPackage = "io.swagger.annotations.ApiModelProperty";

    topLevelClass.addImportedType(apiModelAnnotationPackage);
    topLevelClass.addImportedType(apiModelPropertyAnnotationPackage);

    field.addAnnotation("@ApiModelProperty(value=\"" + introspectedColumn.getJavaProperty() + introspectedColumn.getRemarks() + "\")");
    return super.modelFieldGenerated(field, topLevelClass, introspectedColumn, introspectedTable, modelClassType);
}
 
Example 4
Source File: CustomCommentGenerator.java    From BlogManagePlatform with Apache License 2.0 6 votes vote down vote up
private void resolvePrimaryKey(Field field, IntrospectedTable table, IntrospectedColumn column) {
	String columnName = column.getActualColumnName();
	List<IntrospectedColumn> primaryKey = table.getPrimaryKeyColumns();
	String remark = column.getRemarks();
	for (IntrospectedColumn pk : primaryKey) {
		if (columnName.equals(pk.getActualColumnName())) {
			field.addAnnotation("@Id");
			String tableRemark = table.getRemarks();
			if (tableRemark.endsWith("表")) {
				tableRemark = tableRemark.substring(0, tableRemark.length() - 1);
			}
			remark = tableRemark + remark;
			break;
		}
	}
}
 
Example 5
Source File: DefaultCommentGenerator.java    From mapper-generator-javafx with Apache License 2.0 5 votes vote down vote up
@Override
public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable,
        Set<FullyQualifiedJavaType> imports) {
    imports.add(new FullyQualifiedJavaType("javax.annotation.Generated"));
    String comment = "Source Table: " + introspectedTable.getFullyQualifiedTable().toString();
    field.addAnnotation(getGeneratedAnnotation(comment));
}
 
Example 6
Source File: CustomCommentGenerator.java    From BlogManagePlatform with Apache License 2.0 5 votes vote down vote up
private void resolveNullable(Field field, IntrospectedTable table, IntrospectedColumn column) {
	if (column.isNullable()) {
		field.addAnnotation("@Nullable");
	} else {
		if (field.getType().getShortName().equals("String")) {
			field.addAnnotation("@NotBlank");
		} else {
			field.addAnnotation("@NotNull");
		}
	}
}
 
Example 7
Source File: CustomCommentGenerator.java    From BlogManagePlatform with Apache License 2.0 5 votes vote down vote up
private void resolveColumn(Field field, IntrospectedTable table, IntrospectedColumn column) {
	String columnAnnotation = "@Column(name = \"" + column.getActualColumnName() + "\"";
	String typeName = field.getType().getShortName();
	if (typeName.equals("String") || typeName.equals("BigDecimal")) {
		if (column.getLength() != 0) {
			columnAnnotation = columnAnnotation + ", length = " + column.getLength();
		}
	}
	if (column.getScale() != 0) {
		columnAnnotation = columnAnnotation + ", scale = " + column.getScale();
	}
	columnAnnotation = columnAnnotation + ")";
	field.addAnnotation(columnAnnotation);
}
 
Example 8
Source File: CustomCommentGenerator.java    From BlogManagePlatform with Apache License 2.0 5 votes vote down vote up
private void resolveApiModelProperty(Field field, IntrospectedTable table, IntrospectedColumn column) {
	String remark = column.getRemarks();
	String apiModelProperty;
	//swagger插件处理defaultValue
	//      String defaultValue = column.getDefaultValue();
	//		if (EmptyUtil.no(defaultValue)) {
	//			apiModelProperty = "@ApiModelProperty(value = \"" + remark + "\"" + ", example = \"" + defaultValue + "\")";
	//		} else {
	//			apiModelProperty = "@ApiModelProperty(\"" + remark + "\")";
	//		}
	apiModelProperty = "@ApiModelProperty(\"" + remark + "\")";
	field.addAnnotation(apiModelProperty);
}
 
Example 9
Source File: CustomCommentGenerator.java    From BlogManagePlatform with Apache License 2.0 5 votes vote down vote up
private void resolveLength(Field field, IntrospectedTable table, IntrospectedColumn column) {
	if (field.getType().getShortName().equals("String")) {
		if (column.getLength() != 0) {
			String length = "@Length(max = " + column.getLength() + ")";
			field.addAnnotation(length);
		}
	}
}
 
Example 10
Source File: PluginUtil.java    From maven-archetype with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * 	添加属性域
 * 
 * @param annotation
 * @param classType
 * @param fieldName
 * @return
 */
public static Field getField (String annotation,FullyQualifiedJavaType classType, String fieldName){
	Field field = new Field();
	field.setVisibility(JavaVisibility.PRIVATE);
	field.addAnnotation(annotation);
	field.setType(classType);
	field.setName(fieldName);
       return field;
}
 
Example 11
Source File: HySwaggerMapperPlugin.java    From jvue-admin with MIT License 4 votes vote down vote up
private void swaggerApiAnnotation(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
	List<String> annotations = topLevelClass.getAnnotations();
	Iterator i$ = null;
	if (this.swaggerApiEnabled) {
		String apiModel = "io.swagger.annotations.ApiModel";
		String apiModelProperty = "io.swagger.annotations.ApiModelProperty";
		topLevelClass.addImportedType(apiModel);
		topLevelClass.addImportedType(apiModelProperty);
		String remarks = introspectedTable.getRemarks();
		if (StringUtil.isEmpty(remarks)) {
			remarks = "";
		}else{
			remarks = remarks.replaceAll("\n", " ");
		}
		if (remarks.endsWith("表")) {
			remarks = remarks.substring(0, remarks.length() - 1) + "对象";
		}
		remarks = topLevelClass.getType().getShortName() + "(" + remarks + ")";
		topLevelClass.addAnnotation("@ApiModel(\"" + remarks + "\")");
		List<Field> fields = topLevelClass.getFields();
		for (i$ = fields.iterator(); i$.hasNext();) {
			Field field = (Field) i$.next();
			List<IntrospectedColumn> allColumns = introspectedTable.getAllColumns();
			for (IntrospectedColumn introspectedColumn : allColumns) {
				if (field.getName().equals(introspectedColumn.getJavaProperty())) {
					String remark = introspectedColumn.getRemarks();
					if(StringUtil.isEmpty(remark)){
						remark = "";
					}else{
						remark = remark.replaceAll("\n", " ");
					}
					field.addAnnotation("@ApiModelProperty(value =\"" + remark + "\",required = false)");
                    if (!introspectedTable.getPrimaryKeyColumns().contains(introspectedColumn)) {
                        if ("version".equalsIgnoreCase(introspectedColumn.getJavaProperty())) {
                            // 处理乐观锁 2018-04-25
                            topLevelClass.addImportedType(new FullyQualifiedJavaType("tk.mybatis.mapper.annotation.Version"));
                            field.addAnnotation("@Version");
                        }
                    }
				}
	        }
		}
	}
}