import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class DBUtil {
@Value("${spring.datasource.url}")
private String dbUrl;
public String getDbName() {
String dbName = null;
if (dbUrl != null) {
int index = dbUrl.lastIndexOf("/");
if (index != -1) {
dbName = dbUrl.substring(index + 1); // 获取最后一个斜杠后面的内容
}
}
return dbName;
}
}
第二种:
JdbcTemplate jdbcTemplate = SpringUtils.getBean(JdbcTemplate.class);
jdbcTemplate.getDataSource().getConnection().getCatalog();