|
| 1 | +/* |
| 2 | + * Copyright [2019] [恒宇少年 - 于起宇] |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +package org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen; |
| 19 | + |
| 20 | +import com.gitee.hengboy.builder.common.CodeBuilderProperties; |
| 21 | +import com.gitee.hengboy.builder.common.enums.DbTypeEnum; |
| 22 | +import com.gitee.hengboy.builder.core.database.DataBase; |
| 23 | +import com.gitee.hengboy.builder.core.database.DataBaseFactory; |
| 24 | +import com.gitee.hengboy.builder.core.database.model.Table; |
| 25 | +import org.apache.maven.plugin.AbstractMojo; |
| 26 | +import org.apache.maven.plugin.MojoExecutionException; |
| 27 | +import org.apache.maven.plugin.MojoFailureException; |
| 28 | +import org.apache.maven.plugins.annotations.Execute; |
| 29 | +import org.apache.maven.plugins.annotations.LifecyclePhase; |
| 30 | +import org.apache.maven.plugins.annotations.Mojo; |
| 31 | +import org.apache.maven.plugins.annotations.Parameter; |
| 32 | +import org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen.builder.ClassBuilder; |
| 33 | +import org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen.builder.ClassBuilderFactory; |
| 34 | +import org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen.builder.impl.DynamicEntityClassBuilder; |
| 35 | +import org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen.builder.impl.EntityClassBuilder; |
| 36 | +import org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen.builder.wrapper.ClassBuilderWrapper; |
| 37 | +import org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen.tools.CamelTools; |
| 38 | +import org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen.writer.JavaClassWriter; |
| 39 | +import org.springframework.util.ObjectUtils; |
| 40 | + |
| 41 | +import java.io.File; |
| 42 | +import java.util.Arrays; |
| 43 | +import java.util.List; |
| 44 | + |
| 45 | +/** |
| 46 | + * ApiBoot Mybatis Enhance Codegen Maven Plugin |
| 47 | + * 1. Generating data entities |
| 48 | + * 2. Generating dynamic query entities |
| 49 | + * 3. Generate Mapper files |
| 50 | + * |
| 51 | + * @author:恒宇少年 - 于起宇 |
| 52 | + * <p> |
| 53 | + * DateTime:2019-04-29 16:28 |
| 54 | + * Blog:http://blog.yuqiyu.com |
| 55 | + * WebSite:http://www.jianshu.com/u/092df3f77bca |
| 56 | + * Gitee:https://gitee.com/hengboy |
| 57 | + * GitHub:https://github.com/hengboy |
| 58 | + */ |
| 59 | +@Mojo(name = "generator", defaultPhase = LifecyclePhase.COMPILE) |
| 60 | +@Execute(phase = LifecyclePhase.COMPILE) |
| 61 | +public class ApiBootMybatisEnhanceCodegen extends AbstractMojo { |
| 62 | + /** |
| 63 | + * file suffix |
| 64 | + */ |
| 65 | + private static final String FILE_SUFFIX = ".java"; |
| 66 | + /** |
| 67 | + * Whether to execute automatically |
| 68 | + * Default not to execute |
| 69 | + */ |
| 70 | + @Parameter(defaultValue = "false", required = true) |
| 71 | + private boolean execute; |
| 72 | + /** |
| 73 | + * database type |
| 74 | + */ |
| 75 | + @Parameter(defaultValue = "MySQL") |
| 76 | + private DbTypeEnum dbType; |
| 77 | + /** |
| 78 | + * database name |
| 79 | + */ |
| 80 | + @Parameter(required = true) |
| 81 | + private String dbName; |
| 82 | + /** |
| 83 | + * database url |
| 84 | + * for example:jdbc:mysql://xxx.xx.xx.xxx:3306 |
| 85 | + */ |
| 86 | + @Parameter(required = true) |
| 87 | + private String dbUrl; |
| 88 | + /** |
| 89 | + * database username |
| 90 | + */ |
| 91 | + @Parameter(required = true) |
| 92 | + private String dbUserName; |
| 93 | + /** |
| 94 | + * database password |
| 95 | + */ |
| 96 | + @Parameter(required = true) |
| 97 | + private String dbPassword; |
| 98 | + /** |
| 99 | + * database driver class name |
| 100 | + */ |
| 101 | + @Parameter |
| 102 | + private String dbDriverClassName; |
| 103 | + /** |
| 104 | + * table name pattern |
| 105 | + * Used to get a list of tables |
| 106 | + */ |
| 107 | + @Parameter(defaultValue = "%") |
| 108 | + private String tableNamePattern; |
| 109 | + /** |
| 110 | + * project base dir |
| 111 | + */ |
| 112 | + @Parameter(defaultValue = "${basedir}") |
| 113 | + private String projectBaseDir; |
| 114 | + |
| 115 | + /** |
| 116 | + * generator class target dir |
| 117 | + */ |
| 118 | + @Parameter(defaultValue = "target.generated-sources.java") |
| 119 | + private String targetDir; |
| 120 | + /** |
| 121 | + * java file package name |
| 122 | + */ |
| 123 | + @Parameter(required = true) |
| 124 | + private String packageName; |
| 125 | + |
| 126 | + @Override |
| 127 | + public void execute() throws MojoExecutionException, MojoFailureException { |
| 128 | + if (!execute) { |
| 129 | + getLog().warn("Automatic code generation is not turned on. If you need to generate entity classes, configure 【execute=true】"); |
| 130 | + return; |
| 131 | + } |
| 132 | + // code builder properties |
| 133 | + CodeBuilderProperties codeBuilderProperties = CodeBuilderProperties.builder().dbType(dbType).dbName(dbName).dbUrl(dbUrl).dbUserName(dbUserName).dbPassword(dbPassword).dbDriverClassName(dbDriverClassName).build(); |
| 134 | + |
| 135 | + // get database instance by DbTypeEnum |
| 136 | + DataBase dataBase = DataBaseFactory.newInstance(codeBuilderProperties); |
| 137 | + |
| 138 | + List<String> tableNames = getTableNames(dataBase); |
| 139 | + |
| 140 | + tableNames.stream().forEach(tableName -> { |
| 141 | + |
| 142 | + getLog().info("Execution table: 【" + tableName + "】 entity creation."); |
| 143 | + |
| 144 | + // get table |
| 145 | + Table table = dataBase.getTable(tableName); |
| 146 | + |
| 147 | + // execute the builders created |
| 148 | + // ClassBuilder implementation classes can be added |
| 149 | + Class<? extends ClassBuilder>[] builders = new Class[]{ |
| 150 | + EntityClassBuilder.class, |
| 151 | + DynamicEntityClassBuilder.class |
| 152 | + }; |
| 153 | + |
| 154 | + // Encapsulated objects needed to perform generation |
| 155 | + ClassBuilderWrapper wrapper = ClassBuilderWrapper.builder().packageName(packageName).tableCamelName(CamelTools.upper(tableName)).table(table).build(); |
| 156 | + |
| 157 | + // execute generator |
| 158 | + Arrays.stream(builders).forEach(builderClass -> { |
| 159 | + ClassBuilder builder = ClassBuilderFactory.newInstance(builderClass, wrapper); |
| 160 | + if (!ObjectUtils.isEmpty(builder)) { |
| 161 | + // setting class prefix |
| 162 | + wrapper.setTableCamelName(builder.getDefaultPrefix() + wrapper.getTableCamelName()); |
| 163 | + // class file path |
| 164 | + String classPath = getNewClassPath(wrapper.getTableCamelName()); |
| 165 | + // class content |
| 166 | + String classContent = builder.getClassContent(); |
| 167 | + // invoke content write |
| 168 | + JavaClassWriter.writeToJavaFile(classPath, classContent); |
| 169 | + } |
| 170 | + }); |
| 171 | + }); |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * get table name list |
| 176 | + * |
| 177 | + * @param dataBase data base instance |
| 178 | + * @return table name list |
| 179 | + */ |
| 180 | + private List<String> getTableNames(DataBase dataBase) { |
| 181 | + return dataBase.getTableNames(tableNamePattern); |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * get generator dir |
| 186 | + * |
| 187 | + * @return generator dir |
| 188 | + */ |
| 189 | + private String getGeneratorDir() { |
| 190 | + // java file base dir |
| 191 | + String baseDir = String.format("%s%s%s", projectBaseDir, File.separator, targetDir.replace(".", File.separator)); |
| 192 | + // package dir |
| 193 | + String packageDir = String.format("%s%s%s", baseDir, File.separator, packageName.replace(".", File.separator)); |
| 194 | + // create dirs |
| 195 | + File file = new File(packageDir); |
| 196 | + if (!file.exists()) { |
| 197 | + file.mkdirs(); |
| 198 | + } |
| 199 | + return packageDir; |
| 200 | + } |
| 201 | + |
| 202 | + /** |
| 203 | + * get generator class path |
| 204 | + * |
| 205 | + * @param entityClassName entity class name |
| 206 | + * @return class path |
| 207 | + */ |
| 208 | + private String getNewClassPath(String entityClassName) { |
| 209 | + return String.format("%s%s%s%s", getGeneratorDir(), File.separator, entityClassName, FILE_SUFFIX); |
| 210 | + } |
| 211 | +} |
0 commit comments