MyBatis Generator:[WARNING] Table Configuration users matched more than one table

在使用 MyBatis Generator 自动生成代码时出现如下警告,生成的文件与目标存在偏差。

[WARNING] Table Configuration users matched more than one table (test..users,performance_schema..users)
[WARNING] Cannot obtain primary key information from the database, generated objects may be incomplete

我在 generatorConfig.xml 配置文件中指定了对应的数据表是 test 数据库中的 users 表(见如下配置文件中的部分代码),而最终生成的 mapper.xml 文件中同时存在着 performance_schema 中的 users 表和 test 中的 users 表的查询映射语句,而且缺少了针对主键的查询语句(有关 MyBatis Generator XML 配置文件的详解可见我的另一篇 博客)。

generatorConfig.xml 部分代码如下:

        <!-- jdbc连接 -->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/test?serverTimezone=UTC" userId="root"
                        password="123456">

        </jdbcConnection>

        .
        .
        .

        <!--指定数据表-->
        <table tableName="users" enableCountByExample="false" enableUpdateByExample="false"
                enableDeleteByExample="false" enableSelectByExample="false" enableSelectByPrimaryKey="true"
                enableDeleteByPrimaryKey="true" enableUpdateByPrimaryKey="true" selectByExampleQueryId="false">
            <property name="useActualColumnNames" value="false"/>
        </table>

 mapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xsr.springboottest.dao.UsersMapper">
  <resultMap id="BaseResultMap" type="com.xsr.springboottest.entity.Users">
    <result column="USER" jdbcType="CHAR" property="user" />
    <result column="CURRENT_CONNECTIONS" jdbcType="BIGINT" property="currentConnections" />
    <result column="TOTAL_CONNECTIONS" jdbcType="BIGINT" property="totalConnections" />
  </resultMap>
  <insert id="insert" parameterType="com.xsr.springboottest.entity.Users">
    insert into users (`USER`, CURRENT_CONNECTIONS, TOTAL_CONNECTIONS
      )
    values (#{user,jdbcType=CHAR}, #{currentConnections,jdbcType=BIGINT}, #{totalConnections,jdbcType=BIGINT}
      )
  </insert>
  <select id="selectAll" resultMap="BaseResultMap">
    select `USER`, CURRENT_CONNECTIONS, TOTAL_CONNECTIONS
    from users
  </select>
  <resultMap id="BaseResultMap" type="com.xsr.springboottest.entity.Users">
    <result column="id" jdbcType="BIGINT" property="id" />
    <result column="userName" jdbcType="VARCHAR" property="username" />
    <result column="passWord" jdbcType="VARCHAR" property="password" />
    <result column="user_sex" jdbcType="VARCHAR" property="userSex" />
    <result column="nick_name" jdbcType="VARCHAR" property="nickName" />
  </resultMap>
  <insert id="insert" parameterType="com.xsr.springboottest.entity.Users">
    insert into users (id, userName, `passWord`, 
      user_sex, nick_name)
    values (#{id,jdbcType=BIGINT}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, 
      #{userSex,jdbcType=VARCHAR}, #{nickName,jdbcType=VARCHAR})
  </insert>
  <select id="selectAll" resultMap="BaseResultMap">
    select id, userName, `passWord`, user_sex, nick_name
    from users
  </select>
</mapper>

环境

数据库:MySQL 5.7

数据库驱动:mysql-connector-java-8.0.16.jar

MyBatis Generator 1.3.7

问题解决

在 MyBatis Generator官网 中对这一问题做出了解答。

Catalogs and Schema

大致翻译下:Mysql 无法正常支持 SQL catalogs 和 schema。因此,最好不要在 generator 配置文件中指定 catalog 以及schema,仅需指定数据表的名字并在 JDBC URL 中指定数据库即可。如果使用 mysql-connector-java 8.x 版本,generator 会为MySql中信息数据库(sys, information_schema, performance_schema)的表生成代码,若要避免这种操作,请在 JDBC URL  中加入属性“nullCatalogMeansCurrent=true”。有关于 MySQL information schemas的信息可参阅 这篇博文

在 generatorConfig.xml 中的 <jdbcConnection> 标签下加入如上属性后问题得到解决,生成文件无误,红框表示生成的文件。

文件目录

自动生成的 UserMapper.xml 文件,其他文件内容就不展示了。 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xsr.springboottest.dao.UsersMapper">
  <resultMap id="BaseResultMap" type="com.xsr.springboottest.entity.Users">
    <result column="USER" jdbcType="CHAR" property="user" />
    <result column="CURRENT_CONNECTIONS" jdbcType="BIGINT" property="currentConnections" />
    <result column="TOTAL_CONNECTIONS" jdbcType="BIGINT" property="totalConnections" />
  </resultMap>
  <insert id="insert" parameterType="com.xsr.springboottest.entity.Users">
    insert into users (`USER`, CURRENT_CONNECTIONS, TOTAL_CONNECTIONS
      )
    values (#{user,jdbcType=CHAR}, #{currentConnections,jdbcType=BIGINT}, #{totalConnections,jdbcType=BIGINT}
      )
  </insert>
  <select id="selectAll" resultMap="BaseResultMap">
    select `USER`, CURRENT_CONNECTIONS, TOTAL_CONNECTIONS
    from users
  </select>
  <resultMap id="BaseResultMap" type="com.xsr.springboottest.entity.Users">
    <result column="id" jdbcType="BIGINT" property="id" />
    <result column="userName" jdbcType="VARCHAR" property="username" />
    <result column="passWord" jdbcType="VARCHAR" property="password" />
    <result column="user_sex" jdbcType="VARCHAR" property="userSex" />
    <result column="nick_name" jdbcType="VARCHAR" property="nickName" />
  </resultMap>
  <insert id="insert" parameterType="com.xsr.springboottest.entity.Users">
    insert into users (id, userName, `passWord`, 
      user_sex, nick_name)
    values (#{id,jdbcType=BIGINT}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, 
      #{userSex,jdbcType=VARCHAR}, #{nickName,jdbcType=VARCHAR})
  </insert>
  <select id="selectAll" resultMap="BaseResultMap">
    select id, userName, `passWord`, user_sex, nick_name
    from users
  </select>
</mapper>

参考文章

在解决该问题的过程中借鉴了如下文章,感谢作者们。

MyBatis Generator 生成器把其他数据库的同名表生成下来的问题

MyBatis Generator踩坑与自救

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值