Jooq动态切换数据库

一般一个项目有多个数据库(database)的时候会用到这个功能。

主要代码:

    private static final String DEFAULT_DB = "jooqdb";

    private DataSource dataSource;

    public DSLContext getDSLContent(String dbName){
        Settings settings = new Settings();
        settings.withRenderSchema(true)
                .withRenderMapping(
                        new RenderMapping()
                        .withSchemata(
                                new MappedSchema()
                                        //jooq生成代码时的db
                                .withInput(DEFAULT_DB)
                                        //现在要使用的db
                                .withOutput(dbName)));
        return DSL.using(dataSource, SQLDialect.MYSQL,settings);
    }

withInput(DEFAULT_DB) 这里一定要设置你用jooq生成代码时的db,大小写也要注意区分。
withOutput(dbName) 这块是你要切换的新的数据库。

posted @ 2020-08-07 15:02  路要一步一步走  阅读(529)  评论(0编辑  收藏  举报