Mybatis Plus 代码生成器-让上班划水不再是梦

1. 废话哔哔

不得不说, Mybatis Plus 的代码生成器真是个懒人神器, 它可以根据你的数据库表自动生成Controller + Service + Entity + Mapper 层的代码让你在工作或接私活的过程中爽到飞起; 废话少说,开搞老实人的笑容

2. 开搞

为了避免一些和我一样的菜逼程序员踩坑, 先列出一我的一些环境配置

2.1 核心maven依赖

	<!-- MP代码生成器的依赖 生成器依赖+模板引擎 -->
	<dependency>
	  <groupId>com.baomidou</groupId>
	  <artifactId>mybatis-plus-generator</artifactId>
	  <version>3.2.0</version>
	</dependency>
	<dependency>
	  <groupId>org.freemarker</groupId>
	  <artifactId>freemarker</artifactId>
	  <version>2.3.28</version>
	</dependency>
	
	<!-- Mybatis plus + mysql 驱动 + lombok -->
	<dependency>
	   <groupId>com.baomidou</groupId>
	   <artifactId>mybatis-plus-boot-starter</artifactId>
	   <version>3.2.0</version>
	</dependency>
	<dependency>
	   <groupId>mysql</groupId>
	   <artifactId>mysql-connector-java</artifactId>
	   <scope>runtime</scope>
	</dependency>
	<dependency>
	   <groupId>org.projectlombok</groupId>
	   <artifactId>lombok</artifactId>
	   <optional>true</optional>
	</dependency>
	

2.2 Spring Boot主要配置

spring:
  datasource:
    platform: mysql
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/your_database?serverTimezone=GMT%2b8&useUnicode=true&useSSL=false&characterEncoding=utf8
    username: your_username
    password: your_password
    
mybatis-plus:
  mapper-locations: classpath:mapper/*.xml #mybatis-plus mapper xml文件扫描路径 
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #mybatis-plus显示SQL

2.3 Mybatis Plus 代码生成器代码

public class MybatisPlusGenerator {
    public static String scanner(String someThing) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入" + someThing + ":");
        if (scanner.hasNext()) {
            String sc = scanner.next();
            if (StringUtils.isNotEmpty(sc)) {
                return sc;
            }
        }
        throw new MybatisPlusException("请输入正确的" + someThing + "!");
    }

    public static void main(String[] args) {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/src/main/java"); //生成文件的输出目录
        gc.setAuthor("AuthorName");
        gc.setOpen(false);
        mpg.setGlobalConfig(gc);

        // 你的数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://127.0.0.1:3306/your_database?serverTimezone=GMT%2b8&useUnicode=true&useSSL=false&characterEncoding=utf8");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("your_username");
        dsc.setPassword("your_password");
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName(scanner("模块名"));
        pc.setParent("com.jinchange.mp_demo"); //在这个包目录下创建模块
        mpg.setPackageInfo(pc);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };

        // 如果模板引擎是 freemarker
        String templatePath = "/templates/mapper.xml.ftl";
       
        // 自定义输出配置
        List<FileOutConfig> focList = new ArrayList<>();
        // 自定义配置会被优先输出
        focList.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
                return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
                        + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);

        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig();

        templateConfig.setXml(null);
        mpg.setTemplate(templateConfig);

        // 策略配置,数据库表配置
        StrategyConfig strategy = new StrategyConfig();
        //数据库表映射到实体的命名策略
        strategy.setNaming(NamingStrategy.underline_to_camel);
        //数据库表字段映射到实体类的命名策略
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        //自定义继承entity类,添加这一个会在生成实体类的时候继承entity
        //strategy.setSuperEntityClass("com.wy.testCodeGenerator.entity");
        //实体是否为lombok模型
        strategy.setEntityLombokModel(true);
        //生成@RestController控制器
        strategy.setRestControllerStyle(true);
        //是否继承controller
       // strategy.setSuperControllerClass("com.wy.testCodeGenerator.controller");
        strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));

        //驼峰转连字符串
        strategy.setControllerMappingHyphenStyle(true);
        //表前缀
        strategy.setTablePrefix(pc.getModuleName() + "_");
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }
}

ojbk, 现在将代码生成器粘贴到Spring Boot的测试类路径下中点击main函数运行 --> 选择你要生成的模块名 如generator --> 输入你要生成的表就可以愉快的偷懒了
测试类路径

2.4生成的代码结构如下

生成的目录结构生成的目录结构二
注意,需要再启动类配置Mapper扫描路径,否则会报找不到Mapper的错误

@MapperScan(basePackages = { "com.xxx.mapper" })

更详细的定制化配置请参考–>
1.Mybatis Plus官网
2.大佬博客

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值