¶Ô±ÈÐÂÎļþ |
| | |
| | | /* |
| | | * Copyright (c) 2018-2028, Chill Zhuang All rights reserved. |
| | | * |
| | | * Redistribution and use in source and binary forms, with or without |
| | | * modification, are permitted provided that the following conditions are met: |
| | | * |
| | | * Redistributions of source code must retain the above copyright notice, |
| | | * this list of conditions and the following disclaimer. |
| | | * Redistributions in binary form must reproduce the above copyright |
| | | * notice, this list of conditions and the following disclaimer in the |
| | | * documentation and/or other materials provided with the distribution. |
| | | * Neither the name of the dreamlu.net developer nor the names of its |
| | | * contributors may be used to endorse or promote products derived from |
| | | * this software without specific prior written permission. |
| | | * Author: Chill åºéª (smallchill@163.com) |
| | | */ |
| | | package org.springblade.core.mp.config; |
| | | |
| | | import com.baomidou.mybatisplus.core.injector.ISqlInjector; |
| | | import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; |
| | | import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler; |
| | | import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor; |
| | | import lombok.AllArgsConstructor; |
| | | import net.sf.jsqlparser.expression.Expression; |
| | | import net.sf.jsqlparser.expression.StringValue; |
| | | import org.mybatis.spring.annotation.MapperScan; |
| | | import org.springblade.core.launch.props.BladePropertySource; |
| | | import org.springblade.core.mp.injector.BladeSqlInjector; |
| | | import org.springblade.core.mp.intercept.QueryInterceptor; |
| | | import org.springblade.core.mp.plugins.BladePaginationInterceptor; |
| | | import org.springblade.core.mp.plugins.SqlLogInterceptor; |
| | | import org.springblade.core.mp.props.MybatisPlusProperties; |
| | | import org.springblade.core.mp.resolver.PageArgumentResolver; |
| | | import org.springblade.core.secure.utils.AuthUtil; |
| | | import org.springblade.core.tool.constant.BladeConstant; |
| | | import org.springblade.core.tool.utils.Func; |
| | | import org.springblade.core.tool.utils.ObjectUtil; |
| | | import org.springframework.beans.factory.ObjectProvider; |
| | | import org.springframework.boot.autoconfigure.AutoConfiguration; |
| | | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| | | import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.core.annotation.AnnotationAwareOrderComparator; |
| | | import org.springframework.web.method.support.HandlerMethodArgumentResolver; |
| | | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
| | | |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * mybatis-plus é
ç½® |
| | | * |
| | | * @author Chill |
| | | */ |
| | | @AutoConfiguration |
| | | @AllArgsConstructor |
| | | @MapperScan("org.springblade.**.mapper.**") |
| | | @EnableConfigurationProperties(MybatisPlusProperties.class) |
| | | @BladePropertySource(value = "classpath:/blade-mybatis.yml") |
| | | public class MybatisPlusConfiguration implements WebMvcConfigurer { |
| | | |
| | | /** |
| | | * ç§æ·æ¦æªå¨ |
| | | */ |
| | | @Bean |
| | | @ConditionalOnMissingBean(TenantLineInnerInterceptor.class) |
| | | public TenantLineInnerInterceptor tenantLineInnerInterceptor() { |
| | | return new TenantLineInnerInterceptor(new TenantLineHandler() { |
| | | @Override |
| | | public Expression getTenantId() { |
| | | return new StringValue(Func.toStr(AuthUtil.getTenantId(), BladeConstant.ADMIN_TENANT_ID)); |
| | | } |
| | | |
| | | @Override |
| | | public boolean ignoreTable(String tableName) { |
| | | return true; |
| | | } |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | * mybatis-plus æ¦æªå¨éå |
| | | */ |
| | | @Bean |
| | | @ConditionalOnMissingBean(MybatisPlusInterceptor.class) |
| | | public MybatisPlusInterceptor mybatisPlusInterceptor(ObjectProvider<QueryInterceptor[]> queryInterceptors, |
| | | TenantLineInnerInterceptor tenantLineInnerInterceptor, |
| | | MybatisPlusProperties mybatisPlusProperties) { |
| | | MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); |
| | | // é
ç½®ç§æ·æ¦æªå¨ |
| | | if (mybatisPlusProperties.getTenantMode()) { |
| | | interceptor.addInnerInterceptor(tenantLineInnerInterceptor); |
| | | } |
| | | // é
ç½®åé¡µæ¦æªå¨ |
| | | BladePaginationInterceptor paginationInterceptor = new BladePaginationInterceptor(); |
| | | // é
ç½®èªå®ä¹æ¥è¯¢æ¦æªå¨ |
| | | QueryInterceptor[] queryInterceptorArray = queryInterceptors.getIfAvailable(); |
| | | if (ObjectUtil.isNotEmpty(queryInterceptorArray)) { |
| | | AnnotationAwareOrderComparator.sort(queryInterceptorArray); |
| | | paginationInterceptor.setQueryInterceptors(queryInterceptorArray); |
| | | } |
| | | paginationInterceptor.setMaxLimit(mybatisPlusProperties.getPageLimit()); |
| | | paginationInterceptor.setOverflow(mybatisPlusProperties.getOverflow()); |
| | | paginationInterceptor.setOptimizeJoin(mybatisPlusProperties.getOptimizeJoin()); |
| | | interceptor.addInnerInterceptor(paginationInterceptor); |
| | | return interceptor; |
| | | } |
| | | |
| | | /** |
| | | * sql æ¥å¿ |
| | | */ |
| | | @Bean |
| | | public SqlLogInterceptor sqlLogInterceptor(MybatisPlusProperties mybatisPlusProperties) { |
| | | return new SqlLogInterceptor(mybatisPlusProperties); |
| | | } |
| | | |
| | | /** |
| | | * sql 注å
¥ |
| | | */ |
| | | @Bean |
| | | @ConditionalOnMissingBean(ISqlInjector.class) |
| | | public ISqlInjector sqlInjector() { |
| | | return new BladeSqlInjector(); |
| | | } |
| | | |
| | | /** |
| | | * page è§£æå¨ |
| | | */ |
| | | @Override |
| | | public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) { |
| | | argumentResolvers.add(new PageArgumentResolver()); |
| | | } |
| | | |
| | | } |
| | | |