1 Star 4 Fork 0

xbatis/xbatis-generator-core

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
README.md 17.63 KB
一键复制 编辑 原始数据 按行查看 历史
Ai东 提交于 14天前 . 1.1.2

代码生成器

该模块从xbatis独立而出,已经历多次迭代,可放心使用!!!

maven引入

<dependency>
    <groupId>cn.xbatis</groupId>
    <artifactId>xbatis-generator-core</artifactId>
    <version>1.1.2-M1</version>
</dependency>

添加数据库驱动 例如:


<dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <version>9.1.0</version>
</dependency>

然后,编写一个任意带有 main 方法的类,如下所示

// 根据数据库链接生成
new FastGenerator(new GeneratorConfig(
        "jdbc:mysql://xxx.xx.x:3306/数据库名字",
        "用户名",
        "密码")
        .basePackage("com.test")//根包路径
        ).create();

        or
        
//根据数据源生成
new FastGenerator(new GeneratorConfig(
        DbType.H2,//数据库类型
        dataSource)
        .basePackage("com.test")//根包路径
        ).create();

配置 GeneratorConfig

属性 默认值 说明
charset utf-8 生成文件的字符集
containerType SPRING

容器类型,默认SPRING

目前支持,SPRING、SOLON

swaggerVersion 3 swagger版本:2 代表2.x,3代表3.x
author "" 作者
fileCover true 文件是否覆盖
ignoreView false 是否忽略视图
ignoreTable false 是否忽略表
baseFilePath System.getProperty("user.dir") + "/demo-generate" 根文件路径
basePackage NULL 根包路径
javaPath NULL 基于baseFilePath的java源码文件相对路径
resourcePath NULL 基于baseFilePath的resource文件相对路径
templateRootPath templates 模板根目录,默认即可
templateEngine new FreemarkerTemplateEngine() 模板引擎,默认Freemarker引擎,其他引擎需要自己实现
templateBuilders 包含 实体类,mapper,mapper xml,dao,service,serviceImpl,action等模板生成构建器 模板生成构建器,继承AbstractTemplateBuilder,即可实现自己的生成器(生成自己的页面或其他类等)

配置 TableConfig(表配置)

new GeneratorConfig(...).tableConfig(tableConfig->{
    tableConfig.includeTables("table1","table2");
});
属性 默认值 说明
tablePrefixes 表、视图的前缀,用于生成类名时忽略前缀
includeTables 默认包含所有表、视图
excludeTables 排除表,默认不排除

配置 ColumnConfig(列配置)

new GeneratorConfig(...).columnConfig(columnConfig->{
    
});
属性 默认值 说明
columnPrefixes 列前缀,可进行列字段忽略前缀
versionColumn 指定乐观锁列名
tenantIdColumn 指定租户ID列名
logicDeleteColumn 逻辑删除列名,配置实体类配置:logicDeleteCode 一起使用
disableUpdateColumns 禁止更新的列,这样字段上会生成@TableField(update=false)
disableSelectColumns 禁止Select的列,这样字段上会生成@TableField(select=false)
defaultValueConvert 默认实现 可动态转换数据库的默认值(由静态值转成动态值)

配置 EntityConfig(实体类配置)

new GeneratorConfig(...).entityConfig(entityConfig->{
    entityConfig.lombok(true).excludeColumns("create_time","creater_id");;
});
属性 默认值 说明
excludeColumns 排除列,默认不排除(在有公共实体类的时候很实用)
swagger false 是否开启swagger
serial false 是否序列化,会implements Serializable
createFieldClass true 是否生成静态字段名类Fields
superClass NULL 实体类的父类,例如:com.xx.test.BaseEntity
lombok true 是否开启lombok,这样类上会生成@Data
lombokBuilder false 是否开启lombok buidler,这样类上会生成@Buidler
defaultValueEnable true 是否生成默认值
schema false 注解上是否加上schema信息
packageName DO 实体类包名
nameConvert NULL 实体类名转换器,可以自定义规则,默认大驼峰规则
fieldNamingStrategy NamingStrategy.UNDERLINE_TO_CAMEL 字段名策略,支持 NO_CHANGE ,UNDERLINE_TO_CAMEL
fieldNameConverter NULL 字段名转换器,优先级大于 fieldNamingStrategy
remarksConverter NULL 字段备注转换器,用于实现不一样的备注
defaultTableIdCode NULL 默认TableId代码,数据库非自增时生效,例如@TableId(...)
logicDeleteCode NULL 默认@LogicDelete代码,数据库非自增时生效,例如@LogicDelete(beforeValue="0",afterValue="1",deleteTimeField="create_time")
typeMapping 内置包含各种列类型的java映射 数据库列类型映射,用于定制
alwaysAnnotation false 是否总是生成注解

配置 MapperConfig(mapper类配置)

new GeneratorConfig(...).mapperConfig(mapperConfig->{
    mapperConfig.mapperAnnotation(true);
});
属性 默认值 说明
superClass 默认继承 MybatisMapper 接口 Mapper接口的父接口,例如:cn.xbatis.core.mybatis.mapper.MybatisMapper
mapperAnnotation true 是否开启mybatis @Mapper注解,这样类上会生成@Mapper
packageName mapper mapper类的包名
suffix Mapper mapper类的后缀

配置 MapperXmlConfig(mapper xml配置)

new GeneratorConfig(...).mapperXmlConfig(mapperXmlConfig->{
    mapperXmlConfig.enable(true);
});
属性 默认值 说明
enable false 是否生成mapper xml
resultMap false 是否生成resultMap
columnList false 是否生成列信息,用于select 列
packageName mappers mapper xml的目录名字
suffix "" mapper xml文件的后缀

配置 DaoConfig(dao接口配置)

new GeneratorConfig(...).daoConfig(daoConfig->{
    daoConfig.enable(true);
});
属性 默认值 说明
enable true 是否生成 dao 接口
superClass 默认继承 Dao 接口 dao接口的父接口,例如:cn.xbatis.core.mvc.Dao
generic true 是否启用泛型,启用后会在superclass后面加泛型>Entity,ID>
packageName dao dao接口的包名
suffix Dao dao接口的后缀

配置 DaoImplConfig(dao接口实现类的配置)

new GeneratorConfig(...).daoImplConfig(daoImplConfig->{
    daoImplConfig.enable(true);
});
属性 默认值 说明
superClass 默认继承 DaoImpl 实现类 dao接口的父接口,例如:cn.xbatis.core.mvc.impl.DaoImpl
enable true 是否生成 dao impl接口
generic true 是否启用泛型,启用后会在superclass后面加泛型>Entity,ID>
packageName dao.impl dao实现类的包名
suffix DaoImpl dao实现类的后缀

配置 ServiceConfig(service接口配置)

new GeneratorConfig(...).serviceConfig(serviceConfig->{
    serviceConfig.enable(true);
});
属性 默认值 说明
enable true 是否生成 Service 接口
superClass 默认继承 Service 接口 Service接口的父接口,例如:cn.xbatis.core.mvc.Service
generic false 是否启用泛型,启用后会在superclass后面加泛型>Entity,ID>
packageName service Service接口的包名
suffix Service Service接口的后缀

配置 ServiceImplConfig(service接口实现类的配置)

new GeneratorConfig(...).serviceImplConfig(serviceImplConfig->{
    serviceImplConfig.injectDao(true);
});
属性 默认值 说明
enable true 是否生成 dao impl 接口
injectDao true 是否注入dao
injectMapper true 是否注入mapper
superClass 默认继承 ServiceImpl 实现类 dao接口的父接口,例如:cn.xbatis.core.mvc.impl.ServiceImpl
generic false 是否启用泛型,启用后会在superclass后面加泛型>Entity,ID>
packageName service.impl service实现类的包名
suffix ServiceImpl service实现类的后缀

配置 ActionConfig(action实现类的配置)

new GeneratorConfig(...).actionConfig(actionConfig->{
    actionConfig.enable(true);
});
属性 默认值 说明
enable true 是否生成控制器
swagger true 是否开启swagger
injectService true 是否注入service
superClass NULL action父类,例如:cn.xxx.BaseAction
generic false 是否启用泛型,启用后会在superclass后面加泛型>Entity,ID>
packageName action action实现类的包名
suffix Action action实现类的后缀
returnClass Object get save update delete find等返回的类型
enableSave true 是否生成save方法
enableUpdate true 是否生成update方法
enableDelete true 是否生成delete方法
enableGet true 是否生成get方法
enableFind true 是否生成find方法
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/xbatis/xbatis-generator-core.git
git@gitee.com:xbatis/xbatis-generator-core.git
xbatis
xbatis-generator-core
xbatis-generator-core
main

搜索帮助