MyBatis插件生成代码 – MyBatis Generator常用配置

基于MyBatis Generator 1.4.1逆向解析数据库,使用常用配置力求最小配置

添加generator插件 – pom.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>org.example</groupId>
    <artifactId>mybatis-generator</artifactId>
    <version>1.0-SNAPSHOT</version>
 
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
 
    <!--    项目依赖MySQL连接配置   -->
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.30</version>
        </dependency>
    </dependencies>
 
    <!--    配置generator为maven插件 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.4.1</version>
 
                <!--    maven build时自动执行    -->
                <!--    <executions>-->
                <!--        <execution>-->
                <!--            <id>Generate MyBatis Artifacts</id>-->
                <!--            <goals>-->
                <!--                <goal>generate</goal>-->
                <!--            </goals>-->
                <!--        </execution>-->
                <!--    </executions>-->
 
                <configuration>
                    <!--    生成代码覆盖原文件   -->
                    <overwrite>true</overwrite>
                    <!--    打印生成进度日志    -->
                    <verbose>true</verbose>
                    <!--    插件所需依赖共享项目依赖,主要是jdbcDriver  -->
                    <includeCompileDependencies>true</includeCompileDependencies>
                    <!--    generatorConfig.xml位置   -->
                    <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

配置generator插件 – generatorConfig.xml配置

报错信息处理

XML Parser Error on line 52: 元素类型为 "context" 的内容必须匹配 "(property*,plugin*,commentGenerator?,(connectionFactory|jdbcConnection),javaTypeResolver?,javaModelGenerator,sqlMapGenerator?,javaClientGenerator?,table+)"

targetRuntime=”MyBatis3″时强制要求子标签满足以上正则表达式,应按照匹配顺序列出子标签,缺省标记*、?、+决定子标签是否强制

generate failed: Cannot instantiate object of type null

标签中强制属性type未填写,需要查阅官方文档,将各标签的Required属性补齐

generate failed: Exception getting JDBC Driver: com.mysql.cj.jdbc.Driver

把mysql-connector-java配置到<plugin>中的<dependency>,或在<plugin>中设置<includeCompileDependencies>

参考文献

MyBatis Generator官方文档

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

©2018-2024 Howell版权所有 备案号:冀ICP备19000576号