• MyBatis-plus实现逆向生成器


    1、前言

    在日常的Spring Boot项目开发中,我们都会建立几个固有的包,分别是Controller、entity(pojo)、dao、service、serviceimpl
    在单个Spring Boot项目中,用手动建立倒是简单,但是在Spring Cloud项目中,会建立许多的Spring Boot项目,如果此时还使用手动建立这几个固有的包,那么就有点耽误时间了。
    作为程序猿的我们,怎么可能会手动做这种重复而又浪费时间的事呢!那么Mybatis-plus所提供的逆向生成器就发挥出了我们想要的效果。

    2、实现逆向生成器

    2.1、导入依赖

    
    <dependency>
    	<groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starterartifactId>
    dependency>
    
    
    <dependency>
        <groupId>com.baomidougroupId>
        <artifactId>mybatis-plus-boot-starterartifactId>
        <version>3.5.2version>
    dependency>
    
    
    <dependency>
        <groupId>com.baomidougroupId>
        <artifactId>mybatis-plus-generatorartifactId>
        <version>3.4.1version>
    dependency>
    
    
    <dependency>
        <groupId>mysqlgroupId>
        <artifactId>mysql-connector-javaartifactId>
        <scope>runtimescope>
    dependency>
    
    
    <dependency>
        <groupId>org.projectlombokgroupId>
        <artifactId>lombokartifactId>
        <optional>trueoptional>
    dependency>
    
    
    <dependency>
        <groupId>org.freemarkergroupId>
        <artifactId>freemarkerartifactId>
    dependency>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    mybatis-plus-generator这个依赖要使用3.5.0版本以下的,我使用的是3.4.1版本,因为这个依赖在3.5.0以后就进行了修改,需要使用相关类的Builder类来构建对象,然后Builder类所提供的方法又不多,所以我个人觉得使用起来不太方便。
    导入数据库的依赖,是因为在逆向生成的过程中,Mybatis-plus会根据指定的表来生成相应的实力类对象以及其他层的配置。
    导入模板引擎依赖,可以根据模板引擎来规定初始化生成的包的模式。

    2.2、项目结构

    在这里插入图片描述
    项目中只有一个Utils包用来存放相关的工具类,而我也把逆向生成器类放在了这个包里面,如上图所示的MybatisPlusGenerate类。
    我习惯了使用Spring Boot项目,所以这里的演示也是使用了Spring Boot项目,对spring Boot不熟悉的猿友可以使用Maven项目。

    2.3、MybatisPlusGenerate类

    public class MybatisPlusGenerate {
    
        private static Scanner scanner = new Scanner(System.in);
        // 获得当前项目的路径,如上图就是 F:stady_program/Mybatis_plus逆向工程
        private final static String PROJECT_PATH = System.getProperty("user.dir");
        
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2.3.1、获取用户在控制台输入的表名

     public static void scannerTableName(String parentPackageName){
            List<String> tableList = new ArrayList<>(); // 使用集合来存储用户输入的多个表的名称
            System.out.println("<=================表名列表===============>");
            while(!scanner.hasNext("end")){ // 当用户输入end后结束输入
                String tableName = scanner.next();
                if(tableName.equals("")){
                    throw new RuntimeException("所输入的表名不合法");
                }
                tableList.add(tableName); //将表名添加到集合中。
            }
            // 通过表名和当前项目的目录来生成相应的包和类。
            for(String name : tableList) generateInformationByTableName(parentPackageName,name);
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    当前项目的目录就是指当前Spring Boot项目中启动类所在的包的名称。也就是上图所示的com.example.mybatis_plus_reverse包。

    2.3.2、数据源的配置

     private static DataSourceConfig dataSourceConfig(){
            DataSourceConfig dataSourceConfig = new DataSourceConfig();
            dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/mybatis?useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai")
                    .setUsername("root")
                    .setPassword("123456")
                    .setDriverName("com.mysql.cj.jdbc.Driver").setDbType(DbType.MYSQL);
    
            return dataSourceConfig;
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    数据源的配置这就不用说了吧,这个配置没什么难度。

    2.3.3、全局属性的配置

     private static GlobalConfig globalConfig(){
            GlobalConfig globalConfig = new GlobalConfig();
            globalConfig.setOutputDir(PROJECT_PATH + "/src/main/java")// 输出文件路径
                    .setAuthor("Time Travel")// 设置作者名字
                    .setOpen(false)// 是否打开资源管理器
                    .setFileOverride(true)// 是否覆盖原来生成的
                    .setIdType(IdType.AUTO)// 主键策略
                    .setBaseResultMap(true)// 生成resultMap
                    .setDateType(DateType.ONLY_DATE) // 设置时间格式,采用Date
                    .setServiceName("%sService");// 生成的service接口名字首字母是否为I,这样设置就没有I
            //setBaseColumnList(true)  XML中生成基础列
            return globalConfig;
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    2.3.4、包属性的配置

        private static PackageConfig packageConfig(String fatherPackageName){
            PackageConfig packageConfig = new PackageConfig();
            packageConfig.setParent(fatherPackageName) // 配置指定项目中各层的名称
                    .setController("controller") // Controller层
                    .setEntity("entity") // 实体层(pojo层)
                    .setMapper("dao") // Dao 层
                    .setService("service") // service层
                    .setServiceImpl("service.serviceImpl"); // ServiceImp层
    
            return packageConfig;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    fatherPackageName就是上图所示的com.example.mybatis_plus_reverse包的名称。

    2.3.5、逆向生成类的名称配置

       private static StrategyConfig strategyConfig (String tableName){
            StrategyConfig strategyConfig = new StrategyConfig();
            strategyConfig.setCapitalMode(true)// 开启全局大写命名
                   .setInclude(tableName)// 设置要映射的表
                   .setNaming(NamingStrategy.underline_to_camel)// 下划线到驼峰的命名方式
                   .setColumnNaming(NamingStrategy.underline_to_camel)// 下划线到驼峰的命名方式
                   .setEntityLombokModel(true)// 是否使用lombok
                   .setRestControllerStyle(true)// 是否开启rest风格
                   .setTablePrefix("t_") // 去除前缀
                   .setControllerMappingHyphenStyle(true); // localhost:8080/hello_a_2
    
            return strategyConfig;
       }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    2.3.6、在resource目录下生成Mapper文件的配置

     private static InjectionConfig injectionConfig(){
            InjectionConfig injectionConfig = new InjectionConfig() {
                @Override
                public void initMap() {
                    this.setMap(new HashMap<>()); // 实现InjectionConfig抽象类就需要初始化一个Map集合
                }
            };
            List<FileOutConfig> fileOutConfigList = new ArrayList<>();
            // 根据/templates/mapper.xml.ftl规则在指定的位置生成Mapper文件,可以在多个地方生成。
            fileOutConfigList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
                @Override
                public String outputFile(TableInfo tableInfo) {
                    // 返回Mapper文件的绝对路径
                    String path = PROJECT_PATH + "/src/main/resources/mapper/" +
                            tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
                    return path;
                }
            });
            // 将对Mapper文件的配置添加到文件输出对象中
            injectionConfig.setFileOutConfigList(fileOutConfigList);
            return injectionConfig;
       }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    2.3.7、配置生成器的生成模板

    	// 最简单的配置
       private static TemplateConfig templateConfig() {
            TemplateConfig templateConfig = new TemplateConfig();
    
            return templateConfig.setXml(null);
       }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    	// 复杂点的配置
    	  private static TemplateConfig templateConfig() {
            TemplateConfig templateConfig = new TemplateConfig();
            templateConfig.setController("templates/controller.java")
                    .setEntity("templates/entity.java")
                    .setService("templates/service.java")
                    .setServiceImpl("templates/serviceImpl.java")
                    .setMapper("templates/mapper.java")
                    .setXml(null);
            return templateConfig;
       }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    使用第二个配置方式的前提需要在mybatis-plus-generator-3.4.1.jar这个包的templates目录中将上述的五个类的 .ftl 的文件复制到当前项目的resource目录下的templates包下。
    如:controller.java.ftl

    在这里插入图片描述

    2.3.8、判断用户输入的项目包名是否存在

      // 判断输入的父项目是否存在
       private static boolean parentPackageExits(String project, String parentPackageName){
            StringBuilder path = new StringBuilder(PROJECT_PATH);
            if(!"".equals(project) && project != null){
                path.append("/" + project);
            }
            path.append(("/src/main/java/" + parentPackageName).replace(".","/"));
            File file = new File(path.toString());
            return file.exists();
       }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2.3.9、测试

    // 执行、测试
        public static void main(String[] args) {
            System.out.print("父包名称: ");
            String input = scanner.next();
            if(!parentPackageExits(null,input)){
                throw new RuntimeException("输入的父包名不存在或者输入错误,请重新输入父包名");
            }
            scannerTableName(input);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    输入
    在这里插入图片描述
    输出上图内容则表示配置成功。

    实现效果
    在这里插入图片描述
    执行完毕后就会在项目中自动生成相应的包和类,因为我们在GlobleConfig中配置的setFileOverride属性,所以会覆盖掉相同的包。

    3、总结

    在日常的开发中,对于重复、费时、费力的操作,作为程序猿的我们,应该都能利用程序来简化这些操作,做到事倍功半的效果。

  • 相关阅读:
    .NET MVC Spring配置及常见问题处理
    软件测试那十个不为人知的秘密,千万不要被误解了、
    阿里P8架构师吐血整理的超全Java进阶教程:基础+容器+并发+虚拟机+IO
    [工业自动化-8]:西门子S7-15xxx编程 - PLC主站 - CPU模块
    wpf devexpress自定义编辑器
    Dev C++编写C/C++程序 出现[Error] ld returned 1 exit status报错分析及解决
    鸿蒙HarmonyOS应用开发:扫描仪文件扫描
    countUp插件用法-数字跳动特效
    SpringBoot整合Junit
    HTTP 面试知识点提炼
  • 原文地址:https://blog.csdn.net/qq_45515182/article/details/126379281