# multitenancy-demo-springboot-jpa **Repository Path**: hdl_1107/multitenancy-demo-springboot-jpa ## Basic Information - **Project Name**: multitenancy-demo-springboot-jpa - **Description**: 一个使用springboot+jpa实现多租户的小demo - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 3 - **Forks**: 6 - **Created**: 2018-02-10 - **Last Updated**: 2024-05-13 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # multitenancy-demo-springboot-jpa 一个使用springboot+jpa实现多租户的小demo 此文参考了作者Gerard Vico的代码,几乎只是加了一些注释和执行来增加自己的立理解,如有错误,请理解和指出,让我们不断的进步! https://github.com/HDBandit/multitenancy_springboot.git 实现结果:通过访问url时传入租户的唯一性标识,来区分此次访问所属租户的数据库,返回相关的数据信息 1、通过访问的url拿到租户的唯一性标识(这里使用拦截器中的HandlerMapping)来获取形如/{tenancyId}/xxxx/**中的租户标识,并将其存入request中,让后面可以使用。 2、配置租户的数据源,这里是一个租户一个数据源,并设置我们默认的租户,且默认租户只能为一个,数据源配置需要实现AbstractDataSourceBasedMultiTenantConnectionProviderImpl该抽象方法,还需要实现CurrentTenantIdentifierResolver来设置我们当前的租户(因为不仅仅只有默认的租户). 3、通过jpa来配置我们的LocalContainerEntityManagerFactoryBean,通过数据源集合和当前租户信息配置. 大致思路就是这样了,具体实现请看代码理解,代码基本已经鞋油注释。