| | |
| | | package com.vincent.rsf.server.common.config; |
| | | |
| | | import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer; |
| | | import com.baomidou.mybatisplus.core.MybatisConfiguration; |
| | | import com.baomidou.mybatisplus.extension.MybatisMapWrapperFactory; |
| | | import com.baomidou.mybatisplus.extension.parser.JsqlParserGlobal; |
| | | import com.baomidou.mybatisplus.extension.parser.cache.JdkSerialCaffeineJsqlParseCache; |
| | | import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; |
| | | import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler; |
| | | import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor; |
| | | import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; |
| | | import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor; |
| | | import com.vincent.rsf.server.system.entity.User; |
| | | import net.sf.jsqlparser.expression.Expression; |
| | | import net.sf.jsqlparser.expression.LongValue; |
| | | import net.sf.jsqlparser.expression.NullValue; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.Configuration; |
| | | import org.springframework.security.core.Authentication; |
| | | import org.springframework.security.core.context.SecurityContextHolder; |
| | | import org.springframework.transaction.annotation.EnableTransactionManagement; |
| | | |
| | | import java.util.Arrays; |
| | | import java.util.concurrent.LinkedBlockingQueue; |
| | | import java.util.concurrent.ThreadPoolExecutor; |
| | | import java.util.concurrent.TimeUnit; |
| | | import java.util.concurrent.atomic.AtomicInteger; |
| | | |
| | | /** |
| | | * MybatisPlus配置 |
| | |
| | | * @since 2018-02-22 11:29:28 |
| | | */ |
| | | @Configuration |
| | | @EnableTransactionManagement |
| | | public class MybatisPlusConfig { |
| | | |
| | | private static volatile boolean jsqlParserConfigured = false; |
| | | |
| | | @Bean |
| | | public MybatisPlusInterceptor mybatisPlusInterceptor() { |
| | | configureJsqlParser(); |
| | | MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); |
| | | |
| | | // 添加乐观锁插件 |
| | | interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor()); |
| | | |
| | | // 多租户插件配置 |
| | | TenantLineHandler tenantLineHandler = new TenantLineHandler() { |
| | |
| | | "sys_host", |
| | | "sys_user_role", |
| | | "sys_role_menu", |
| | | "sys_config" |
| | | "sys_menu", |
| | | "sys_pda_role_menu", |
| | | "sys_menu_pda", |
| | | "sys_matnr_role_menu", |
| | | "sys_warehouse_role_menu", |
| | | "man_loc_type_rela", |
| | | "man_qly_inspect_result", |
| | | "view_stock_manage", |
| | | "view_stock_statistic", |
| | | "man_transfer_order", |
| | | "man_wave_order_rela" |
| | | ).contains(tableName); |
| | | } |
| | | }; |
| | | TenantLineInnerInterceptor tenantLineInnerInterceptor = new TenantLineInnerInterceptor(tenantLineHandler); |
| | | // interceptor.addInnerInterceptor(tenantLineInnerInterceptor); |
| | | interceptor.addInnerInterceptor(tenantLineInnerInterceptor); |
| | | |
| | | // 分页插件配置 |
| | | PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(); |
| | | interceptor.addInnerInterceptor(paginationInnerInterceptor); |
| | | |
| | | return interceptor; |
| | | } |
| | | |
| | | private void configureJsqlParser() { |
| | | if (jsqlParserConfigured) { |
| | | return; |
| | | } |
| | | synchronized (MybatisPlusConfig.class) { |
| | | if (jsqlParserConfigured) { |
| | | return; |
| | | } |
| | | AtomicInteger threadIndex = new AtomicInteger(1); |
| | | int parserThreads = Math.max(4, Runtime.getRuntime().availableProcessors()); |
| | | ThreadPoolExecutor parserExecutor = new ThreadPoolExecutor( |
| | | parserThreads, |
| | | parserThreads, |
| | | 0L, |
| | | TimeUnit.MILLISECONDS, |
| | | new LinkedBlockingQueue<>(2048), |
| | | runnable -> { |
| | | Thread thread = new Thread(runnable); |
| | | thread.setName("jsql-parser-" + threadIndex.getAndIncrement()); |
| | | thread.setDaemon(true); |
| | | return thread; |
| | | }, |
| | | new ThreadPoolExecutor.CallerRunsPolicy() |
| | | ); |
| | | JsqlParserGlobal.setExecutorService(parserExecutor, new Thread(() -> { |
| | | if (!parserExecutor.isShutdown()) { |
| | | parserExecutor.shutdown(); |
| | | } |
| | | }, "jsql-parser-shutdown")); |
| | | JsqlParserGlobal.setJsqlParseCache(new JdkSerialCaffeineJsqlParseCache(builder -> |
| | | builder.maximumSize(1024).expireAfterAccess(30, TimeUnit.MINUTES))); |
| | | jsqlParserConfigured = true; |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | } catch (Exception e) { |
| | | System.out.println(e.getMessage()); |
| | | } |
| | | return new NullValue(); |
| | | //TODO 需设置一个系统调度帐号 |
| | | // return new NullValue(); |
| | | return new LongValue(1); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 解决Map映射非驼峰 |
| | | * @return |
| | | */ |
| | | @Bean |
| | | public ConfigurationCustomizer mybatisConfigurationCustomizer(){ |
| | | return new ConfigurationCustomizer() { |
| | | /** |
| | | * Customize the given a {@link MybatisConfiguration} object. |
| | | * |
| | | * @param configuration the configuration object to customize |
| | | */ |
| | | @Override |
| | | public void customize(MybatisConfiguration configuration) { |
| | | configuration.setObjectWrapperFactory(new MybatisMapWrapperFactory()); |
| | | } |
| | | }; |
| | | } |
| | | } |
| | | |