File tree Expand file tree Collapse file tree 1 file changed +29
-1
lines changed Expand file tree Collapse file tree 1 file changed +29
-1
lines changed Original file line number Diff line number Diff line change @@ -221,10 +221,38 @@ public class MultiTenantConnectionProviderImpl extends AbstractDataSourceBasedMu
221221 // 提供了tenantId的话就根据ID来返回数据源
222222 @Override
223223 protected DataSource selectDataSource (String tenantIdentifier ) {
224- System . out. println(" selectDataSource:" + tenantIdentifier);
225224 return TenantDataSourceProvider . getTenantDataSource(tenantIdentifier);
226225 }
227226}
227+ ```
228+ #### MultiTenantIdentifierResolver
229+ ``` java
230+ package cloud.tenant ;
231+
232+ import cloud.config.ConstId ;
233+ import org.hibernate.context.spi.CurrentTenantIdentifierResolver ;
234+
235+ /**
236+ * 这个类是由Hibernate提供的用于识别tenantId的类,当每次执行sql语句被拦截就会调用这个类中的方法来获取tenantId
237+ * @author lanyuanxiaoyao
238+ * @version 1.0
239+ */
240+ public class MultiTenantIdentifierResolver implements CurrentTenantIdentifierResolver {
241+
242+ // 获取tenantId的逻辑在这个方法里面写
243+ @Override
244+ public String resolveCurrentTenantIdentifier () {
245+ if (! " " . equals(ConstId . Id )){
246+ return ConstId . Id ;
247+ }
248+ return " Default" ;
249+ }
250+
251+ @Override
252+ public boolean validateExistingCurrentSessions () {
253+ return true ;
254+ }
255+ }
228256```
229257
230258 [ 1 ] : https://www.github.com/lanyuanxiaoyao/GitGallery/raw/master/2017/7/14/Spring%20Boot%EF%BC%88%E4%B8%89%EF%BC%89%20Spring%20boot%20+%20Hibernate%20%E5%A4%9A%E7%A7%9F%E6%88%B7%E7%9A%84%E4%BD%BF%E7%94%A8/Ashampoo_Snap_2017%E5%B9%B47%E6%9C%8814%E6%97%A5_12h27m50s_001_.png " 目录结构 "
You can’t perform that action at this time.
0 commit comments