• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

fount4j-generator: 基于 Beetl 模板引擎的数据库反向工程项目。内置 MyBatis 的 Enti ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

fount4j-generator

开源软件地址:

https://gitee.com/mwxx/fount4j-generator

开源软件介绍:

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.ColumnIntrospectorintrospectors:  -    # 解析器的类路径    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>

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap