Spring Boot はデータベース名を取得します

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;
    }
}

 2 番目のタイプ:

JdbcTemplate jdbcTemplate = SpringUtils.getBean(JdbcTemplate.class);
jdbcTemplate.getDataSource().getConnection().getCatalog();

おすすめ

転載: blog.csdn.net/qq_22905801/article/details/130629498