001/* 002 * Copyright (c) 2022-2025, Mybatis-Flex (fuhai999@gmail.com). 003 * <p> 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * <p> 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * <p> 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.mybatisflex.processor.builder; 018 019import com.mybatisflex.processor.entity.ColumnInfo; 020import com.mybatisflex.processor.entity.TableInfo; 021import com.mybatisflex.processor.util.StrUtil; 022 023import java.util.Collection; 024import java.util.List; 025import java.util.StringJoiner; 026 027/** 028 * 文件内容构建。 029 * 030 * @author 王帅 031 * @since 2023-06-23 032 */ 033@SuppressWarnings({"squid:S107", "squid:S1192"}) 034public class ContentBuilder { 035 036 private ContentBuilder() { 037 } 038 039 /** 040 * 构建 Mapper 文件内容。 041 */ 042 public static String buildMapper(TableInfo tableInfo, String mappersPackage, String mapperClassName, 043 String baseMapperClass, boolean mapperAnnotationEnable) { 044 String entityClass = tableInfo.getEntityName(); 045 StringBuilder content = new StringBuilder("package "); 046 content.append(mappersPackage).append(";\n\n"); 047 content.append("import ").append(baseMapperClass).append(";\n"); 048 content.append("import ").append(entityClass).append(";\n\n"); 049 if (mapperAnnotationEnable) { 050 content.append("import org.apache.ibatis.annotations.Mapper;\n\n"); 051 content.append("@Mapper\n"); 052 } 053 String realEntityClassName = StrUtil.getClassName(entityClass); 054 String baseMapperClassName = StrUtil.getClassName(baseMapperClass); 055 content.append("public interface ").append(mapperClassName).append(" extends ").append(baseMapperClassName).append("<").append(realEntityClassName).append("> {\n}"); 056 return content.toString(); 057 } 058 059 /** 060 * 构建 TableDef 文件内容。 061 */ 062 public static String buildTableDef(TableInfo tableInfo, boolean allInTablesEnable, 063 String tableDefPackage, String tableDefClassName, 064 String tableDefPropertiesNameStyle, String tableDefInstanceSuffix, 065 Collection<ColumnInfo> columnInfos, List<String> defaultColumns) { 066 StringBuilder content = new StringBuilder("package "); 067 content.append(tableDefPackage).append(";\n\n"); 068 content.append("import com.mybatisflex.core.query.QueryColumn;\n"); 069 content.append("import com.mybatisflex.core.table.TableDef;\n\n"); 070 content.append("// Auto generate by mybatis-flex, do not modify it.\n"); 071 content.append("public class ").append(tableDefClassName).append(" extends TableDef {\n\n"); 072 073 //TableDef 类的属性名称 074 String tableDefPropertyName = null; 075 if (!allInTablesEnable) { 076 String entityComment = tableInfo.getEntityComment(); 077 if (!StrUtil.isBlank(entityComment)) { 078 content.append(" /**\n") 079 .append(" * ").append(entityComment.trim()).append("\n") 080 .append(" */\n"); 081 } 082 tableDefPropertyName = StrUtil.buildFieldName(tableInfo.getEntitySimpleName().concat(tableDefInstanceSuffix != null ? tableDefInstanceSuffix.trim() : ""), tableDefPropertiesNameStyle); 083 content.append(" public static final ").append(tableDefClassName).append(' ').append(tableDefPropertyName) 084 .append(" = new ").append(tableDefClassName).append("();\n\n"); 085 } 086 087 088 String finalTableDefPropertyName = tableDefPropertyName; 089 columnInfos.forEach(columnInfo -> { 090 String comment = columnInfo.getComment(); 091 if (!StrUtil.isBlank(comment)) { 092 content.append(" /**\n") 093 .append(" * ").append(comment.trim()).append("\n") 094 .append(" */\n"); 095 } 096 097 // QueryColumn 属性定义的名称 098 String columnPropertyName = StrUtil.buildFieldName(columnInfo.getProperty(), tableDefPropertiesNameStyle); 099 100 //当字段名称和表名一样时,自动为字段添加一个小尾巴 "_",例如 account_ 101 if (columnPropertyName.equals(finalTableDefPropertyName)) { 102 columnPropertyName = columnPropertyName + "_"; 103 } 104 content.append(" public final QueryColumn ") 105 .append(columnPropertyName) 106 .append(" = new QueryColumn(this, \"") 107 .append(columnInfo.getColumn()).append("\""); 108 if (columnInfo.getAlias() != null && columnInfo.getAlias().length > 0) { 109 content.append(", \"").append(columnInfo.getAlias()[0]).append("\""); 110 } 111 content.append(");\n\n"); 112 }); 113 content.append(" /**\n") 114 .append(" * 所有字段。\n") 115 .append(" */\n"); 116 content.append(" public final QueryColumn ").append(StrUtil.buildFieldName("allColumns", tableDefPropertiesNameStyle)).append(" = new QueryColumn(this, \"*\");\n"); 117 StringJoiner defaultColumnJoiner = new StringJoiner(", "); 118 columnInfos.forEach(columnInfo -> { 119 if (defaultColumns.contains(columnInfo.getColumn())) { 120 String columnPropertyName = StrUtil.buildFieldName(columnInfo.getProperty(), tableDefPropertiesNameStyle); 121 if (columnPropertyName.equals(finalTableDefPropertyName)) { 122 columnPropertyName = columnPropertyName + "_"; 123 } 124 defaultColumnJoiner.add(columnPropertyName); 125 } 126 }); 127 content.append("\n /**\n") 128 .append(" * 默认字段,不包含逻辑删除或者 large 等字段。\n") 129 .append(" */\n"); 130 content.append(" public final QueryColumn[] ").append(StrUtil.buildFieldName("defaultColumns", tableDefPropertiesNameStyle)).append(" = new QueryColumn[]{").append(defaultColumnJoiner).append("};\n\n"); 131 String schema = !StrUtil.isBlank(tableInfo.getSchema()) 132 ? tableInfo.getSchema() 133 : ""; 134 String tableName = !StrUtil.isBlank(tableInfo.getTableName()) 135 ? tableInfo.getTableName() 136 : StrUtil.firstCharToLowerCase(tableInfo.getEntitySimpleName()); 137 content.append(" public ").append(tableDefClassName).append("() {\n") 138 .append(" super").append("(\"").append(schema).append("\", \"").append(tableName).append("\");\n") 139 .append(" }\n\n"); 140 141 content.append(" private ").append(tableDefClassName).append("(String schema, String name, String alisa) {\n") 142 .append(" super(schema, name, alisa);\n") 143 .append(" }\n\n"); 144 145 content.append(" public ").append(tableDefClassName).append(" as(String alias) {\n") 146 .append(" String key = getNameWithSchema() + \".\" + alias;\n") 147 .append(" return getCache(key, k -> new ").append(tableDefClassName).append("(\"").append(schema).append("\", \"").append(tableName).append("\", alias));\n") 148 .append(" }\n\n}\n"); 149 return content.toString(); 150 } 151 152 /** 153 * 构建 Tables 文件内容。 154 */ 155 public static String buildTables(StringBuilder importBuilder, StringBuilder fieldBuilder, 156 String tablesPackage, String tablesClassName) { 157 return "package " + tablesPackage + ";\n\n" + 158 importBuilder.toString() + 159 "\n// Auto generate by mybatis-flex, do not modify it.\n" + 160 "public class " + tablesClassName + " {\n\n" + 161 " private " + tablesClassName + "() {\n" + 162 " }\n\n" + 163 fieldBuilder.toString() + 164 "\n}\n"; 165 } 166 167 /** 168 * 构建 Tables 文件常量属性。 169 */ 170 public static void buildTablesField(StringBuilder importBuilder, StringBuilder fieldBuilder, TableInfo tableInfo, 171 String tableDefClassSuffix, String tableDefPropertiesNameStyle, String tableDefInstanceSuffix, String tableDefPackage) { 172 String tableDefClassName = tableInfo.getEntitySimpleName().concat(tableDefClassSuffix); 173 importBuilder.append("import ").append(tableDefPackage).append('.').append(tableDefClassName).append(";\n"); 174 String entityComment = tableInfo.getEntityComment(); 175 if (!StrUtil.isBlank(entityComment)) { 176 fieldBuilder.append(" /**\n") 177 .append(" * ").append(entityComment).append("\n") 178 .append(" */\n"); 179 } 180 fieldBuilder.append(" public static final ").append(tableDefClassName).append(' ') 181 .append(StrUtil.buildFieldName(tableInfo.getEntitySimpleName().concat(tableDefInstanceSuffix != null ? tableDefInstanceSuffix.trim() : ""), tableDefPropertiesNameStyle)) 182 .append(" = new ").append(tableDefClassName).append("();\n"); 183 } 184 185}