代码生成器 如此好用~

时间:2021-08-13
本文章向大家介绍代码生成器 如此好用~,主要包括代码生成器 如此好用~使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

代码自动生成器

我们以前写CRUD一直都很麻烦 比较头疼,接下来简单介绍一下代码自动生成器,让我们的工作效率更高效!!

mybatis-plus代码生成器

  那这该怎么使用呢 ,非常的简单好用,步骤如下:

  1.创建一个maven项目        

  2.导入mybatis-plus依赖

 <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.0.5</version>
</dependency>

  3.编写代码自动生成器

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

    mybatis-plus官网:https://mp.baomidou.com/guide/generator.html 

    创建一个类如AutoCreateCode.java,创建一个方法如:getCode(),方法内容如下:

 public void  getCode(){
        //构建一个代码生成器对象
        AutoGenerator mpg=new AutoGenerator();
        //配置
        /**
         * 全局配置
         */
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");//获取当前项目路径
        gc.setOutputDir(projectPath+"/src/main/java");//设置代码生成的路径
        gc.setOpen(false);//是否打开资源管理器
        gc.setAuthor("Mr.li");//设置作者
        gc.setFileOverride(false);//是否覆盖原来的
        gc.setServiceName("%sService");//去掉Service的I前缀
        gc.setIdType(IdType.ID_WORKER);//主键生成策略:
        gc.setDateType(DateType.ONLY_DATE);//设置日期类型
        gc.setSwagger2(true);//是否用swagger

        mpg.setGlobalConfig(gc);
        /**
         * 数据源配置
         */
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/mybatis_plus?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC");
        dsc.setUsername("root");
        dsc.setPassword("root");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setDbType(DbType.MYSQL);//设置数据库类型
        mpg.setDataSource(dsc);
        /**
         * 包配置
         */
        PackageConfig pc = new PackageConfig();
        pc.setModuleName("MEC");//设置模块名称
        pc.setParent("com.ljh");//该模块放到com.ljh下
        pc.setEntity("pojo");//实体类包名称
        pc.setMapper("mapper");//mapper包名称
        pc.setService("service");
        pc.setController("controller");
        mpg.setPackageInfo(pc);
        /**
         * 策略配置
         */
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);//设置命名规则_转驼峰命名
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);//设置字段属性命名规则_转驼峰命名
        strategy.setInclude("user");//设置要映射的表名
        strategy.setEntityLombokModel(true);//自动生成lombok
        strategy.setLogicDeleteFieldName("deleted");//逻辑删除字段
        //自动填充配置
        TableFill createTime = new TableFill("create_time", FieldFill.INSERT);
        TableFill updateTime = new TableFill("update_time", FieldFill.INSERT_UPDATE);
        ArrayList<TableFill> tableFills = new ArrayList<>();
        tableFills.add(createTime);
        tableFills.add(updateTime);
        strategy.setTableFillList(tableFills);
        //乐观锁配置
        strategy.setVersionFieldName("version");

        strategy.setRestControllerStyle(true);
        strategy.setControllerMappingHyphenStyle(true);
        mpg.setStrategy(strategy);
        mpg.execute();//执行
    }

  4.调用测试

  在main方法中调用即可,运行可能出现  java.lang.NoClassDefFoundError: org/apache/velocity/context/Context 异常,解决方式如下:添加依赖即可

 

 <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.0</version>
</dependency>

  再次启动测试,可以看到:

  

关于mybatis-plus的代码生成器就完成了,关于mybatis-plus的基本使用  请查看官网:https://mp.baomidou.com/guide/quick-start.html

 

原文地址:https://www.cnblogs.com/ljhStudy/p/15139273.html