About the Bug...

  • [x] I have checked that this issue has not already been reported.

  • [x] I have confirmed this bug exists on the latest version of MyBatis.

  • [x] I have confirmed this bug reproduces without 3rd party extensions (e.g. mybatis-plus).

Database Version

mysql 8

JDBC Driver Version

mysql 8

Issue Description

when I use databaseId to adapt to multiple databases, such as mysql,postgrel and oracle, I have three groups of mapper.xml, one for mysql(/mapper/xxx.xml),one for postgre(/mapper/postgre/xxx.xml),one for oracle(/mapper/oracle/xxx.xml). As my project was based on mysql previously, I want to use mapper.xml of mysql as
shared sqls, that means I would not add 'databaseId' to sqls in /mapper/xxx.xml, and /mapper/postgre/xxx.xml and /mapper/oracle/xxx.xml would only contain conflicted sqls which have been adapted to correspondent databases with databaseId. However, I find that it doesn't work. If I want to use sqls of mysql as default sqls, the three groups of sqls must be put into one single mapper.xml. When I separate them into three mapper.xml( for readability), it will throw exceptions of sqlid conflict. It seems that the following code lead to the problem: private boolean databaseIdMatchesCurrent(String id, String databaseId, String requiredDatabaseId) { if (requiredDatabaseId != null) { return requiredDatabaseId.equals(databaseId); } else if (databaseId != null) { return false; } else { id = this.builderAssistant.applyCurrentNamespace(id, false); if (!this.configuration.hasStatement(id, false)) { return true; } else { MappedStatement previous = this.configuration.getMappedStatement(id, false); return previous.getDatabaseId() == null; } } } for each mapper.xml, the code is executed twice, firstly with non-null requiredDatabaseId, secondly with null requiredDatabaseId. For example ,mysql(/mapper/xxx.xml) contains a sql with sqlid 'selectTest', it's compatible with postgre but conflicts with oracle, so I put a modified sql with databaseId="oracle" into oracle(/mapper/oracle/xxx.xml) . If the parsing order is mysql(/mapper/xxx.xml),postgre(/mapper/postgre/xxx.xml),oracle(/mapper/oracle/xxx.xml), when it's parsing mysql(/mapper/xxx.xml), the sql with sqlid 'selectTest' would be put into statementmap via databaseIdMatchesCurrent with null requiredDatabaseId. However, when it's parsing oracle(/mapper/oracle/xxx.xml), databaseIdMatchesCurrent with non-null requiredDatabaseId returns true for sql 'selectTest', so it try to put the sql into statementmap, then the conflict occurs. Is there any way to solve this problem?

About your report...

  • [x] I did not use images 🖼️ for showing text information (code, error, etc.).

  • [x] I checked the Preview and my report looks awesome! 👍