栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Spring Boot JPA中的动态数据源

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Boot JPA中的动态数据源

这是我对DataSource的实现

public class DataSourceManager implements DataSource {    private Map<String, DataSource> dataSources = new HashMap<>();    private DataSource dataSource;    public DataSourceManager() {    }    public DataSourceManager(DataSource dataSource) {        this.dataSource = dataSource;    }    public void add(String name, DataSource dataSource) {        dataSources.put(name, dataSource);    }    public void switchDataSource(String name) {        dataSource = dataSources.get(name);    }    @Override    public PrintWriter getLogWriter() throws SQLException {        return dataSource.getLogWriter();    }    @Override    public void setLogWriter(PrintWriter out) throws SQLException {        dataSource.setLogWriter(out);    }    @Override    public void setLoginTimeout(int seconds) throws SQLException {        dataSource.setLoginTimeout(seconds);    }    @Override    public int getLoginTimeout() throws SQLException {        return dataSource.getLoginTimeout();    }    @Override    public Logger getParentLogger() throws SQLFeatureNotSupportedException {        return dataSource.getParentLogger();    }    @Override    public <T> T unwrap(Class<T> iface) throws SQLException {        return dataSource.unwrap(iface);    }    @Override    public boolean isWrapperFor(Class<?> iface) throws SQLException {        return dataSource.isWrapperFor(iface);    }    @Override    public Connection getConnection() throws SQLException {        return dataSource.getConnection();    }    @Override    public Connection getConnection(String username, String password) throws SQLException {        return dataSource.getConnection(username, password);    }}

这是我的配置

@Configurationpublic class DataSourceConfig {    @Autowired    private Environment env;    public DataSource makeDataSource(String name) {        return DataSourceBuilder.create()     .driverClassName(env.getProperty("spring.datasource." + name + ".driver-class-name"))     .url(env.getProperty("spring.datasource." + name + ".url")).build();    }    @Bean    public DataSource dataSource() {        DataSourceManager dataSourceManager = new DataSourceManager();        dataSourceManager.add("test1", makeDataSource("test1"));        dataSourceManager.add("test2", makeDataSource("test2"));        dataSourceManager.switchDataSource("test1");        return dataSourceManager;    }}

这是application.yml

spring:  jpa:    hibernate:      ddl-auto: create    properties:      hibernate:        dialect: org.hibernate.dialect.H2Dialect  datasource:    test1:      name: test2      url: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE      driver-class-name: org.h2.Driver      username: h2      password: h2    test2:      name: test1      url: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE      driver-class-name: org.h2.Driver      username: h2      password: h2


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/506877.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号