代码拉取完成,页面将自动刷新
Fount4j Generator 是使用 Java 语言编写的,基于 Beetl 模板引擎的数据库反向工程项目。内置 MyBatis 的 Entity、Dao、Mapper 文件模板。
git clone 或者 zip 打包下载项目
下载项目附件中的 jar 包 下载地址
将 jar 包加入自己的项目依赖中,编写 Java 代码调用生成器。
在项目目录 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
编写代码调用生成器
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 官网了解本项目所使用的模板引擎相关知识。
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;
}
...
}
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);
}
<?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>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。