MyBatisGenerator(MBG)代码自动生成器,从此解放你的双手

1.概述

官方文档解释:

MyBatis Generator (MBG) is a code generator for MyBatis MyBatis and iBATIS. 
It will generate code for all versions of MyBatis, and versions of iBATIS after version 2.2.0. 
It will introspect a database table (or many tables) and will generate artifacts that can be used to access the table(s). 
This lessens the initial nuisance of setting up objects and configuration files to interact with database tables. 
MBG seeks to make a major impact on the large percentage of database operations that are simple CRUD (Create, Retrieve, Update, Delete).
You will still need to hand code SQLand objects for join queries, or stored procedures.

MyBatis生成器(MBG)是MyBatis MyBatis和iBATIS的代码生成器。它将为MyBatis的所有版本以及2.2.0版之后的iBATIS版本生成代码。它将内省一个数据库表(或多个表),并生成可用于访问表的工件。这减少了设置对象和配置文件以与数据库表交互的最初麻烦。MBG试图对大部分简单CRUD(Create、Retrieve、Update、Delete)的数据库操作产生重大影响。仍然需要手工编写SQL和对象以用于连接查询或存储过程。

2.整体架构与数据流

在这里插入图片描述
整体来说,mbg的结构比较简单,清晰。
相对特点:

  • 代码的生成没有采用模板,而是把代码和xml的结构定义成了对应的java类
  • 支持kotlin语言 (侧面印证kotlin火热)
  • mbg设计之初就充分考虑了扩展性,设计了插件机制

3.快速入门

第一步:创建coder模块,导入依赖信息

<dependencies>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-generator</artifactId>
        <version>3.3.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.velocity</groupId>
        <artifactId>velocity-engine-core</artifactId>
        <version>2.0</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
</dependencies>

第二步:编写代码生成器代码:

public class CodeGet {

    public static void main(String[] args) {

        // 1、创建代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 2、全局配置
        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        //gc.setOutputDir(projectPath + "/src/main/java");
        // 例如模块路径:D:\\repos\\idearepos\\yygh1126\\yygh_parent\\service\\yygh_hosp
        gc.setOutputDir("模块路径"+"/src/main/java");

        gc.setServiceName("%sService");    //去掉Service接口的首字母I
        // gc.setAuthor("项目作者");
        gc.setAuthor("tlc");
        gc.setOpen(false);
        mpg.setGlobalConfig(gc);

        // 3、数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://192.168.136.10:3306/数据库名称");
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("用户名");
        dsc.setPassword("密码");
        //dsc.setDbType(数据库类型);
        dsc.setDbType(DbType.MYSQL);
        mpg.setDataSource(dsc);

        // 4、包配置
        PackageConfig pc = new PackageConfig();
        // com.tlc.yygh.hosp
        pc.setModuleName("hosp"); //模块名/包名
        pc.setParent("com.tlc.yygh");// 父级包名
        pc.setController("controller");
        pc.setEntity("entity");
        pc.setService("service");
        pc.setMapper("mapper");
        mpg.setPackageInfo(pc);

        // 5、策略配置
        StrategyConfig strategy = new StrategyConfig();

        strategy.setInclude("数据库表名");

        strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略

        strategy.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略
        strategy.setEntityLombokModel(true); // lombok 模型 @Accessors(chain = true) setter链式操作

        strategy.setRestControllerStyle(true); //restful api风格控制器
        strategy.setControllerMappingHyphenStyle(true); //url中驼峰转连字符

        mpg.setStrategy(strategy);

        // 6、执行
        mpg.execute();
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值