现象
例如下面的错误信息:1
2
3
4
5
6
7
8
9
10
11
12
13# 是因为在另外一个库里的同名表 sys_dept 没有 status 和 dept_id 这两个字段。
Column status, specified for override in table sys_dept, does not exist in the table.
Column dept_id, specified as an identity column in table sys_dept, does not exist in the table.
# 是因为在另外一个库里的同名表 sys_menu 没有 menu_id 这个字段
Column menu_id, specified as an identity column in table sys_menu, does not exist in the table.
# 是因为有两个库存在表:sys_dept 和 sys_menu,所以在生成第二个库的同名表文件时,会提示:was overwritten
Existing file ...\entity\SysDeptExample.java was overwritten
Existing file ...\entity\SysDept.java was overwritten
Existing file ...\mapper\original\SysDeptMapper.java was overwritten
Existing file ...\entity\SysMenuExample.java was overwritten
Existing file ...\entity\SysMenu.java was overwritten
Existing file ...\mapper\original\SysMenuMapper.java was overwritten
值得注意的是,如果表结构不一致,不一定会报错。只有当配置里的字段不一致时才会报错或警告,所以,一定要避免从多个库生成同名表的 mapper。
针对表的配置,如:1
<table tableName="sys_dept" domainObjectName="SysDept"><property name="useActualColumnNames" value="false"/><generatedKey identity="true" type="post" column="dept_id" sqlStatement="Mysql"/><columnOverride column="status" javaType="java.lang.Integer" jdbcType="INTEGER" /></table>
解决办法
数据库连接参数
在生成器的配置文件里的数据库连接地址中添加: nullCatalogMeansCurrent=true
1
2
3
4
5
6
7<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mbg?nullCatalogMeansCurrent=true"
userId="userName"
password="userPassword">
<!--MySQL 8.x 需要指定服务器的时区-->
<property name="serverTimezone" value="UTC"/>
</jdbcConnection>
- 或者在连接属性里添加:
<property name="nullCatalogMeansCurrent" value="true"/>
1
2
3
4
5
6
7
8<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mbg"
userId="userName"
password="userPassword">
<!--MySQL 8.x 需要指定服务器的时区-->
<property name="serverTimezone" value="UTC"/>
<property name="nullCatalogMeansCurrent" value="true"/>
</jdbcConnection>
在 table 配置项添加 catalog 属性
如:<table catalog="mbg" tableName="sys_dept" domainObjectName="SysDept"><generatedKey identity="true" type="post" column="id" sqlStatement="Mysql"/><columnOverride column="status" javaType="java.lang.Integer" jdbcType="INTEGER" /></table>
有关 MyBatis 的使用经验
请参考:MyBatis,文章目录