使用逆向工程生成代码时,会发现mapper类和*mapper.xml会出现一个类中重复生成多次代码的问题,这会导致项目运行时初始化mapper失败并报错

翻阅mybatis官方API文档,发现了一下这句话

MySql does not properly support SQL catalogs and schema. If you run the create schema command in MySql, it actually creates a database - and the JDBC driver reports it back as a catalog. But MySql syntax does not support the standard catalog..table SQL syntax.

For this reason, it is best to not specify either catalog or schema in generator configurations. Just specify table names and specify the database in the JDBC URL.

If you are using version 8.x of Connector/J you may notice that the generator attempts to generate code for tables in the MySql information schemas (sys, information_schema, performance_schema, etc.) This is probably not what you want! To disable this behavior, add the property "nullCatalogMeansCurrent=true" to your JDBC URL.

For example:

    <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/my_schema"
            userId="my_user" password="my_password">
        <property name="nullCatalogMeansCurrent" value="true" />
    </jdbcConnection>

大意就是:如果你不是标准的数据库表命名格式,就有可能出现数据库中的所有同名表被多次生成的情况。

解决方法

在generator的xml配置文件里,数据库连接部分加上 <property name="nullCatalogMeansCurrent" value="true" />

<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
    <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
    connectionURL="jdbc:mysql://localhost:3306/shop?useUnicode=true&amp;useSSL=false&amp;characterEncoding=UTF-8&amp;serverTimezone=UTC"
    userId="root"
    password="123456">
        <!--解决mysql8.0以后重复生成所有表的问题-->
        <property name="nullCatalogMeansCurrent" value="true" />
    </jdbcConnection>

下附mybatis generator的配置文件

generatorConfig.xml,放在resources下,与application.yml同级

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
    <!--添加数据库的连接jar包的位置,在项目的左边最下的external libraries中找到复制全路径-->
    <classPathEntry location="D:\code\maven_repository\mysql\mysql-connector-java\8.0.21\mysql-connector-java-8.0.21.jar"/>
    <!--
       targetRuntime  :MyBatis3 生成  exmple
                       MyBatis3Simple   不生成exmple
     -->
    <context id="testTables" targetRuntime="MyBatis3Simple">
        <commentGenerator>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>

        <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/shop?useUnicode=true&amp;useSSL=false&amp;characterEncoding=UTF-8&amp;serverTimezone=UTC"
                        userId="root"
                        password="123456">
            <!--解决mysql8.0以后重复生成所有表的问题-->
            <property name="nullCatalogMeansCurrent" value="true" />
        </jdbcConnection>

        <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
            NUMERIC 类型解析为java.math.BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!-- targetProject:生成PO类的位置 -->
        <javaModelGenerator targetPackage="com.yijumao.shop.domain"
                            targetProject=".\src\main\java\">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
            <!-- 从数据库返回的值被清理前后的空格 -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!-- targetProject:mapper映射文件生成的位置 -->
        <sqlMapGenerator targetPackage="com.yijumao.shop.mapper"
                         targetProject=".\src\main\java\">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>

        <!-- targetPackage:mapper接口生成的位置 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.yijumao.shop.mapper"
                             targetProject=".\src\main\java\">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>

        <!-- 指定在数据库中已经存在的表名 -->
        <table schema="" tableName="admin_user" ></table>
        <table schema="" tableName="user"  ></table>
        <table schema="" tableName="item_cat"></table>
        <table schema="" tableName="item"></table>
        <table schema="" tableName="orders"></table>
        <table schema="" tableName="order_item"></table>
        <!-- 有些表的字段需要指定java类型
         <table schema="" tableName="user">
            <columnOverride column="id" javaType="Long" />
        </table> -->


    </context>
</generatorConfiguration>

pom.xml,添加插件和依赖

<!-- 追加依赖 -->
<dependencies>
    <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.4.0</version>
        </dependency>
        <dependency>
</dependencies>

<!-- 追加插件 -->
<build>
        <plugins>
            <!--配置Mybatis反向代理的插件-->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.4.0</version>
                <configuration>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>
            <!-- 设置编译源代码JDK的版本 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>