java.lang.Object
org.miaixz.bus.mapper.handler.AbstractSqlHandler
org.miaixz.bus.pager.handler.SqlParserHandler
org.miaixz.bus.pager.handler.ConditionHandler
org.miaixz.bus.pager.handler.TenantHandler
- All Implemented Interfaces:
Serializable,org.miaixz.bus.core.Handler,org.miaixz.bus.mapper.handler.MapperHandler
public class TenantHandler
extends ConditionHandler
implements org.miaixz.bus.mapper.handler.MapperHandler
Multi-tenancy handler, responsible for adding tenant conditions to SQL statements. This handler intercepts various
SQL operations (SELECT, INSERT, UPDATE, DELETE) and modifies them to include tenant-specific filtering or data
manipulation based on the configured
TenantProvider.- Since:
- Java 17+
- Author:
- Kimi Liu
- See Also:
-
Field Summary
Fields inherited from class org.miaixz.bus.mapper.handler.AbstractSqlHandler
DEFAULT_REFLECTOR_FACTORY, DELEGATE_BOUNDSQL, DELEGATE_BOUNDSQL_SQL, DELEGATE_MAPPEDSTATEMENT, MAPPEDSTATEMENT -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidappendSelectItem(List<net.sf.jsqlparser.statement.select.SelectItem<?>> selectItems) Appends the tenant column to the SELECT clause of a SELECT statement.net.sf.jsqlparser.expression.ExpressionbuildTableExpression(net.sf.jsqlparser.schema.Table table, net.sf.jsqlparser.expression.Expression where, String segment) Builds the tenant condition expression (e.g., `tenant_id = ?protected net.sf.jsqlparser.schema.ColumngetAliasColumn(net.sf.jsqlparser.schema.Table table) Retrieves the tenant column name, optionally prefixed with the table alias.Retrieves the tenant service provider.voidprepare(org.apache.ibatis.executor.statement.StatementHandler statementHandler) Pre-processes INSERT, UPDATE, and DELETE statements to add tenant conditions.protected voidprocessDelete(net.sf.jsqlparser.statement.delete.Delete delete, int index, String sql, Object object) Processes a DELETE statement, adding tenant conditions to its WHERE clause.protected voidprocessInsert(net.sf.jsqlparser.statement.insert.Insert insert, int index, String sql, Object object) Processes an INSERT statement, adding tenant columns and values.protected voidprocessInsertSelect(net.sf.jsqlparser.statement.select.Select select, String segment) Processes an INSERT INTO ...protected voidprocessSelect(net.sf.jsqlparser.statement.select.Select select, int index, String sql, Object object) Processes a SELECT statement, adding tenant conditions to its WHERE clause.protected voidprocessUpdate(net.sf.jsqlparser.statement.update.Update update, int index, String sql, Object object) Processes an UPDATE statement, adding tenant conditions to its WHERE clause.voidquery(Object object, org.apache.ibatis.executor.Executor executor, org.apache.ibatis.mapping.MappedStatement mappedStatement, Object parameter, org.apache.ibatis.session.RowBounds rowBounds, org.apache.ibatis.session.ResultHandler resultHandler, org.apache.ibatis.mapping.BoundSql boundSql) Handles query operations by adding tenant conditions to SELECT statements.booleansetProperties(Properties properties) Sets the properties for the handler, initializing the tenant service provider.voidsetProvider(TenantProvider provider) Sets the tenant service provider.Methods inherited from class org.miaixz.bus.pager.handler.ConditionHandler
andExpression, appendExpression, builderExpression, processFunction, processOtherFromItem, processPlainSelect, processSelectBody, processSelectItem, processWhereSubSelectMethods inherited from class org.miaixz.bus.pager.handler.SqlParserHandler
parse, parserMulti, parserSingle, parseStatements, processParser, validateSqlMethods inherited from class org.miaixz.bus.mapper.handler.AbstractSqlHandler
getMappedStatement, getMappedStatement, getMetaObject, getSqlParserInfo, mapperBoundSql, mapperStatementHandler, realTarget, setAdditionalParameterMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.miaixz.bus.core.Handler
after, beforeMethods inherited from interface org.miaixz.bus.mapper.handler.MapperHandler
getBoundSql, isQuery, isUpdate, update
-
Constructor Details
-
TenantHandler
public TenantHandler()
-
-
Method Details
-
query
public void query(Object object, org.apache.ibatis.executor.Executor executor, org.apache.ibatis.mapping.MappedStatement mappedStatement, Object parameter, org.apache.ibatis.session.RowBounds rowBounds, org.apache.ibatis.session.ResultHandler resultHandler, org.apache.ibatis.mapping.BoundSql boundSql) Handles query operations by adding tenant conditions to SELECT statements. The SQL is parsed and modified to include tenant-specific WHERE clauses.- Specified by:
queryin interfaceorg.miaixz.bus.mapper.handler.MapperHandler- Parameters:
object- The result object (unused).executor- The MyBatis executor.mappedStatement- The MappedStatement.parameter- The query parameters.rowBounds- The pagination parameters.resultHandler- The result handler.boundSql- The BoundSql object.
-
prepare
public void prepare(org.apache.ibatis.executor.statement.StatementHandler statementHandler) Pre-processes INSERT, UPDATE, and DELETE statements to add tenant conditions. This method is called before the statement is executed.- Specified by:
preparein interfaceorg.miaixz.bus.mapper.handler.MapperHandler- Parameters:
statementHandler- The StatementHandler.
-
processSelect
protected void processSelect(net.sf.jsqlparser.statement.select.Select select, int index, String sql, Object object) Processes a SELECT statement, adding tenant conditions to its WHERE clause.- Overrides:
processSelectin classSqlParserHandler- Parameters:
select- The SELECT statement.index- The statement index (unused).sql- The original SQL string.object- The WHERE clause segment (as a string).
-
processInsert
protected void processInsert(net.sf.jsqlparser.statement.insert.Insert insert, int index, String sql, Object object) Processes an INSERT statement, adding tenant columns and values.- Overrides:
processInsertin classSqlParserHandler- Parameters:
insert- The INSERT statement.index- The statement index (unused).sql- The original SQL string.object- The WHERE clause segment (unused).- Throws:
org.miaixz.bus.core.lang.exception.InternalException- if multi-table update is attempted without proper exclusion.
-
processUpdate
protected void processUpdate(net.sf.jsqlparser.statement.update.Update update, int index, String sql, Object object) Processes an UPDATE statement, adding tenant conditions to its WHERE clause.- Overrides:
processUpdatein classSqlParserHandler- Parameters:
update- The UPDATE statement.index- The statement index (unused).sql- The original SQL string.object- The WHERE clause segment (as a string).
-
processDelete
protected void processDelete(net.sf.jsqlparser.statement.delete.Delete delete, int index, String sql, Object object) Processes a DELETE statement, adding tenant conditions to its WHERE clause.- Overrides:
processDeletein classSqlParserHandler- Parameters:
delete- The DELETE statement.index- The statement index (unused).sql- The original SQL string.object- The WHERE clause segment (as a string).
-
processInsertSelect
protected void processInsertSelect(net.sf.jsqlparser.statement.select.Select select, String segment) Processes an INSERT INTO ... SELECT statement, ensuring the SELECT subquery includes tenant conditions.- Parameters:
select- The SELECT statement body.segment- The WHERE clause segment (as a string).
-
appendSelectItem
Appends the tenant column to the SELECT clause of a SELECT statement.- Parameters:
selectItems- The list of select items in the SELECT clause.
-
getAliasColumn
protected net.sf.jsqlparser.schema.Column getAliasColumn(net.sf.jsqlparser.schema.Table table) Retrieves the tenant column name, optionally prefixed with the table alias.- Parameters:
table- The Table object.- Returns:
- The tenant column as a
Columnobject (e.g., tenantId or tableAlias.tenantId).
-
setProperties
Sets the properties for the handler, initializing the tenant service provider.- Specified by:
setPropertiesin interfaceorg.miaixz.bus.core.Handler- Parameters:
properties- The configuration properties.- Returns:
trueif properties were set successfully.
-
buildTableExpression
public net.sf.jsqlparser.expression.Expression buildTableExpression(net.sf.jsqlparser.schema.Table table, net.sf.jsqlparser.expression.Expression where, String segment) Builds the tenant condition expression (e.g., `tenant_id = ?`).- Specified by:
buildTableExpressionin classConditionHandler- Parameters:
table- The Table object.where- The current WHERE condition.segment- The full Mapper path (unused).- Returns:
- The tenant condition expression, or
nullif the table is ignored.
-
getProvider
Retrieves the tenant service provider.- Returns:
- The
TenantProviderinstance.
-
setProvider
Sets the tenant service provider.- Parameters:
provider- TheTenantProviderinstance to set.
-