mybatis-generator生成器问题

使用生成器生成代码时遇到的问题:出现多出来的字段,某些字段反而没有

Reference

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

(该问题主要出现在MySQL8中)

参考:MyBatis-MySQL Usage Notes

解决办法:添加参数”nullCatalogMeansCurrent=true”到JDBC URL

例如:

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

完整配置文件如下:

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
41
42
43
44
<?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>

<context id="MysqlContext" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>

<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<property name="mappers" value="com.utils.MyMapper"/>
</plugin>

<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/muxin-dev"
userId="******"
password="******">
<!--MySQL8需要指定服务器的时区-->
<property name="serverTimezone" value="UTC"/>
<!--MySQL 不支持 schema 或者 catalog -->
<property name="nullCatalogMeansCurrent" value="true"/>
<!-- MySQL8默认启用 SSL ,不关闭会有警告-->
<property name="useSSL" value="false"/>
</jdbcConnection>

<!-- 对于生成的pojo所在包 -->
<javaModelGenerator targetPackage="com.pojo" targetProject="src/main/java"/>

<!-- 对于生成的mapper所在目录 -->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"/>

<!-- 配置mapper对应的java映射 -->
<javaClientGenerator targetPackage="com.mapper" targetProject="src/main/java" type="XMLMAPPER"/>


<table tableName="users"></table>
<table tableName="friends"></table>
<table tableName="request"></table>
<table tableName="chat"></table>

</context>
</generatorConfiguration>