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

    科技2024-03-18  109

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

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

    MyBatis-Plus

    MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。 官网: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只能有一个

    Processed: 0.011, SQL: 8