• Springcloud 学习笔记05-Mybatis-Plus


    Mybatis-Plus是一个Mybatis的增强工具,只是在Mybatis的基础上做了增强却不做改变,MyBatis-Plus支持所有Mybatis原生的特性,所以引入Mybatis-Plus不会对现有的Mybatis构架产生任何影。Mybatis-Plus又简称(MP)是为简化开发,提高开发效率而生正如官网所说的。

    1.代码生成器

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

            <!-- mybatisPlus 核心库 -->
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>3.2.0</version>
            </dependency>
            <!-- 添加 代码生成器 依赖 -->
            <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>    

     AutoGenerator代码

    特别注意:路径最好设置为绝对路径

    package com.ttbank.flep.file.config;
    
    import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
    import com.baomidou.mybatisplus.generator.AutoGenerator;
    import com.baomidou.mybatisplus.generator.config.*;
    import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
    import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
    import org.apache.commons.lang.StringUtils;
    
    import java.util.Scanner;
    
    public class CodeGeneratorConfig {
        /**
         * 读取控制台内容
         */
        public static String scanner(String tip) {
            Scanner scanner = new Scanner(System.in);
            StringBuilder help = new StringBuilder();
            help.append("请输入" + tip + ":");
            System.out.println(help.toString());
            if (scanner.hasNext()) {
                String ipt = scanner.next();
                if (StringUtils.isNotBlank(ipt)) {
                    return ipt;
                }
            }
            throw new MybatisPlusException("请输入正确的" + tip + "!");
        }
    
        public static void main(String[] args) {
            // 代码生成器
            AutoGenerator mpg = new AutoGenerator();
    
            // 全局配置
            GlobalConfig gc = new GlobalConfig();
            String projectPath = "D:\workFiles\flep-test\flep-boot\flep-boot-file";
            System.out.println(projectPath);
            gc.setOutputDir(projectPath + "/src/main/java");
            gc.setAuthor("lucky");
            gc.setOpen(false);
            gc.setSwagger2(false);
            mpg.setGlobalConfig(gc);
    
            // 数据源配置
            DataSourceConfig dsc = new DataSourceConfig();
            dsc.setUrl("jdbc:mysql://localhost:3306/day1?serverTimezone=UTC&useSSL=false");
            dsc.setDriverName("com.mysql.cj.jdbc.Driver");
            dsc.setUsername("root");
            dsc.setPassword("8888888");
            mpg.setDataSource(dsc);
    
            // 包配置
            PackageConfig pc = new PackageConfig();
            pc.setParent("com.ttbank.flep.file");
            pc.setEntity("entity");
            pc.setMapper("mapper");
            pc.setService("service");
            pc.setServiceImpl("service.impl");
            pc.setXml("mapper.xml");
            mpg.setPackageInfo(pc);
    
            // 配置模板
            TemplateConfig templateConfig = new TemplateConfig();
            mpg.setTemplate(templateConfig);
    
            // 策略配置
            StrategyConfig strategy = new StrategyConfig();
            strategy.setNaming(NamingStrategy.underline_to_camel);
            strategy.setColumnNaming(NamingStrategy.underline_to_camel);
            strategy.setEntityLombokModel(true);
            strategy.setRestControllerStyle(true);
    
            strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
            strategy.setControllerMappingHyphenStyle(true);
            strategy.setTablePrefix(pc.getModuleName() + "_");
            mpg.setStrategy(strategy);
            mpg.setTemplateEngine(new FreemarkerTemplateEngine());
            mpg.execute();
            System.out.println("代码生成完毕");
      }
    }

    效果图:

    参考文献:https://mp.baomidou.com/guide/generator.html#使用教程

    https://blog.csdn.net/xie19900123/article/details/81448299

    https://zhuanlan.zhihu.com/p/338371471

  • 相关阅读:
    Python学习之路
    like's photos
    Django---子框架drf内容总结
    Django---子框架drf项目---路飞学城---前后端搭建
    Django---drf,JWT补充、基于就角色的权限控制(django内置auth体系)
    常见三种加密(MD5、非对称加密,对称加密)
    Django---drf,自定制频率、自动生成文档、JWT
    PEP8 常用规范
    Class meta
    Django---drf, books系列表接口、分页器、根据ip进行频率限制---作业
  • 原文地址:https://www.cnblogs.com/luckyplj/p/15166224.html
Copyright © 2020-2023  润新知