Write in front
In business development, product sales information needs to be stored according to different channels. Since the data volume of a single channel is relatively large, it is not appropriate to store it in one table and it needs to be stored separately for each channel.
Code implementation
Define annotations and facets
Definition annotation
@Documented @Retention() @Target({}) public @interface DynamicTable { /** * Table name that needs to be divided * @return */ String tableName(); /** * Suffix key * @return */ String separateKey(); }
Sectional processing logic
@Aspect @Component @Slf4j public class DynamicTableAspect { /** * Used for SpEL expression parsing. */ private SpelExpressionParser parser = new SpelExpressionParser(); /** * Used to obtain method parameter definition name. */ private DefaultParameterNameDiscoverer nameDiscoverer = new DefaultParameterNameDiscoverer(); private static final String TABLE_NAME = "tableName"; private static final String TABLE_SUFFIX = "tableSuffix"; /** * Use the annotation as the cut point */ @Pointcut("@annotation(com.)") public void dynamicTable() { } @Around("dynamicTable()") public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable { MethodSignature methodSignature = (MethodSignature)(); Method method = (); if(()){ DynamicTable dynamicTable = (DynamicTable) (); String key = (); // Get parameters Object[] args = (); Expression expression = (key); // Use spring's DefaultParameterNameDiscoverer to get the method formal parameter array String[] paramNames = (method); // spring expression context object EvaluationContext context = new StandardEvaluationContext(); // Assign values to the context for (int i = 0; i < ; i++) { (paramNames[i], args[i]); } (new HashMap<String, Object>() {{ put(TABLE_NAME, ()); put(TABLE_SUFFIX, (context)); }}); Object proceed = (); (); return proceed; }else{ Object proceed = (); return proceed; } } }
mybatis interceptor
The above code contains the following code in the processing section logic:
(new HashMap<String, Object>() {{ put(TABLE_NAME, ()); put(TABLE_SUFFIX, (context)); }}); Object proceed = (); ();
This paragraph is actually set up the upper and lower parts in the code logic. After the section logic is processed, the context content will be removed. The purpose is to use the context content of the current request for the mybatis interceptor.
The internal use of mubatis dynamic table name, the content is as follows:
@Configuration @Slf4j public class MyBatisPlusConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor(){ MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor(); // Add a paging plugin (new PaginationInnerInterceptor()); // Dynamic table name plugin DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor(); ((sql, tableName) -> { // Get parameter method Map<String, Object> paramMap = (); if ((paramMap)) { var tableNameParam = (String) ("tableName"); ((k, v) -> (k + "----" + v)); if((tableName)){ // Get the passed parameters String tableSuffix = (String) ("tableSuffix"); if((tableSuffix)){ return tableName; }else{ // Assembly dynamic table name return tableName + "_" + tableSuffix; } } } return tableName; }); (dynamicTableNameInnerInterceptor); return mybatisPlusInterceptor; } }
This is the end of this article about Java's detailed explanation of subtables through annotations. For more related content of Java subtables, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!