我们可以使用相同的存储库,模型

我想连接到同一数据库服务器上的两个数据库模式,为此,我只想使用一组模型和JPA存储库,而两个数据源则连接到两个不同的模式。但是到目前为止,我还没有能够找到一种可以重用现有模型和存储库的方法,现在,我已经创建了两组具有不同模式的模型和存储库。有没有一种方法可以重用模型和存储库?

但是到目前为止,我还无法找到一种方法来重用现有的模型和存储库,因为现在我已经创建了两组具有不同模式的模型和存储库

注意:

我能够通过两个数据源连接到两个模式,因此不需要多租户支持,唯一的事情是,当连接到两个独立的模式时,我创建模型(实体)和JPA存储库两次,即使表中的表相同两种模式都有,是否有删除代码重复项

beibeiji 回答:我们可以使用相同的存储库,模型

看起来您需要多租户支持。每个架构的租户

您需要TenantInterceptor来解析tenantId,例如来自会话或JWT令牌。还有MultiTenantConnectionProvider,它返回所需的提供程序。

来自Multi-Tenancy Implementation for Spring Boot + Hibernate Projects的代码

@Component
public class TenantInterceptor extends HandlerInterceptorAdapter {

    @Autowired  
    private JwtTokenUtil jwtTokenUtil;

    @Value("${jwt.header}")
    private String tokenHeader;

    @Override
    public boolean preHandle(HttpServletRequest request,HttpServletResponse response,Object handler)
            throws Exception {
        String authToken = request.getHeader(this.tokenHeader);
        String tenantId = jwtTokenUtil.getTenantIdFromToken(authToken);
        TenantContext.setCurrentTenant(tenantId);
        return true;
    }

    @Override
    public void postHandle(
            HttpServletRequest request,Object handler,ModelAndView modelAndView)
            throws Exception {
        TenantContext.clear();
    }
}

@Component
public class MultiTenantConnectionProviderImpl implements MultiTenantConnectionProvider {

    @Autowired
    private DataSource dataSource;

    @Override
    public Connection getAnyConnection() throws SQLException {
        return dataSource.getConnection();
    }

    @Override
    public void releaseAnyConnection(Connection connection) throws SQLException {
        connection.close();
    }

    @Override
    public Connection getConnection(String tenantIdentifie) throws SQLException {
        String tenantIdentifier = TenantContext.getCurrentTenant();
        final Connection connection = getAnyConnection();
        try {
            if (tenantIdentifier != null) {
                connection.createStatement().execute("USE " + tenantIdentifier);
            } else {
                connection.createStatement().execute("USE " + DEFAULT_TENANT_ID);
            }
        }
        catch ( SQLException e ) {
            throw new HibernateException(
                    "Problem setting schema to " + tenantIdentifier,e
            );
        }
        return connection;
    }

    @Override
    public void releaseConnection(String tenantIdentifier,Connection connection) throws SQLException {
        try {
            connection.createStatement().execute( "USE " + DEFAULT_TENANT_ID );
        }
        catch ( SQLException e ) {
            throw new HibernateException(
                   "Problem setting schema to " + tenantIdentifier,e
            );
        }
        connection.close();
    }

    @SuppressWarnings("rawtypes")
    @Override
    public boolean isUnwrappableAs(Class unwrapType) {
        return false;
    }

    @Override
    public <T> T unwrap(Class<T> unwrapType) {
        return null;
    }

    @Override
    public boolean supportsAggressiveRelease() {
        return true;
    }
}
本文链接:https://www.f2er.com/3118576.html

大家都在问