MyBatis-Plus的代码生成器的使用

MyBatis-Plus的代码生成器的使用

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

MyBatis-Plus

MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
annotation(注释) extension(扩展)core(核心)generator(自动生成)
官网:https://baomidou.com/guide/generator.html

使用

1、模板引擎 依赖

MyBatis-Plus 支持 Velocity(默认)、Freemarker、Beetl,用户可以选择自己熟悉的模板引擎,如果都不满足您的要求,可以采用自定义模板引擎。

 		<dependency>
           <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.0</version>
        </dependency>
        <!--mybatis-plus的代码生成器-->
        <!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-generator -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.4.0</version>
        </dependency>

        <!--添加模板引擎 velocity为默认引擎-->
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.2</version>
        </dependency>
       <!--
       Freemarker:
       <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.30</version>
        </dependency>
        beetl:
        <dependency>
            <groupId>com.ibeetl</groupId>
            <artifactId>beetl</artifactId>
            <version>3.2.1.RELEASE</version>
        </dependency>
        -->

注:如果使用非默认:需要在 AutoGenerator 中 设置模板引擎。

2、编写配置

package com.flyfly.utils;

public class MPUtil {
    public static void main(String[] args) {
        //选择引擎
        AutoGenerator mpg=new AutoGenerator();
        mpg.setTemplateEngine(new VelocityTemplateEngine());

        //配置全局属性
        GlobalConfig globalConfig = new GlobalConfig();
        String property = System.getProperty("user.dir");//此处定位到最上层的文件目录(F://MyProject)我的项目在MyProject文件下的campusRecruitment,因此下面加了:campusRecruitment
        globalConfig.setAuthor("flyfly") // 作者
                 .setOutputDir(property+"/campusRecruitment/src/main/java")// 生成路径
                 .setFileOverride(true) // 文件覆盖
                 .setIdType(IdType.AUTO) // 主键策略
                 .setEnableCache(false)  //是否开启二级缓存
                //设置文件名%s 表示自动填充
                .setMapperName("%sMapper")
                .setXmlName("%sMapper")
                .setServiceName("%sService")
                .setServiceImplName("%sServiceImpl")
                .setControllerName("%sController")
                .setBaseResultMap(true)
                .setBaseColumnList(true);

        //配置数据源
        DataSourceConfig dsConfig = new DataSourceConfig();
        dsConfig.setDbType(DbType.MYSQL)
                .setDriverName("com.mysql.cj.jdbc.Driver")
                .setUrl("jdbc:mysql://127.0.0.1:3306/school_ask_job?serverTimezone=GMT%2B8")
                .setUsername("root")
                .setPassword("password");

        //策略配置
        StrategyConfig stConfig = new StrategyConfig();
        stConfig.setCapitalMode(true)
                //设置要生成代码的表名前缀
                // .setTablePrefix("")
                .setInclude("token","news")  //配置要生成的表名
                //.setExclude("users")  //排除要生成的表
                .setNaming(NamingStrategy.underline_to_camel)  // 数据库表映射到实体的命名策略
                .setEntityLombokModel(true)
                .setEntityBuilderModel(true);   // 【实体】是否为构建者模型(默认 false)

        //包策略配置
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent("com.flyfly")
                .setEntity("pojo")
                .setMapper("mapper")
                .setXml("mapper")
                .setService("service")
                .setServiceImpl("service.impl")
                .setController("controller");

        //整合配置
        mpg.setGlobalConfig(globalConfig)
                .setDataSource(dsConfig)
                .setStrategy(stConfig)
                .setPackageInfo(packageConfig);

        //执行
        mpg.execute();
    }
}

点击运行此main方法则代码生成成功
在这里插入图片描述

遇到的坑

在这里插入图片描述

<strategy> 标签中 <include><exclude> 只能配置一项!

意为下图中的include和exclude只能有一个
在这里插入图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值