Cannot resolve method 'setBiao' in 'DruidDataSource' 怎么在DruidDataSource中加入setBiao
时间: 2023-10-24 14:16:04 浏览: 98
DruidDataSource是阿里巴巴开发的数据库连接池,它是基于Apache Commons Pool2实现的。在DruidDataSource中没有setBiao方法,因为该方法不是DruidDataSource类中的方法。
如果您需要在DruidDataSource中设置表名,可以通过以下方式实现:
1. 设置URL属性
您可以在URL中指定表名,如下所示:
```java
String url = "jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&autoReconnect=true&failOverReadOnly=false&maxReconnects=10&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true&allowMultiQueries=true&rewriteBatchedStatements=true&useServerPrepStmts=true&cachePrepStmts=true&prepStmtCacheSize=250&prepStmtCacheSqlLimit=2048&useLocalSessionState=true&useLocalTransactionState=true&sessionVariables=sql_mode='NO_ENGINE_SUBSTITUTION'&tableName=my_table";
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(url);
```
在URL中指定表名为"my_table"。这种方式比较简单,但是如果需要在不同的地方使用不同的表名,就需要修改URL了。
2. 使用连接池的拦截器
DruidDataSource支持使用拦截器来拦截获取连接和执行SQL语句的操作,我们可以在拦截器中动态修改SQL语句中的表名。示例代码如下:
```java
public class TableInterceptor implements Interceptor {
private ThreadLocal<String> threadLocal = new ThreadLocal<>();
public void setTable(String table) {
threadLocal.set(table);
}
public void clearTable() {
threadLocal.remove();
}
@Override
public Object intercept(Invocation invocation) throws Throwable {
if (invocation.getTarget() instanceof StatementHandler) {
StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
MetaObject metaObject = MetaObject.forObject(statementHandler, DEFAULT_OBJECT_FACTORY, DEFAULT_OBJECT_WRAPPER_FACTORY, new DefaultReflectorFactory());
MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
if (mappedStatement.getSqlCommandType() == SqlCommandType.SELECT) {
String originalSql = (String) metaObject.getValue("delegate.boundSql.sql");
String newSql = originalSql.replace("my_table", threadLocal.get());
metaObject.setValue("delegate.boundSql.sql", newSql);
}
}
return invocation.proceed();
}
@Override
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
@Override
public void setProperties(Properties properties) {
}
}
```
使用拦截器需要先创建一个TableInterceptor,然后在需要修改表名的地方调用setTable方法,设置要使用的表名。例如:
```java
DruidDataSource dataSource = new DruidDataSource();
TableInterceptor interceptor = new TableInterceptor();
dataSource.setFilters("stat,wall,log4j");
dataSource.setConnectionProperties("druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500");
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&autoReconnect=true&failOverReadOnly=false&maxReconnects=10&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true&allowMultiQueries=true&rewriteBatchedStatements=true&useServerPrepStmts=true&cachePrepStmts=true&prepStmtCacheSize=250&prepStmtCacheSqlLimit=2048&useLocalSessionState=true&useLocalTransactionState=true&sessionVariables=sql_mode='NO_ENGINE_SUBSTITUTION'");
dataSource.setUsername("root");
dataSource.setPassword("123456");
interceptor.setTable("my_table");
dataSource.setProxyFilters(Lists.newArrayList(interceptor));
```
以上示例中,我们创建了一个TableInterceptor,并将其设置为DruidDataSource的拦截器。然后在需要修改表名的地方调用setTable方法设置表名。注意,这种方式只能修改SELECT语句的表名,如果需要修改其他类型的SQL语句的表名,需要修改intercept方法中的代码。
阅读全文