1 Star 1 Fork 0

mark / fount4j-generator

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

Fount4j Generator

Fount4j Generator 是使用 Java 语言编写的,基于 Beetl 模板引擎的数据库反向工程项目。内置 MyBatis 的 Entity、Dao、Mapper 文件模板。

特点

  • 生成的文件中增加了自定义的内容后,再次生成不会被合并;
  • 支持自定义实体类、Dao 类、映射文件的名字,而不像 mybatis-generator 生成为 EntityMapper.java 和 EntityMapper.xml;
  • 可以自定义文件的模板,实现深度定制需求;
  • 通过自定义解析类,能够很方便地扩展生成器。

用法

命令行方式启动

  1. 下载该项目附件中的压缩包 下载地址
  2. 解压后将自己使用的数据库驱动 jar 包放入 libs 目录
  3. 修改 ./assets/generator.yml 配置文件
  4. 运行 start.bat

使用项目源码

git clone 或者 zip 打包下载项目

使用项目 jar 包

  1. 下载项目附件中的 jar 包 下载地址

  2. 将 jar 包加入自己的项目依赖中,编写 Java 代码调用生成器。

  3. 在项目目录 assets 目录下新建 generator.yml 配置文件

     dataSource:
       driverClass: org.h2.Driver
       url: jdbc:h2:file:./assets/data/fount4j-generator
       user: sa
       password: ""
     
     introspectors:
       -
         type: entity
         ignoreTablePrefix: FT_
         # 代码路径
         resourcePath: ./src/main/resources
         packageName: com.fount4j.demo.entity
       -
         type: dao
         resourcePath: ./src/main/resources
         packageName: com.fount4j.demo.dao
         imports:
           - org.springframework.stereotype.Repository
       -
         type: mapper
         resourcePath: ./src/main/resources
         packageName: mappers.mysql
     
     # 要生成的表
     tables:
       -
         name: FT_USER
  4. 编写代码调用生成器

     GeneratorContext context = new YmlGeneratorContext("./config/generator.yml");
     Generator generator = new Generator(context);
     generator.generate();

项目依赖的库

帮助

配置文件

对 YAML 配置文件不熟悉的可以先通过YAML 教程了解配置文件的语法。

# 需要加入到 classPath 中的 jar 包
classPathEntry:
  - "./libs/h2-1.4.193.jar"

dataSource:
  class: com.fount4j.generator.introspector.DataSourceIntrospector
  driverClass: org.h2.Driver
  url: jdbc:h2:file:./assets/data/fount4j-generator
  user: sa
  password: ""
  # 其他属性会在创建数据库连接时传入 DriverManager.getConnection(url, properties) 方法
  # 如:MySQL 需要配置“useInformationSchema: "true"”才能获取到表的注释
  otherProperty: ""

# 模板配置
template:
  # Beetl 配置文件的位置,默认从 ClassPath 加载文件,如以 file: 开头则以绝对路径加载配置
  config: ./config/beetl.cfg
  # 模板根目录
  root: ./template/

# 基础解析器配置,继承 com.fount4j.generator.introspect.TableIntrospectorector 可以实现更多自定义操作
tableIntrospector: com.fount4j.generator.introspector.extend.TableIntrospector
# 表字段解析器
columnIntrospector: com.fount4j.generator.introspector.extend.ColumnIntrospector

introspectors:
  -
    # 解析器的类路径
    class: com.fount4j.generator.introspector.extend.EntityIntrospector
    # 模板文件名以及解析结果在模板变量中的键值
    infoKey: entity
    # 是否生成文件,有的解析器只负责解析参数,不生成文件
    generate: true
    # 是否忽略数据库表的前缀,多个前缀使用“,”隔开,如“FT_,SYS_”
    # 配置该参数后,“FT_USER”表转换实体类名称时,按照表名为“USER”处理
    ignoreTablePrefix: FT_
    # 代码路径
    resourcePath: E:\code\fount4j-generator\src\main\resources
    # 实体类包路径
    packageName: com.fount4j.demo.entity
    # 实体类中要添加的导入,例如放在不同包下面的父类
    imports:
      - com.fount4j.base.entity.BaseEntity
  -
    # 项目内部封装了 entity, dao, mapper 三种 type
    # 当配置了 type 时,可以不配置 class 以及 infoKey
    # type: entity 等同于 class: com.fount4j.generator.introspector.extend.EntityIntrospector infoKey: entity
    # type: dao 等同于 class: com.fount4j.generator.introspector.extend.DaoIntrospector infoKey: dao
    # type: mapper 等同于 class: com.fount4j.generator.introspector.extend.MyBatisMapperIntrospector infoKey: mapper
    type: dao
    resourcePath: ./src/main/resources
    packageName: com.fount4j.demo.dao
    # Dao 类名相对于 Entity 的后缀,如 Entity 为 User,那么 Dao 类的名字会是 UserDao
    nameSuffix: Dao
    # Dao 中要添加的导入,如 Spring 的 Repository 注解
    imports:
      - org.springframework.stereotype.Repository
  -
    class: com.fount4j.generator.introspector.extend.MyBatisMapperIntrospector
    infoKey: mapper
    resourcePath: ./src/main/resources
    packageName: mappers.mysql
    nameSuffix: Mapper

# 要生成的表
tables:
  -
    # 表名,大小写需要与数据库一致
    name: FT_USER
    # 表的 catalog
    catalog: ""
    # 表的 schema
    schema: PUBLIC
  -
    name: FT_CONFIG

文件自定义内容

在文件中,以注释的方式,在自定义内容前后行增加<custom></custom>标签,即可在下次生成文件时保持自定义区域不被覆盖。如:

// <custom> 这是 Java 类中标注自定义区域的方式
public String toString() {
    ...
}
// </custom>

<!-- <custom> 这是 XML 中标注自定义区域的方式 -->
<select id="selectByParams" parameterType="java.util.Map" resultMap="BaseResultMap">
    ...
</select>
<!-- </custom> -->

修改模板内容

修改模板前建议通过Beetl 官网了解本项目所使用的模板引擎相关知识。

生成的文件

实体类:FtUser.java

package com.fount4j.demo.entity;

/**
 * 用户表<br>
 * FT_USER<br>
 *
 * @author Fount4j generator
 */
public class FtUser {

    /**
     * 主键:用户ID<br>
     * ID BIGINT(19)<br>
     */
    private Long id;
    ...

    /**
     * 登录名<br>
     * LOGIN_NAME VARCHAR(50)<br>
     */
    private String loginName;

    /**
     * get 主键:用户ID<br>
     * ID BIGINT(19)<br>
     *
     * @return 主键:用户ID
     */
    public Long getId() {
        return id;
    }
    ...
}

Dao 类:FtUserDao.java

package com.fount4j.demo.dao;

import com.fount4j.demo.entity.FtUser;

/**
 * 用户表 Dao<br>
 * FT_USER<br>
 *
 * @author Fount4j generator
 */
public interface FtUserDao {

    /**
     * 插入一条记录(忽略空列)
     *
     * @param record 用户表
     * @return 影响的行数
     */
    int insertSelective(FtUser record);

    /**
     * 根据主键删除一条记录
     *
     * @param key 主键
     * @return 影响的行数
     */
    int deleteByPrimaryKey(Long key);

    /**
     * 更新一条记录
     *
     * @param record 用户表
     * @return 影响的行数
     */
    int updateByPrimaryKey(FtUser record);

    /**
     * 更新一条记录(不更新 NULL 的字段)
     *
     * @param record 用户表
     * @return 影响的行数
     */
    int updateByPrimaryKeySelective(FtUser record);

    /**
     * 根据主键查询一条记录
     *
     * @param key 主键
     * @return 用户表
     */
    FtUser selectByPrimaryKey(Long key);

}

映射文件:FtUserMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.fount4j.demo.dao.FtUserDao">
  <resultMap id="BaseResultMap" type="com.fount4j.demo.entity.FtUser">
      <id column="ID" jdbcType="BIGINT" property="id"/><!-- 主键:用户ID -->
      <result column="LOGIN_NAME" jdbcType="VARCHAR" property="loginName"/><!-- 登录名 -->
      ...
  </resultMap>

  <sql id="Base_Column_List">
    ID,CREATE_TIME,UPDATE_TIME,LOGIN_NAME,REAL_NAME,EMAIL,MOBILE,PASSWORD,STATUS
  </sql>

  <insert id="insertSelective" parameterType="com.fount4j.demo.entity.FtUser">
    insert into FT_USER (
      ID,
    <if test="loginName != null and loginName != ''">
      LOGIN_NAME,
    </if>
    ...
    ) values (
      #{id,jdbcType=BIGINT},
    <if test="loginName != null and loginName != ''">
      #{loginName,jdbcType=VARCHAR},
    </if>
    ...
    )
  </insert>

  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    delete from FT_USER where ID = #{id,jdbcType=BIGINT}
  </delete>

  <update id="updateByPrimaryKey" parameterType="com.fount4j.demo.entity.FtUser">
    update FT_USER
    set
      LOGIN_NAME = #{loginName,jdbcType=VARCHAR},
      ...
    where ID = #{id,jdbcType=BIGINT}
  </update>

  <update id="updateByPrimaryKeySelective" parameterType="com.fount4j.demo.entity.FtUser">
    update FT_USER
    set
      <if test="loginName != null and loginName != ''">
        LOGIN_NAME = #{loginName,jdbcType=VARCHAR},
      </if>
      ...
    where ID = #{id,jdbcType=BIGINT}
  </update>

  <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
      select
      <include refid="Base_Column_List"/>
      from FT_USER where ID = #{id,jdbcType=BIGINT}
  </select>
</mapper>
GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. [http://fsf.org/] Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below. 0. Additional Definitions. As used herein, "this License" refers to version 3 of the GNU Lesser General Public License, and the "GNU GPL" refers to version 3 of the GNU General Public License. "The Library" refers to a covered work governed by this License, other than an Application or a Combined Work as defined below. An "Application" is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library. A "Combined Work" is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the "Linked Version". The "Minimal Corresponding Source" for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version. The "Corresponding Application Code" for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work. 1. Exception to Section 3 of the GNU GPL. You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL. 2. Conveying Modified Versions. If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version: a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy. 3. Object Code Incorporating Material from Library Header Files. The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following: a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the object code with a copy of the GNU GPL and this license document. 4. Combined Works. You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following: a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the Combined Work with a copy of the GNU GPL and this license document. c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document. d) Do one of the following: 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source. 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version. e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.) 5. Combined Libraries. You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License. b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 6. Revised Versions of the GNU Lesser General Public License. The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation. If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library.

简介

基于 Beetl 模板引擎的数据库反向工程项目。内置 MyBatis 的 Entity、Dao、Mapper 文件模板。 展开 收起
Java
LGPL-3.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/mark0931/fount4j-generator.git
git@gitee.com:mark0931/fount4j-generator.git
mark0931
fount4j-generator
fount4j-generator
master

搜索帮助