|
| 1 | +## ApiBoot Mybatis Enhance Codegen |
| 2 | + |
| 3 | +`Mybatis Enhance Codegen`是一款`maven plugin`插件,在项目编译时运行,可把控`是否执行生成逻辑`、可根据自己的需求`过滤表名`生成,表名根据`like`语法匹配,完美搭配`Mybatis Enhance`使用,可自动生成`数据实体`、`动态查询实体`,不再为实体类映射表信息字段而犯愁、浪费个人精力。 |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +### 添加插件 |
| 8 | + |
| 9 | +```xml |
| 10 | +<build> |
| 11 | + <plugins> |
| 12 | + //... |
| 13 | + <plugin> |
| 14 | + <groupId>org.minbox.framework</groupId> |
| 15 | + <artifactId>api-boot-mybatis-enhance-maven-codegen</artifactId> |
| 16 | + <version>2.0.7.RC1</version> |
| 17 | + <dependencies> |
| 18 | + <!--数据驱动依赖--> |
| 19 | + <dependency> |
| 20 | + <groupId>mysql</groupId> |
| 21 | + <artifactId>mysql-connector-java</artifactId> |
| 22 | + <version>5.1.47</version> |
| 23 | + </dependency> |
| 24 | + </dependencies> |
| 25 | + <executions> |
| 26 | + <execution> |
| 27 | + <goals> |
| 28 | + <goal>generator</goal> |
| 29 | + </goals> |
| 30 | + </execution> |
| 31 | + </executions> |
| 32 | + <configuration> |
| 33 | + <execute>true</execute> |
| 34 | + <dbName>knowledge</dbName> |
| 35 | + <dbUrl>jdbc:mysql://localhost:3306</dbUrl> |
| 36 | + <dbUserName>root</dbUserName> |
| 37 | + <dbPassword>123456</dbPassword> |
| 38 | + <packageName>org.minbox.framework.api.boot.sample</packageName> |
| 39 | + <tableNamePattern>kl%</tableNamePattern> |
| 40 | + </configuration> |
| 41 | + </plugin> |
| 42 | + //... |
| 43 | + </plugins> |
| 44 | +</build> |
| 45 | +``` |
| 46 | + |
| 47 | +`Codegen`在运行时,需要`数据库驱动`的支持,我本机使用的是`MySQL`,因为在上面我添加了相关的依赖。 |
| 48 | + |
| 49 | +> 注意:`Codegen`内部使用`Code-Builder`的表信息获取的模块,MySQL的驱动默认只能使用5.x版本,不可以使用8.x。 |
| 50 | +
|
| 51 | +### 相关配置参数 |
| 52 | + |
| 53 | +| 参数名 | 默认值 | 描述 | |
| 54 | +| ------------------ | ------ | ---------------------------------------------------------- | |
| 55 | +| `execute` | false | `Codegen`是否执行 | |
| 56 | +| `dbName` | | 数据库名称 | |
| 57 | +| `dbUrl` | | 数据库连接路径(排除数据库名称) | |
| 58 | +| `dbUserName` | | 连接数据库用户名 | |
| 59 | +| `dbPassword` | | 连接数据库密码 | |
| 60 | +| `packageName` | | 生成后实体类的package | |
| 61 | +| `tableNamePattern` | % | 表名过滤表达式,向like语法一样使用,默认匹配数据库内全部表 | |
| 62 | + |
| 63 | +> 在上面配置中,排除有默认值的配置,其他都必须进行声明配置。 |
| 64 | +
|
| 65 | +### 执行生成 |
| 66 | + |
| 67 | +`Codegen`是在编译项目时执行,编译项目我们可以通过如下方式执行: |
| 68 | + |
| 69 | +1. 在项目根目录执行`mvn compile` |
| 70 | +2. 通过IDEA工具自带的`Maven->Lifecycle->compile`窗口双击进行编译项目 |
| 71 | +3. 通过IDEA工具自带的`Maven->Plugins->api-boot-mybatis-enhancecodegen:generator`窗口双击进行执行`Codegen` |
| 72 | + |
| 73 | +编译过程中,控制台会进行输出自动生成表的日志信息,如下所示: |
| 74 | + |
| 75 | +```sh |
| 76 | +...... |
| 77 | +[INFO] Execution table: 【kl_article_info】 - 文章信息表 entity creation. |
| 78 | +...... |
| 79 | +``` |
| 80 | + |
| 81 | +`Codegen`会把根据`tableNamePattern`查询到的表名进行输出,并且每个表会自动执行`实体类`、`动态查询实体`创建。 |
| 82 | + |
| 83 | +### 生成的实体类 |
| 84 | + |
| 85 | +**实体类命名** |
| 86 | + |
| 87 | +生成的实体类的命名规则是表名`驼峰`后的格式,示例如下所示: |
| 88 | + |
| 89 | +```java |
| 90 | +package org.minbox.framework.api.boot.sample; |
| 91 | + |
| 92 | +import com.gitee.hengboy.mybatis.enhance.common.annotation.Column; |
| 93 | +import com.gitee.hengboy.mybatis.enhance.common.annotation.Id; |
| 94 | +import com.gitee.hengboy.mybatis.enhance.common.annotation.Table; |
| 95 | +import com.gitee.hengboy.mybatis.enhance.common.enums.KeyGeneratorTypeEnum; |
| 96 | +import lombok.Data; |
| 97 | + |
| 98 | +import java.sql.Timestamp; |
| 99 | + |
| 100 | +/** |
| 101 | + * 文章信息表 |
| 102 | + * @author ApiBoot Mybatis Enhance Codegen |
| 103 | + */ |
| 104 | +@Data |
| 105 | +@Table(name = "kl_article_info") |
| 106 | +public class KlArticleInfo { |
| 107 | + |
| 108 | + /** |
| 109 | + * 主键自增 |
| 110 | + */ |
| 111 | + @Id(generatorType = KeyGeneratorTypeEnum.UUID) |
| 112 | + @Column(name = "AI_ID") |
| 113 | + private String aiId; |
| 114 | + /** |
| 115 | + * 文章所属用户 |
| 116 | + */ |
| 117 | + @Column(name = "AI_USER_ID") |
| 118 | + private String aiUserId; |
| 119 | + /** |
| 120 | + * 文章标题 |
| 121 | + */ |
| 122 | + @Column(name = "AI_TITLE") |
| 123 | + private String aiTitle; |
| 124 | + /** |
| 125 | + * 阅读量 |
| 126 | + */ |
| 127 | + @Column(name = "AI_READ_COUNT") |
| 128 | + private Integer aiReadCount; |
| 129 | + /** |
| 130 | + * 喜欢数量 |
| 131 | + */ |
| 132 | + @Column(name = "AI_LIKE_COUNT") |
| 133 | + private Integer aiLikeCount; |
| 134 | + /** |
| 135 | + * 评论数量 |
| 136 | + */ |
| 137 | + @Column(name = "AI_COMMENT_COUNT") |
| 138 | + private Integer aiCommentCount; |
| 139 | + /** |
| 140 | + * 分享数量 |
| 141 | + */ |
| 142 | + @Column(name = "AI_SHARE_COUNT") |
| 143 | + private Integer aiShareCount; |
| 144 | + /** |
| 145 | + * 文章内容 |
| 146 | + */ |
| 147 | + @Column(name = "AI_CONTENT") |
| 148 | + private String aiContent; |
| 149 | + /** |
| 150 | + * 是否为原创文章,Y:原创,N:转载 |
| 151 | + */ |
| 152 | + @Column(name = "AI_IS_ORIGINAL") |
| 153 | + private String aiIsOriginal; |
| 154 | + /** |
| 155 | + * 文章是否发布,Y:已发布,N:未发布 |
| 156 | + */ |
| 157 | + @Column(name = "AI_IS_RELEASE") |
| 158 | + private String aiIsRelease; |
| 159 | + /** |
| 160 | + * 是否热门,Y:热门,N:非热门 |
| 161 | + */ |
| 162 | + @Column(name = "AI_IS_HOT") |
| 163 | + private String aiIsHot; |
| 164 | + /** |
| 165 | + * 是否置顶,Y:置顶,N:普通 |
| 166 | + */ |
| 167 | + @Column(name = "AI_IS_TOP") |
| 168 | + private String aiIsTop; |
| 169 | + /** |
| 170 | + * 是否推荐,Y:推荐,N:不推荐 |
| 171 | + */ |
| 172 | + @Column(name = "AI_IS_RECOMMEND") |
| 173 | + private String aiIsRecommend; |
| 174 | + /** |
| 175 | + * 是否为markdown语法文章 |
| 176 | + */ |
| 177 | + @Column(name = "AI_IS_MARKDOWN") |
| 178 | + private String aiIsMarkdown; |
| 179 | + /** |
| 180 | + * 发布时间 |
| 181 | + */ |
| 182 | + @Column(name = "AI_RELEASE_TIME") |
| 183 | + private Timestamp aiReleaseTime; |
| 184 | + /** |
| 185 | + * 文章状态,O:正常,D:已删除 |
| 186 | + */ |
| 187 | + @Column(name = "AI_STATUS") |
| 188 | + private String aiStatus; |
| 189 | + /** |
| 190 | + * 备注信息 |
| 191 | + */ |
| 192 | + @Column(name = "AI_MARK") |
| 193 | + private String aiMark; |
| 194 | + /** |
| 195 | + * 文章创建时间 |
| 196 | + */ |
| 197 | + @Column(name = "AI_CREATE_TIME") |
| 198 | + private Timestamp aiCreateTime; |
| 199 | +} |
| 200 | +``` |
| 201 | + |
| 202 | +`@Id`的主键生成策略,会根据表内主键是否定义了自增来进行判断,如果是自增使用`KeyGeneratorTypeEnum.AUTO`,如果不是则使用`KeyGeneratorTypeEnum.UUID`,如果你项目内是自定义的主键,可以进行修改为`KeyGeneratorTypeEnum.DIY`。 |
| 203 | + |
| 204 | +### 生成的动态查询实体 |
| 205 | + |
| 206 | +**动态查询实体命名** |
| 207 | + |
| 208 | +动态查询实体的命名规则同样是`驼峰`,不过有个前缀为`D`,上面实体类名称为`KlArticleInfo`对应动态查询实体为`DKlArticleInfo`,生成示例如下所示: |
| 209 | + |
| 210 | +```java |
| 211 | +package org.minbox.framework.api.boot.sample; |
| 212 | + |
| 213 | +import com.gitee.hengboy.mybatis.enhance.dsl.expression.ColumnExpression; |
| 214 | +import com.gitee.hengboy.mybatis.enhance.dsl.expression.TableExpression; |
| 215 | + |
| 216 | +/** |
| 217 | + * 文章信息表 |
| 218 | + * @author ApiBoot Mybatis Enhance Codegen |
| 219 | + */ |
| 220 | +public class DKlArticleInfo extends TableExpression<KlArticleInfo> { |
| 221 | + |
| 222 | + public DKlArticleInfo(String root) { |
| 223 | + super(root); |
| 224 | + } |
| 225 | + |
| 226 | + public static DKlArticleInfo DSL() { |
| 227 | + return new DKlArticleInfo("kl_article_info"); |
| 228 | + } |
| 229 | + |
| 230 | + /** |
| 231 | + * 主键自增 |
| 232 | + */ |
| 233 | + public ColumnExpression aiId = new ColumnExpression("AI_ID", this); |
| 234 | + /** |
| 235 | + * 文章所属用户 |
| 236 | + */ |
| 237 | + public ColumnExpression aiUserId = new ColumnExpression("AI_USER_ID", this); |
| 238 | + /** |
| 239 | + * 文章标题 |
| 240 | + */ |
| 241 | + public ColumnExpression aiTitle = new ColumnExpression("AI_TITLE", this); |
| 242 | + /** |
| 243 | + * 阅读量 |
| 244 | + */ |
| 245 | + public ColumnExpression aiReadCount = new ColumnExpression("AI_READ_COUNT", this); |
| 246 | + /** |
| 247 | + * 喜欢数量 |
| 248 | + */ |
| 249 | + public ColumnExpression aiLikeCount = new ColumnExpression("AI_LIKE_COUNT", this); |
| 250 | + /** |
| 251 | + * 评论数量 |
| 252 | + */ |
| 253 | + public ColumnExpression aiCommentCount = new ColumnExpression("AI_COMMENT_COUNT", this); |
| 254 | + /** |
| 255 | + * 分享数量 |
| 256 | + */ |
| 257 | + public ColumnExpression aiShareCount = new ColumnExpression("AI_SHARE_COUNT", this); |
| 258 | + /** |
| 259 | + * 文章内容 |
| 260 | + */ |
| 261 | + public ColumnExpression aiContent = new ColumnExpression("AI_CONTENT", this); |
| 262 | + /** |
| 263 | + * 是否为原创文章,Y:原创,N:转载 |
| 264 | + */ |
| 265 | + public ColumnExpression aiIsOriginal = new ColumnExpression("AI_IS_ORIGINAL", this); |
| 266 | + /** |
| 267 | + * 文章是否发布,Y:已发布,N:未发布 |
| 268 | + */ |
| 269 | + public ColumnExpression aiIsRelease = new ColumnExpression("AI_IS_RELEASE", this); |
| 270 | + /** |
| 271 | + * 是否热门,Y:热门,N:非热门 |
| 272 | + */ |
| 273 | + public ColumnExpression aiIsHot = new ColumnExpression("AI_IS_HOT", this); |
| 274 | + /** |
| 275 | + * 是否置顶,Y:置顶,N:普通 |
| 276 | + */ |
| 277 | + public ColumnExpression aiIsTop = new ColumnExpression("AI_IS_TOP", this); |
| 278 | + /** |
| 279 | + * 是否推荐,Y:推荐,N:不推荐 |
| 280 | + */ |
| 281 | + public ColumnExpression aiIsRecommend = new ColumnExpression("AI_IS_RECOMMEND", this); |
| 282 | + /** |
| 283 | + * 是否为markdown语法文章 |
| 284 | + */ |
| 285 | + public ColumnExpression aiIsMarkdown = new ColumnExpression("AI_IS_MARKDOWN", this); |
| 286 | + /** |
| 287 | + * 发布时间 |
| 288 | + */ |
| 289 | + public ColumnExpression aiReleaseTime = new ColumnExpression("AI_RELEASE_TIME", this); |
| 290 | + /** |
| 291 | + * 文章状态,O:正常,D:已删除 |
| 292 | + */ |
| 293 | + public ColumnExpression aiStatus = new ColumnExpression("AI_STATUS", this); |
| 294 | + /** |
| 295 | + * 备注信息 |
| 296 | + */ |
| 297 | + public ColumnExpression aiMark = new ColumnExpression("AI_MARK", this); |
| 298 | + /** |
| 299 | + * 文章创建时间 |
| 300 | + */ |
| 301 | + public ColumnExpression aiCreateTime = new ColumnExpression("AI_CREATE_TIME", this); |
| 302 | + @Override |
| 303 | + public ColumnExpression[] getColumns() { |
| 304 | + return new ColumnExpression[]{aiId, aiUserId, aiTitle, aiReadCount, aiLikeCount, aiCommentCount, aiShareCount, aiContent, aiIsOriginal, aiIsRelease, aiIsHot, aiIsTop, aiIsRecommend, aiIsMarkdown, aiReleaseTime, aiStatus, aiMark, aiCreateTime}; |
| 305 | + } |
| 306 | + |
| 307 | +} |
| 308 | +``` |
| 309 | + |
| 310 | + |
| 311 | + |
| 312 | +### 实体生成后的位置在哪? |
| 313 | + |
| 314 | +`Codegen`所生成的所有实体都位于项目根目录下的`target/generated-sources/java`下,可以自行复制到业务目录、或者直接使用。 |
| 315 | + |
| 316 | +### 使用动态查询实体示例 |
| 317 | + |
| 318 | +```java |
| 319 | +/** |
| 320 | + * Mybatis Enhance Dsl Factory |
| 321 | + */ |
| 322 | +@Autowired |
| 323 | +private EnhanceDslFactory dslFactory; |
| 324 | + |
| 325 | +/** |
| 326 | + * 根据文章编号查询示例 |
| 327 | + * |
| 328 | + * @param articleId 文章编号 |
| 329 | + * @return |
| 330 | + */ |
| 331 | +public KlArticleInfo selectById(String articleId) { |
| 332 | + DKlArticleInfo dKlArticleInfo = DKlArticleInfo.DSL(); |
| 333 | + return dslFactory.createSearchable() |
| 334 | + .selectFrom(dKlArticleInfo) |
| 335 | + // 文章主键 |
| 336 | + .where(dKlArticleInfo.aiId.eq(articleId)) |
| 337 | + // and 状态正常 |
| 338 | + .and(dKlArticleInfo.aiStatus.eq("O")) |
| 339 | + .resultType(KlArticleInfo.class) |
| 340 | + .fetchOne(); |
| 341 | +} |
| 342 | +``` |
| 343 | + |
0 commit comments