Mybatis代码生成器Mybatis-Generator

pom

        <!--   mybatis-plus代码生成器     -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.4.1</version>
        </dependency>

        <!--   mp启动jar包     -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.2</version>
        </dependency>

        <!--    mybatis-plus注解包,只负责提供依赖        -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-annotation</artifactId>
            <version>3.4.2</version>
        </dependency>

        <!--  mysql      -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.45</version>
        </dependency>

        <!--  模板引擎    -->
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.31</version>
        </dependency>

        <!-- 代码生成模块  -->
        <dependency>
            <groupId>com.auto</groupId>
            <artifactId>auto_entity</artifactId>
        </dependency>

代码

package com.auto.util;

import com.auto.common.BaseEntity;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.HashMap;
import java.util.Map;

/**
 * 代码生成器工具类
 */
public class MyGenerator {

    public static void main(String[] args) {
        generatorCode();
    }

    /**
     * 代码生成器
     */
    public static void generatorCode(){
        //配置需要生成的表名
        String table = "auto_user";
        //配置生成的子项目(生成到那个项目)
        String project = "auto_admin";
        //获取父项目路径
        String bathPath = System.getProperty("user.dir");
        //定义entity路径(生成实体类的地方)
        String entityPath = bathPath + "/auto_entity/src/main/java/com/auto/entity";
        //定义其他模块路径
        String projectPath = bathPath + "/" + project + "/src/main";

        //创建生成器对象
        AutoGenerator generator = new AutoGenerator();
        //创建全局配置对象
        GlobalConfig gc = new GlobalConfig();
        //阻止每次生成后打开文件夹
        gc.setOpen(false);
        //配置所有者,写在注释里面
        gc.setAuthor("zhuxb");
        //去掉service接口中的I
        gc.setServiceName("%sService");

        generator.setGlobalConfig(gc);

        //创建数据源配置
        DataSourceConfig dc = new DataSourceConfig();
        dc.setDriverName("com.mysql.jdbc.Driver");
        dc.setUrl("jdbc:mysql://192.168.75.131:3306/api_auto?useUnicode=true&characterEncoding=utf8&useSSL=false");
        dc.setUsername("root");
        dc.setPassword("123456");
        dc.setDbType(DbType.MYSQL);
        generator.setDataSource(dc);

        //创建包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.auto");
        Map<String, String> pathInfo = new HashMap<>();
        pathInfo.put("entity_path", entityPath);
        pathInfo.put("mapper_path", projectPath + "/java/com/auto/mapper");
        pathInfo.put("xml_path", projectPath + "/resources/com/auto/mapper");
        pathInfo.put("service_path", projectPath + "/java/com/auto/service");
        pathInfo.put("service_impl_path", projectPath + "/java/com/auto/service/impl");
        pathInfo.put("controller_path", projectPath + "/java/com/auto/controller");
        //自定义生成路径
        pc.setPathInfo(pathInfo);
        generator.setPackageInfo(pc);

        //创建策略配置对象
        StrategyConfig strategy = new StrategyConfig();
        //配置表名下划线转驼峰
        strategy.setNaming(NamingStrategy.underline_to_camel);
        //设置列宁下划线转驼峰
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        //配置所有entity类都加lombok注解
        strategy.setEntityLombokModel(true);
        //配置所有entity类都继承BaseEntity类   BaseEntity自己定义的类
        strategy.setSuperEntityClass(BaseEntity.class);
        //生成entity时候不在生成 id
        strategy.setSuperEntityColumns("id");
        strategy.setSuperEntityColumns("active");
        strategy.setSuperEntityColumns("created_by");
        strategy.setSuperEntityColumns("created");
        strategy.setSuperEntityColumns("modified_by");
        strategy.setSuperEntityColumns("modified");

        //所有的controller都加RestController注解
        strategy.setRestControllerStyle(true);
        //配置生成的表名
        strategy.setInclude(table);
        generator.setStrategy(strategy);

        //配置生成器模板引擎
        generator.setTemplateEngine(new FreemarkerTemplateEngine());

        //执行代码生成
        generator.execute();

    }

}

最后需要注意,如果需要启动服务,需要在@SpringBootApplication注解中添加一个属性,这个属性的作用是,指定mapper接口所在的包,不然就会报找不到mapper接口

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值