1 Star 0 Fork 39

hbyufan / code-builder

forked from 恒宇少年 / code-builder 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

欢迎使用代码生成器Code-Builder

更新记录

  • 1.0.5.RELEASE
    • 添加模板前缀参数filePrefix
    • 修改项目根地址排除target
    • 生成完成后输出本次执行信息

Maven Center 版本

Maven central

欢迎关注公众号

微信公众号 关注微信公众号,回复加群,获取交流群群号。

背景

本来code-builder是专门为MyBatis Enhance来编写的一块代码生成器

code-builder可以用来做什么?

code-builder是一款代码生成maven mojo插件,通过简单的配置就可以完成数据库内Table转换Entity或者其他实体类,想怎么生成完全根据你的个人业务逻辑,code-builder尽可能的完善的提供数据库内的一些定义的信息,让你更方便更灵活的来生成Java文件。

使用环境

  • Maven构建的项目
  • JDK 1.6以上版本

实现方式

是怎么获取到的数据库信息?

code-builder内部采用了java.sql.ConnectionMetaData元数据的方式来获取数据库内TableColumn等信息,MetaData是不局限于任何的数据库类型的,所以code-builder在基础设计上是可以在任何数据库类型中来完成它的生成任务的,不过初版本仅支持了MySQLMariaDB这两种数据库类型,在code-builder后期更新版本中会把主流的数据库进行添加。

生成模板选型

目前code-builder内部采用了freemarker模板来完成实体类的自动生成,模板由使用者来自定义编写,内部预留了使用其他模板的方式,如果你需要使用别的模板,如:Velocity,对应添加生成的实现业务逻辑即可。

怎么配置?

SpringBoot 方式配置

1.0.5.RELEASE版本添加了集成SpringBootstarter,依赖如下所示:

  • 使用Maven构建工具时,复制下面的内容到pom.xml配置文件内
<dependency>
    <groupId>com.gitee.hengboy</groupId>
    <artifactId>code-builder-spring-boot-starter</artifactId>
    <version>1.0.5.RELEASE</version>
</dependency>
  • 如果你是用的Gradle构建工具,那么复制下面的内容到你的build.gradle
compile group: 'com.gitee.hengboy', name: 'code-builder-spring-boot-starter', version: '1.0.5.RELEASE'

那么我们在application.yml或者application.properties配置文件内该怎么配置相关的参数呢?

hengboy:
  code:
    builder:
      execute: true
      configuration:
        package-prefix: com.code.builder.sample.codebuildersample
        templates:
          -
            name: entity.ftl
            packageName: model
            fileSuffix: Entity
          -
            name: service.ftl
            packageName: service
            fileSuffix: Service
          -
            name: controller.ftl
            packageName: controller
            fileSuffix: Controller
      generator-by-pattern: '%app_user_info%'
      db-type: mysql
      engine-type-enum: freemarker
      builder-dir: classes.templates.builder
      target-dir: generated-sources.java
      tables:
        - app_shop_type
        - app_user_exchange_good
      ignore-class-prefix: App

每个参数的具体介绍请往下看。

Maven Plugin 方式配置

由于code-builderMaven mojo插件的形式创建的,所以我们只需要在项目的pom.xml文件内添加plugin插件配置,如下所示:

<plugin>
    <groupId>com.gitee.hengboy</groupId>
    <artifactId>code-builder-maven-plugin</artifactId>
    <version>1.0.5.RELEASE</version>
    <executions>
        <execution>
            <goals>
                <goal>generator</goal>
            </goals>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>
    </dependencies>
    <configuration>
        <execute>true</execute>
        <dbType>MySQL</dbType>
        <dbDriverClassName>com.mysql.jdbc.Driver</dbDriverClassName>
        <dbName>xxxx</dbName>
        <dbUserName>xxxx</dbUserName>
        <dbPassword>xxxxx</dbPassword>
        <dbUrl>jdbc:mysql://xxx.xx.xx.xx:3306</dbUrl>
        <tables>
             <table>app_shop_type</table>
             <table>app_user_exchange_good</table>
         </tables>
         <engineType>FREEMARKER</engineType>
         <generatorByPattern>%app_user_info%</generatorByPattern>
         <ignoreClassPrefix>App</ignoreClassPrefix>
         <builderDir>classes.templates.builder</builderDir>
         <builder>
                <packagePrefix>com.code.builder.sample</packagePrefix>
		         <templates>
		              <template>
		                  <name>entity.ftl</name>
		                  <packageName>model</packageName>
		              </template>
		              <template>
		                  <name>service.ftl</name>
		                  <packageName>service</packageName>
		                  <fileSuffix>Service</fileSuffix>
		              </template>
		              <template>
		                  <name>mapper.ftl</name>
		                  <packageName>mapper</packageName>
		                  <fileSuffix>Mapper</fileSuffix>
		               </template>
		         </templates>
         </builder>
    </configuration>
</plugin>

数据库驱动依赖添加

code-builder不局限你使用的数据库类型,所以在生成时需要使用者添加对应数据类型的依赖,如上面的配置中则是添加了MySQL数据库的依赖

.....
<dependencies>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.46</version>
    </dependency>
</dependencies>
.....

生成的控制开关

并不是每一次的编译或者打包时都需要生成对应的实体,针对这种情况code-builder添加了execute参数来控制开启与关闭。

  • true:开启自动生成
  • false:关闭自动生成

数据库类型配置

执行自动生成前需要配置数据库的相关配置信息

  • dbType:数据库类型,默认使用MySQL数据库类型。
  • dbDriverClassName:数据库驱动类名,根据不用的数据库类型配置不同的驱动类名,默认根据dbType使用内部定义的类名,如需自定义可以设置。(仅maven-plugin使用)

数据库基本信息配置 (仅maven-plugin使用)

  • dbName:数据库名称
  • dbUserName:数据库用户名
  • dbPassword:数据库密码
  • dbUrl:数据库连接路径,连接路径不需要填写数据库名,正确示例如:jdbc:mysql://localhost:3306

生成表名符合规则的表

根据表达式来创建表,表达式与模糊查询语句表达式一般无二,配置generatorByPattern参数并设置对应的表达式就可以根据表达式来匹配出参与生成的Table列表。

  • 指定前缀匹配
<generatorByPattern>app_order%</generatorByPattern>

示例:将会匹配出app_order_infoapp_order_record等表。

  • 指定后缀匹配
<generatorByPattern>%order</generatorByPattern>

示例:将会匹配出app_good_orderapp_exchange_order等表。

  • 包含匹配
<generatorByPattern>%order%</generatorByPattern>

示例:将会匹配出app_order_infoapp_good_order等表。

生成指定表

code-builder支持指定单个或者多个表来生成,只需要配置tables参数即可,如下所示:

<tables>
    <table>app_shop_type</table>
    <table>app_user_exchange_good</table>
</tables>

上面的配置是本次生成仅操作app_shop_typeapp_user_exchange_good两张表。

注意:tables参数的优先级要高于generatorByPattern参数。

自定义builder所需模板路径

code-builder会自动去找classes/templates/builder下的模板,如果使用默认的freemarker模板来生成,那么模板所存放的位置为classes/templates/builder/freemarker。 如果你想自定义模板的路径可以设置builderDir的地址,在这里因为考虑到了不同操作系统的分隔符不一样(Windows系统分隔符\Linux以及O SX分隔符为/)所以这里采用.分隔符配置,code-builder会自动根据操作系统来转换路径,配置如下所示:

<builderDir>classes.code.builder</builderDir>

注意:freemarker文件夹不允许修改,只能修改code-builder加载模板的根路径。

排除生成实体后的前缀

数据库设计有时需要添加前缀,如:app_sys_等,实际生成实体后前缀则是并不想展示,那么配置参数ignoreClassPrefix就可以自动排除前缀,如下所示:

<ignoreClassPrefix>App</ignoreClassPrefix>

注意:由于替换生成后的类名称所以这里要准守驼峰命名规则首字母大写,一次只能配置一个替换前缀。

使用前AppUserInfoEntity,使用后UserInfoEntity

模板配置

使用templates标签配置自定义的模板列表,一次可以使用单个或者多个模板进行生成,如下配置:

<templates>
    <template>
        <name>entity.ftl</name>
        <packageName>entity</packageName>
        <fileSuffix>entity</fileSuffix>
    </template>
    <template>
        <name>service.ftl</name>
        <packageName>service</packageName>
        <fileSuffix>Service</fileSuffix>
    </template>
    <template>
        <name>mapper.ftl</name>
        <packageName>mapper</packageName>
        <fileSuffix>Mapper</fileSuffix>
    </template>
</templates>
  • namefreemarker目录下模板的名称,必填
  • packageName:生成该模板文件后的子包名称,非必填
  • fileSuffix:生成文件的后缀,如:配置后缀为Entity,则添加后缀后的文件名为UserInfoEntity,后缀首字母会自动根据驼峰转换成大写

内置参数

模板驱动数据模型内置了部分参数,code-builder准备的每一个参数都是在生成实体类时都可能会用到的。

Table参数

  • tableName 表名,数据类型:java.lang.String
  • remark 表备注信息,数据类型:java.lang.String
  • entityName 实体类名称,如:user_info转换为userInfo,数据类型:java.lang.String
  • columns 列列表,数据类型:java.util.List<Column>
  • primaryKeys 主键列表,数据类型:java.util.List<Column>
  • hasSqlDate 是否存在java.sql.Date类型,true:存在,false:不存在,数据类型:java.lang.Boolean
  • hasTimeStamp 是否存在java.sql.TimeStamp类型,true:存在,false:不存在,数据类型:java.lang.Boolean
  • hasBigDecimal 是否存在java.math.BigDecimal类型,true:存在,false:不存在,数据类型:java.lang.Boolean
使用方式

freemarker模板${table.xxx},如表名的使用为${table.tableName}

Column参数

  • columnName 列名,如:user_id,数据类型:java.lang.String
  • primaryKey 是否为主键,数据类型:java.lang.Booleantrue:主键,false:非主键
  • foreignKey 是否为外键,数据类型:java.lang.Booleantrue:外键,false:非外键
  • size 列长度,数据类型:java.lang.Integer
  • decimalDigits 小数点精度,数据类型:java.lang.Integer
  • nullable 列是否为空,数据类型:java.lang.Booleantrue:为空,false:非空
  • autoincrement 是否自增,数据类型:java.lang.Booleantrue:自增列,false:普通列
  • defaultValue 默认值,数据类型:java.lang.String
  • remark 列备注,数据类型:java.lang.String
  • jdbcType JDBC类型,对应java.sql.Types内类型,数据类型:java.lang.Integer
  • jdbcTypeName JDBC类型名称,数据类型:java.lang.String
  • javaProperty 格式化后的属性名称,如:userId,数据类型:java.lang.String
  • javaType Java数据类型短名,如:TimeStamp,数据类型:java.lang.String
  • fullJavaType Java数据类型全名,如:java.sql.TimeStamp,数据类型:java.lang.String
使用方式

freemarker模板${column.xxx},如列名的使用为${column.columnName}

基础参数

  • className:Class名称,freemarker指定模板生成文件的类名,模板内配置${className}使用
  • packageName:Package名称,freemarker指定模板生成文件的包名,模板内配置${packageName}使用

怎么自定义模板?

下面提供一个简单的模板示例,根据上面的内置参数可以任意自定义生成文件的内容。

<#if (packageName)??>
package ${packageName};
</#if>
import lombok.Data;

<#if (table.hasSqlDate)>
import java.sql.Date;
</#if>
<#if (table.hasTimeStamp)>
import java.sql.Timestamp;
</#if>
<#if (table.hasBigDecimal)>
import java.math.BigDecimal;
</#if>
/**
 * <p>本类代码由code-builder自动生成</p>
 * <p>表名: ${table.tableName} - ${table.remark}</p>
 * ===============================
 * Created with code-builder.
 * User:恒宇少年
 * Date:${.now}
 * 简书:http://www.jianshu.com/u/092df3f77bca
 * 码云:https://gitee.com/hengboy
 * ================================
 */
@Data
public class ${className} {
<#list table.primaryKeys as key>
    /**
     * ${key.columnName} - ${key.remark}
     */
    private ${key.javaType} ${key.javaProperty};
</#list>
<#list table.columns as column>
    <#if (!column.primaryKey)>
    /**
     * ${column.columnName} - ${column.remark}
     */
    private ${column.javaType} ${column.javaProperty};
    </#if>
</#list>
}

上面是一个数据实体的freemarker模板内容,把这个模板存放到freemarker目录下,对应在templates标签内添加配置就可以完成数据实体的自动创建,创建后的数据实体内容如下所示:

package com.code.builder.sample.model;
import lombok.Data;

import java.sql.Timestamp;
/**
 * <p>本类代码由code-builder自动生成</p>
 * <p>表名: app_balance_type - 余额类型信息表</p>
 * ===============================
 * Created with code-builder.
 * User:恒宇少年
 * Date:Jul 17, 2018 9:09:13 PM
 * 简书:http://www.jianshu.com/u/092df3f77bca
 * 码云:https://gitee.com/hengboy
 * ================================
 */
@Data
public class BalanceTypeEntity {
    /**
     * BT_ID - 余额类型主键
     */
    private String btId;
    /**
     * BT_NAME - 余额类型名称
     */
    private String btName;
    /**
     * BT_FLAG - 余额类型标识
     */
    private String btFlag;
    /**
     * BT_CREATE_TIME - 添加时间
     */
    private Timestamp btCreateTime;
    /**
     * BT_MARK - 余额类型备注信息
     */
    private String btMark;
}

创建的实体类去了哪里?

创建的实体类会在target/generated-sources/java目录下,如果你配置packagePrefix参数,会自动在生成目录下创建packagePrefix配置值的子目录。 如:

<packagePrefix>com.code.builder.sample</packagePrefix>

则最终创建的生成根目录为:target/generated-sources/java/com/code/builder/sample

怎么使用?

SpringBoot 方式使用

  1. 运行项目就可以根据配置生成对应的文件

Maven Plugin 方式使用

  1. 执行mvn clean命令用于清空target目录下的内容
  2. 执行mvn compile命令编译项目并且生成实体类

为什么SpringBoot方式不用配置数据库信息?

Maven Plugin方式是通过配置的数据库连接信息以及数据库连接驱动获取数据库连接对象Connection后来操作JDBC元数据

SpringBoot方式则是直接使用项目中配置的DataSource对象实例来进行获取的Connection数据库连接对象后来操作JDBC元数据

注意:如果你是多数据源项目,默认会使用primary数据源实例。

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: You must give any other recipients of the Work or Derivative Works a copy of this License; and You must cause any modified files to carry prominent notices stating that You changed the files; and You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "{}" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright 2018 恒宇少年 Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

code-builder是适配目前主流数据库的代码生成Maven插件、SpringBoot Starter,提供全部内置属性来百分之百自定义模板来生成对应的代码并自动生成文件. 展开 收起
Java
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/hbyufan/code-builder.git
git@gitee.com:hbyufan/code-builder.git
hbyufan
code-builder
code-builder
master

搜索帮助