ShardingJDBC 报错 Table or view does not exist
ShardingJDBC无法找到单表
依赖版本
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-test-util</artifactId>
</exclusion>
</exclusions>
<version>5.5.0</version>
</dependency>
出现异常
Caused by: org.apache.shardingsphere.infra.exception.kernel.metadata.TableNotFoundException: Table or view 'xxxxxxx' does not exist.
at org.apache.shardingsphere.infra.binder.segment.from.impl.SimpleTableSegmentBinder.lambda$checkTableExists$4(SimpleTableSegmentBinder.java:151) ~[shardingsphere-infra-binder-5.5.0.jar:5.5.0]
at org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions.checkState(ShardingSpherePreconditions.java:41) ~[shardingsphere-infra-exception-core-5.5.0.jar:5.5.0]
at org.apache.shardingsphere.infra.binder.segment.from.impl.SimpleTableSegmentBinder.checkTableExists(SimpleTableSegmentBinder.java:148) ~[shardingsphere-infra-binder-5.5.0.jar:5.5.0]
at org.apache.shardingsphere.infra.binder.segment.from.impl.SimpleTableSegmentBinder.bind(SimpleTableSegmentBinder.java:84) ~[shardingsphere-infra-binder-5.5.0.jar:5.5.0]
at org.apache.shardingsphere.infra.binder.segment.from.TableSegmentBinder.bind(TableSegmentBinder.java:55) ~[shardingsphere-infra-binder-5.5.0.jar:5.5.0]
at org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementBinder.lambda$bind$1(SelectStatementBinder.java:60) ~[shardingsphere-infra-binder-5.5.0.jar:5.5.0]
at java.base/java.util.Optional.map(Optional.java:260) ~[na:na]
at org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementBinder.bind(SelectStatementBinder.java:60) ~[shardingsphere-infra-binder-5.5.0.jar:5.5.0]
at org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementBinder.bind(SelectStatementBinder.java:48) ~[shardingsphere-infra-binder-5.5.0.jar:5.5.0]
at org.apache.shardingsphere.infra.binder.engine.SQLBindEngine.bindDMLStatement(SQLBindEngine.java:82) ~[shardingsphere-infra-binder-5.5.0.jar:5.5.0]
at org.apache.shardingsphere.infra.binder.engine.SQLBindEngine.bind(SQLBindEngine.java:72) ~[shardingsphere-infra-binder-5.5.0.jar:5.5.0]
at org.apache.shardingsphere.infra.binder.engine.SQLBindEngine.bind(SQLBindEngine.java:63) ~[shardingsphere-infra-binder-5.5.0.jar:5.5.0]
at org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSpherePreparedStatement.<init>(ShardingSpherePreparedStatement.java:207) ~[shardingsphere-jdbc-5.5.0.jar:5.5.0]
at org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSpherePreparedStatement.<init>(ShardingSpherePreparedStatement.java:172) ~[shardingsphere-jdbc-5.5.0.jar:5.5.0]
at org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection.prepareStatement(ShardingSphereConnection.java:94) ~[shardingsphere-jdbc-5.5.0.jar:5.5.0]
at com.zaxxer.hikari.pool.ProxyConnection.prepareStatement(ProxyConnection.java:327) ~[HikariCP-5.0.1.jar:na]
at com.zaxxer.hikari.pool.HikariProxyConnection.prepareStatement(HikariProxyConnection.java) ~[HikariCP-5.0.1.jar:na]
at org.apache.ibatis.executor.statement.PreparedStatementHandler.instantiateStatement(PreparedStatementHandler.java:88) ~[mybatis-3.5.16.jar:3.5.16]
at org.apache.ibatis.executor.statement.BaseStatementHandler.prepare(BaseStatementHandler.java:90) ~[mybatis-3.5.16.jar:3.5.16]
... 103 common frames omitted
解决方案
在ShardingJDBC的配置文件中添加以下配置:
# 规则配置
rules:
- !SINGLE
tables:
# 加载全部单表
- "ds0.*"
ds0替换成你单表所在的数据源即可
作者:初衷不改~