学习Mybatis时,使用MySQL8.x版本进行逆向工程出现多余文件的问题


在使用MySQL8.x版本进行MyBatis逆向工程时,测试时发现提示column不存在,即在Mapper.xml映射文件中的SQL语句不正确,映射文件中的字段与数据库中的不一致,但是创建的pojo类正确。而且在pojo和mapper包中不仅生成了如User.java和UserMapper.java的文件,还生成了User.java.1和UserMapper.java.1文件。

最后查明问题出在MySQL版本的问题上,根据官方文档:

1
2
3
4
5
6
7
<!--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>

在逆向文件generatorConfig.xml中的标签中加入即可。

最终经测试可以生成正确文件,正确进行数据库操作。


  TOC