In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "the actual combat tread pit analysis of Mybatis plus multi-tenant scheme". In the daily operation, I believe that many people have doubts about the actual combat tread pit analysis of Mybatis plus multi-tenant scheme. The editor has consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "actual combat tread pit analysis of Mybatis plus multi-tenant scheme". Next, please follow the editor to study!
(1). Scheme
There are many solutions on the Internet, but this article only writes the last one, that is, add tenant id to the table to achieve data isolation.
Option 1: add tenant id, and manually add tenant id to every place where mapper is called
For example:
LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper (); lambdaQueryWrapper.eq (Entity::getTenantId, "tenantId"); entityMapper.selectList (lambdaQueryWrapper)
This method is complex, heavy workload, and easy to leak. Not adopted
Solution 2: use the official multi-tenant plug-in of mp. The code is omitted here, and you can query it on the official documents yourself.
(2) Optimization and pitfalls of the official multi-tenant scheme
After adopting the official multi-tenant plug-in, the early debugging went smoothly, and the crud tested itself and thought there was no problem, so the test environment was sent. However, with the deepening of the test, a lot of problems and areas that need to be changed are found. Here is a list:
1. Analyze which need to add multi-tenancy and which do not.
(1) rewriting of tenant id
The official default rewriting method is:
@ Overridepublic Expression getTenantId () {return null;}
You need to define how to obtain your tenant's id here.
(2) definition of tenant field
Private static final String TENANT_ID = "tenant_id"; @ Overridepublic String getTenantIdColumn () {return TENANT_ID;}
(3) tenant interception
@ Overridepublic boolean ignoreTable (String tableName) {return TenantLineHandler.super.ignoreTable (tableName);}
The scheme I use here is table name interception, and the code is as follows:
@ Overridepublic boolean ignoreTable (String tableName) {/ * the list here is temporarily used as an intercept * because: the following table parsing method uses a large hump to underline, and then compares it with the table name intercepted by the sql interceptor. If it matches, it is considered that the table needs multi-tenant splicing * but some tables are not strictly underlined according to the big hump. So these tables need an additional definition of whether the annotation * @ TableName can do this job has not been tested yet, and we'll talk about it later. * / List list = new ArrayList (); list.add ("das_standard_operation"); list.add ("t_expert"); list.add ("t_nominate_dict"); list.add ("t_nominate_dict_history"); list.add ("t_order"); list.add ("t_standard_sort"); list.add ("t_task"); list.add ("t_task_confirm") List.add ("das_view"); if (list.contains (tableName)) {return false;} EntityTableCache instance = EntityTableCache.getInstance (); if (null = = instance | | null = = instance.getCacheData (tableName)) {/ / if not initialized, do not splice the tenant id return true;} String entityPath = EntityTableCache.getInstance (). GetCacheData (tableName). ToString () / / this method converts the big hump to an underscore and initializes return! EntityUtils.isHaveAttr (entityPath, COLUMN_TENANTID);}
The code of EntityUtils method is as follows (written by a young man on github, please contact me to delete it)
/ * determine whether an entity has an attribute * * @ param entityPath entity full path * @ param attrName attribute name * @ return boolean * / public static boolean isHaveAttr (String entityPath, String attrName) {Optional epOptional = Optional.ofNullable (entityPath); if (! epOptional.isPresent ()) {return false;} try {Class aClass = Thread.currentThread (). GetContextClassLoader (). LoadClass (epOptional.get ()) Field [] fields = aClass.getDeclaredFields (); for (Field field: fields) {if (attrName.equals (field.getName () {return true;}} return false;} catch (ClassNotFoundException e) {/ / log.error ("SystemSqlParser- > isHaveAttr class load exception:" + e.getMessage ()); return false }} 2.jsqlparser this package does not match the pagehelper version
During the self-test, it was found that the update statement could not be stopped, and it was found that the version number of jsqlparser was 1.2 and 1.2 and 2.0 (the jspparser version of mp3.4.1 was 2.0). Through debug, it was found that the update method of 1.2 was entered.
As shown in the figure:
Version 1.2 is getTables (), while version 2.0 is getTable () as shown.
Solution:
Parent pom mandatory version
Com.github.jsqlparser jsqlparser 2.0 com.github.pagehelper pagehelper 5.1.10 3.sql parsing failed
1.regexp
At present, it is found that the parser will report an error if the sql statement with regexp > 0 is used.
2.replace into statement
At present, I find that this sentence is not supported either.
The above two problems have not been solved yet. I read some materials and asked some colleagues. There is little contact with this piece. At present, the version of com.github.jsqlparser:jsqlparser used in the latest version of mp 3.4.3.4 is version 4.2. At present, the above two items are still not supported. (if there is a big god to solve, please comment and guide! )
Solution method
Since you failed, then I don't need you, manual splicing, see the following ignore method ⬇️
4. Ignore multi-tenancy does not take effect
We all know that with the annotation @ InterceptorIgnore (tenantLine = "on"), you can achieve this mapper statement without sql parsing and multi-tenancy modification. However, in the actual application scenario, it is found that there is a special scenario in which the annotation does not take effect.
For example:
Page page = PageHelper.startPage (param.getPageNumber (), param.getPageSize ()); List experts = expertMapper.queryExpertList (page)
This question really wasted my time for a long time, and then through consulting the data, I found that @ InterceptorIgnore will fail when there are pages, but I also thought, it is all mp things, you are conflicting, how can that work?
Then I found PageHelper, which looks very strange. Then I got rid of it, and I didn't page it, and it was fine! The comments are in effect. Perfect, at the same time, mp has provided paging things, why rely on com.github?
Then the transformation is as follows:
IPage page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page (param.getPageNumber (), param.getPageSize ()) List experts = expertMapper.queryExpertList (expertMapping.dtoToEntity (param)); at this point, the study on "the actual trampling analysis of the Mybatis plus multi-tenant scheme" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.