In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail the example analysis of mybatis-plus dynamic table names. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Realize
In MP, dynamic table name resolution is completed through PaginationInterceptor (paging plug-in). The configuration is as follows:
Dependent on com.baomidou mybatis-plus-boot-starter 3.3.1.tmp configuration class package com.huanchuang.common.config; import com.baomidou.mybatisplus.extension.parsers.DynamicTableNameParser;import com.baomidou.mybatisplus.extension.parsers.ITableNameHandler;import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;import org.springframework.context.annotation.Bean Import org.springframework.context.annotation.Configuration; import java.util.Collections;import java.util.HashMap / * * @ Package: com.huanchuang.common.config * @ Description: * @ Author: MILLA * @ CreateDate: 2020-09-04 14:42 * @ UpdateUser: MILLA * @ UpdateDate: 2020-09-04 14:42 * @ UpdateRemark: * @ Version: 1.0 * / @ Configuration@MapperScan ("com.huanchuang.ext.mapper**") @ ConditionalOnProperty (prefix = "spring.config", name = "enableMybatisPlusDynamicTable" HavingValue = "true") public class MybatisPlusDynamicTableConfig {private static final String DYNAMIC_TABLE_PRE = "common_user" / * mybatis-plus paging plug-in * / @ Bean public PaginationInterceptor paginationInterceptor () {PaginationInterceptor paginationInterceptor = new PaginationInterceptor (); / / set the operation after the requested page is larger than the maximum page, true is called back to the home page, and false continues to request the default false paginationInterceptor.setOverflow (false); / / set the maximum number of page limits, default to 500s,-1 unlimited paginationInterceptor.setLimit / / enable join optimization of count, only for some left join paginationInterceptor.setCountSqlParser (new JsqlParserCountOptimize (true)); DynamicTableNameParser dynamicTableNameParser = new DynamicTableNameParser (); dynamicTableNameParser.setTableNameHandlerMap (new HashMap (2) {{/ / dynamic table rules-generate the dynamic table name put (DYNAMIC_TABLE_PRE, (metaObject, sql, tableName)-> DynamicTableTreadLocal.INSTANCE.getTableName ();}}); paginationInterceptor.setSqlParserList (Collections.singletonList (dynamicTableNameParser)); return paginationInterceptor }} dynamic table name storage class package com.huanchuang.common.config; / * * @ Package: com.huanchuang.common.config * @ Description: * @ Author: MILLA * @ CreateDate: 2020-09-04 14:42 * @ UpdateUser: MILLA * @ UpdateDate: 2020-09-04 14:42 * @ UpdateRemark: * @ Version: 1.0 * / public enum DynamicTableTreadLocal {INSTANCE; private ThreadLocal tableName = new ThreadLocal (); public String getTableName () {return tableName.get () } public void setTableName (String tableName) {this.tableName.set (tableName);} public void remove () {tableName.remove ();}}
Use
Private void select (int year) {DynamicTableTreadLocal.INSTANCE.setTableName ("user_" + year); LambdaQueryWrapper wrapper = Wrappers.lambdaQuery (User.class); List userList = userMapper.list (wrapper)} @ Datapublic class User {private Long id; private String userName; private String address; private char sex; private Byte age } this is the end of the article on "sample Analysis of mybatis-plus dynamic Table names". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.