1. 程式人生 > >mybatis generator生成對映檔案時,執行多次,導致xml檔案生成錯誤,設定table schema也無效

mybatis generator生成對映檔案時,執行多次,導致xml檔案生成錯誤,設定table schema也無效

在maven中集成了MBG以後,生成xml等對映檔案時,會生成多次。由於xml檔案是追加,導致xml檔案有多次生成的結果。

原因:

1):在資料庫伺服器上,不同的資料庫中表名相同的表多張。(有幾個同名的表,就會生成幾次)

2):mysql驅動升級到8.x,造成設定schema無效。

解決方法:

jdbc連線新增nullCatalogMeansCurrent屬性:

<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/db?useUnicode=true&amp;characterEncoding=utf-8"
                        userId="root" password="root">
            <property name="nullCatalogMeansCurrent" value="true" />
        </jdbcConnection>

Catalogs and Schema

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.