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.大佬博客

Vivado2023是一款集成开发环境软件,用于设计和验证FPGA(现场可编程门阵列)和可编程逻辑器件。对于使用Vivado2023的用户来说,license是必不可少的。 Vivado2023的license是一种许可证,用于授权用户合法使用该软件。许可证分为多种类型,包括评估许可证、开发许可证和节点许可证等。每种许可证都有不同的使用条件和功能。 评估许可证是免费提供的,让用户可以在一段时间内试用Vivado2023的全部功能。用户可以使用这个许可证来了解软件的性能和特点,对于初学者和小规模项目来说是一个很好的选择。但是,使用评估许可证的用户在使用期限过后需要购买正式的许可证才能继续使用软件。 开发许可证是付费的,可以永久使用Vivado2023的全部功能。这种许可证适用于需要长期使用Vivado2023进行开发的用户,通常是专业的FPGA设计师或工程师。购买开发许可证可以享受Vivado2023的技术支持和更新服务,确保软件始终保持最新的版本和功能。 节点许可证是用于多设备或分布式设计的许可证,可以在多个计算机上安装Vivado2023,并共享使用。节点许可证适用于大规模项目或需要多个处理节点进行设计的用户,可以提高工作效率和资源利用率。 总之,Vivado2023 license是用户在使用Vivado2023时必须考虑的问题。用户可以根据自己的需求选择合适的许可证类型,以便获取最佳的软件使用体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值