Spring mvc hibernate多租户数据库方法
Dispatcher-servlet.xml com.afflatus.edu.thoth.entity ${hibernate.dial} ${hibernate.show_sql} ${hibernate.dbm2ddl} 数据库 com.afflatus.edu.thoth.connection.MultiTenantConnectionProviderImpl com.afflatus.edu.thoth.context.MultiTenantIdentifier ResolveRimpl 这是我从中获取值的当前租户标识符代码 作为租户的spring上下文 这是主数据库源Spring mvc hibernate多租户数据库方法,hibernate,spring-mvc,spring-security,multi-tenant,Hibernate,Spring Mvc,Spring Security,Multi Tenant,Dispatcher-servlet.xml com.afflatus.edu.thoth.entity ${hibernate.dial} ${hibernate.show_sql} ${hibernate.dbm2ddl} 数据库 com.afflatus.edu.thoth.connection.MultiTenantConnectionProviderImpl com.afflatus.edu.thoth.context.MultiTenantIdentifier ResolveRim
package com.domain.master;
导入org.springframework.jdbc.datasource.driverManager数据源;
导入javax.sql.DataSource;
导入java.util.HashMap;
公共级万事达服务{
公共静态HashMap getDataSourceHashMap(){
DriverManager数据源dataSource=新的DriverManager数据源();
setDriverClassName(“com.mysql.jdbc.Driver”);
setUrl(“jdbc:mysql://localhost:3306/multiten");
dataSource.setUsername(“根”);
dataSource.setPassword(“根”);
DriverManager数据源dataSource1=新的DriverManager数据源();
dataSource1.setDriverClassName(“com.mysql.jdbc.Driver”);
dataSource1.setUrl(“jdbc:mysql://localhost:3306/multiten_1");
dataSource1.setUsername(“根”);
dataSource1.setPassword(“根”);
HashMap HashMap=新的HashMap();
hashMap.put(“tenantId1”,数据源);
hashMap.put(“kartiktamta@gmail.com“,数据源1);
返回hashMap;
}
}
使用springsecurity身份验证上下文获取值,并在登录geetting null指针错误后将其指定给租户。somenone能帮我吗先生请帮我
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="autodetectDataSource" value="false" />
<property name="sessionFactory" ref="sessionFactory" />
</bean
package com.domain.multitenancy;
import org.hibernate.context.spi.CurrentTenantIdentifierResolver;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
public class CurrentTenantIdentifierResolverimpl implements CurrentTenantIdentifierResolver {
@Override
public String resolveCurrentTenantIdentifier() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String tenantId = auth.getName();
if (tenantId==null){
return "tenantId1";
}
return tenantId;
}
@Override
public boolean validateExistingCurrentSessions() {
// TODO Auto-generated method stub
return false;
}
package com.domain.multitenancy;
import javax.sql.DataSource;
import
org.hibernate.engine.jdbc.connections.spi.AbstractDataSourceBasedMultiTenantConnectionProviderImpl;
import com.domain.master.MasterService;
public class MultiTenantConnectionprovideImpl extends
AbstractDataSourceBasedMultiTenantConnectionProviderImpl {
@Override
protected DataSource selectAnyDataSource() {
return MasterService.getDataSourceHashMap().get("tenantId1");
}
@Override
protected DataSource selectDataSource(String tenantIdentifier) {
return MasterService.getDataSourceHashMap().get(tenantIdentifier);
}
}
package com.domain.master;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource;
import java.util.HashMap;
public class MasterService {
public static HashMap<String, DataSource> getDataSourceHashMap() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/multiten");
dataSource.setUsername("root");
dataSource.setPassword("root");
DriverManagerDataSource dataSource1 = new DriverManagerDataSource();
dataSource1.setDriverClassName("com.mysql.jdbc.Driver");
dataSource1.setUrl("jdbc:mysql://localhost:3306/multiten_1");
dataSource1.setUsername("root");
dataSource1.setPassword("root");
HashMap<String, DataSource> hashMap = new HashMap<String, DataSource>();
hashMap.put("tenantId1", dataSource);
hashMap.put("kartiktamta@gmail.com", dataSource1);
return hashMap;
}
}