MyBatisPlus代码生成器的使用示例

目录导入依赖表结构当前项目结构配置代码生成器1、globalConfig 全局策略配置2、dataSourceConfig 数据源配置

AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码,极大的提升了开发效率。

导入依赖

        <dependency>            <groupId>com.baomidou</groupId>            <artifactId>mybatis-plus-boot-starter</artifactId>            <version>3.0.5</version>        </dependency>        <dependency>            <groupId>org.apache.velocity</groupId>            <artifactId>velocity-engine-core</artifactId>            <version>2.0</version>        </dependency>        <dependency>            <groupId>io.swagger</groupId>            <artifactId>swagger-annotations</artifactId>            <version>1.5.19</version>        </dependency>

更详细的代码生成器配置请查看官方文档:https://baomidou.com/pages/061573/#superentityclass

表结构

当前项目结构

配置代码生成器

1、globalConfig 全局策略配置

outputDir

生成文件的输出目录 默认值:D 盘根目录

fileOverride

是否覆盖已有文件 默认值:false

open

是否打开输出目录 默认值:true

enableCache

是否在 xml 中添加二级缓存配置 默认值:false

开发人员

默认值:null

kotlin

开启 Kotlin 模式 默认值:false

swagger2

开启 swagger2 模式 默认值:false

activeRecord

开启 ActiveRecord 模式 默认值:false

baseResultMap

开启 BaseResultMap 默认值:false

baseColumnList

开启 baseColumnList 默认值:false

dateType

时间类型对应策略 默认值:TIME_PACK

entityName

实体命名方式 默认值:null 例如:%sEntity 生成 UserEntity

mapperName

mapper 命名方式 默认值:null 例如:%sDao 生成 UserDao

xmlName

Mapper xml 命名方式 默认值:null 例如:%sDao 生成 UserDao.xml

serviceName

service 命名方式 默认值:null 例如:%sBusiness 生成 UserBusiness

serviceImplName

service impl 命名方式 默认值:null 例如:%sBusinessImpl 生成 UserBusinessImpl

controllerName

controller 命名方式 默认值:null 例如:%sAction 生成 UserAction

idType

指定生成的主键的 ID 类型 默认值:null

2、dataSourceConfig 数据源配置

dbQuery

数据库信息查询类 默认由 dbType 类型决定选择对应数据库内置实现

? 实现 IDbQuery 接口自定义数据库查询 SQL 语句 定制化返回自己需要的内容

dbType

数据库类型 该类内置了常用的数据库类型【必须】

schemaName

数据库 schema name 例如 PostgreSQL 可指定为 public

typeConvert

类型转换 默认由 dbType 类型决定选择对应数据库内置实现

? 实现 ITypeConvert 接口自定义数据库 字段类型 转换为自己需要的 java 类型,内置转换类型无法满足可实现 IColumnType 接口自定义

url

驱动连接的 URL

driverName

驱动名称

username

数据库连接用户名

password

数据库连接密码

package com.haoming;import com.baomidou.mybatisplus.annotation.DbType;import com.baomidou.mybatisplus.annotation.FieldFill;import com.baomidou.mybatisplus.annotation.IdType;import com.baomidou.mybatisplus.annotation.TableField;import com.baomidou.mybatisplus.generator.AutoGenerator;import com.baomidou.mybatisplus.generator.config.DataSourceConfig;import com.baomidou.mybatisplus.generator.config.GlobalConfig;import com.baomidou.mybatisplus.generator.config.PackageConfig;import com.baomidou.mybatisplus.generator.config.StrategyConfig;import com.baomidou.mybatisplus.generator.config.po.TableFill;import com.baomidou.mybatisplus.generator.config.rules.DateType;import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;import java.util.ArrayList;public class ChengCode {    public static void main(String[] args) {        //构建代码生成器对象        AutoGenerator mpg = new AutoGenerator();        //1、全局配置        GlobalConfig gc = new GlobalConfig();        String projectPath = System.getProperty("user.dir");        gc.setOutputDir(projectPath + "/src/main/java");//生成文件的输出目录        gc.setAuthor("鼠皓明");//作者        gc.setOpen(false);//是否打开输出目录        gc.setFileOverride(false);//是否覆盖已有的文件        gc.setServiceName("%sService");//去除Service的I前缀        gc.setIdType(IdType.ID_WORKER);//主键生成策略        //ONLY_DATE 只使用 java.util.date 代替,SQL_PACK 使用 java.sql 包下的,TIME_PACK 使用 java.time 包下的 java8 新的时间类型        gc.setDateType(DateType.TIME_PACK);//数据库时间类型 到 实体类时间类型 对应策略        gc.setSwagger2(true);//开启swagger2模式        mpg.setGlobalConfig(gc);        //2、数据源配置        DataSourceConfig dsc = new DataSourceConfig();        dsc.setUrl("jdbc:mysql://localhost:3306/mybatis_plus?useSSl=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC");        dsc.setDriverName("com.mysql.cj.jdbc.Driver");        dsc.setUsername("root");        dsc.setPassword("123456");        dsc.setDbType(DbType.MYSQL);//数据库类型        mpg.setDataSource(dsc);        //3、包的配置        PackageConfig pc = new PackageConfig();        pc.setModuleName("blog");//父包模块名        pc.setParent("com.cheng");//父包名,如果为空,将下面子包名必须写全部, 否则就只需写子包名        pc.setEntity("pojo");        pc.setMapper("mapper");        pc.setService("service");        pc.setController("controller");        mpg.setPackageInfo(pc);        //4、策略配置        StrategyConfig sy = new StrategyConfig();        sy.setInclude("user");//设置要映射的表,可以设置多张        sy.setNaming(NamingStrategy.underline_to_camel);//从数据库表到文件的命名策略,下划线转驼峰命名        sy.setColumnNaming(NamingStrategy.underline_to_camel);//列的命名策略        sy.setEntityLombokModel(true);//开启lombok支持        sy.setLogicDeleteFieldName("deleted");//设置逻辑删除字段        sy.setVersionFieldName("version");//设置乐观锁        sy.setRestControllerStyle(true);//开启controller的restful命名        sy.setControllerMappingHyphenStyle(true);//开启controller中请求映射的连字符样式,如:localhost:8080/hello_id_1        //设置自动填充        TableFill create_time = new TableFill("create_time", FieldFill.INSERT);        TableFill update_time = new TableFill("update_time", FieldFill.INSERT_UPDATE);        ArrayList<TableFill> tableFills = new ArrayList<>();        tableFills.add(create_time);        tableFills.add(update_time);        mpg.setStrategy(sy);        //执行代码生成器        mpg.execute();    }}

执行代码生成器,查看项目结构的变化

代码生成器执行成功,自动生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码。

到此这篇关于MyBatisPlus代码生成器的使用示例的文章就介绍到这了,更多相关MyBatisPlus代码生成器内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

【文章原创作者:韩国机房 kt.html欢迎留下您的宝贵建议】收敛自己的脾气,偶尔要刻意沉默,

MyBatisPlus代码生成器的使用示例

相关文章:

你感兴趣的文章:

标签云: