4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | *
8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | *
10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | package com.kaikai.starter; 17 | 18 | import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties; 19 | import com.baomidou.mybatisplus.core.MybatisConfiguration; 20 | import com.baomidou.mybatisplus.core.config.GlobalConfig; 21 | import com.baomidou.mybatisplus.core.toolkit.Constants; 22 | import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils; 23 | import lombok.Data; 24 | import lombok.experimental.Accessors; 25 | import org.apache.ibatis.scripting.LanguageDriver; 26 | import org.apache.ibatis.session.ExecutorType; 27 | import org.springframework.boot.context.properties.ConfigurationProperties; 28 | import org.springframework.boot.context.properties.NestedConfigurationProperty; 29 | import org.springframework.core.io.Resource; 30 | import org.springframework.core.io.support.PathMatchingResourcePatternResolver; 31 | import org.springframework.core.io.support.ResourcePatternResolver; 32 | 33 | import java.io.IOException; 34 | import java.util.Optional; 35 | import java.util.Properties; 36 | import java.util.stream.Stream; 37 | 38 | /** 39 | * Configuration properties for MyBatis. 40 | * 41 | * @author Eddú Meléndez 42 | * @author Kazuki Shimizu 43 | */ 44 | @Data 45 | @Accessors(chain = true) 46 | @ConfigurationProperties(prefix = Constants.MYBATIS_PLUS) 47 | public class MybatisPlusShardingProperties extends MybatisPlusProperties { 48 | 49 | private static final ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver(); 50 | 51 | /** 52 | * Location of MyBatis xml config file. 53 | */ 54 | private String configLocation; 55 | 56 | /** 57 | * Locations of MyBatis mapper files. 58 | * 59 | * @since 3.1.2 add default value 60 | */ 61 | private String[] mapperLocations = new String[]{"classpath*:/noSharding/**/*.xml"}; 62 | 63 | /** 64 | * Packages to search type aliases. (Package delimiters are ",; \t\n") 65 | */ 66 | private String typeAliasesPackage; 67 | 68 | /** 69 | * The super class for filtering type alias. 70 | * If this not specifies, the MyBatis deal as type alias all classes that searched from typeAliasesPackage. 71 | */ 72 | private Class> typeAliasesSuperType; 73 | 74 | /** 75 | * Packages to search for type handlers. (Package delimiters are ",; \t\n") 76 | */ 77 | private String typeHandlersPackage; 78 | 79 | /** 80 | * Indicates whether perform presence check of the MyBatis xml config file. 81 | */ 82 | private boolean checkConfigLocation = false; 83 | 84 | /** 85 | * Execution mode for {@link org.mybatis.spring.SqlSessionTemplate}. 86 | */ 87 | private ExecutorType executorType; 88 | 89 | /** 90 | * The default scripting language driver class. (Available when use together with mybatis-spring 2.0.2+) 91 | *
92 | * 如果设置了这个,你会至少失去几乎所有 mp 提供的功能 93 | */ 94 | private Class extends LanguageDriver> defaultScriptingLanguageDriver; 95 | 96 | /** 97 | * Externalized properties for MyBatis configuration. 98 | */ 99 | private Properties configurationProperties; 100 | 101 | /** 102 | * A Configuration object for customize default settings. If {@link #configLocation} 103 | * is specified, this property is not used. 104 | * TODO 使用 MybatisConfiguration 105 | */ 106 | @NestedConfigurationProperty 107 | private MybatisConfiguration configuration; 108 | 109 | /** 110 | * TODO 枚举包扫描 111 | */ 112 | private String typeEnumsPackage; 113 | 114 | /** 115 | * TODO 全局配置 116 | */ 117 | @NestedConfigurationProperty 118 | private GlobalConfig globalConfig = GlobalConfigUtils.defaults(); 119 | 120 | 121 | @Override 122 | public Resource[] resolveMapperLocations() { 123 | return Stream.of(Optional.ofNullable(this.mapperLocations).orElse(new String[0])) 124 | .flatMap(location -> Stream.of(getResources(location))) 125 | .toArray(Resource[]::new); 126 | } 127 | 128 | private Resource[] getResources(String location) { 129 | try { 130 | return resourceResolver.getResources(location); 131 | } catch (IOException e) { 132 | return new Resource[0]; 133 | } 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /mybatis-plus-sharding-spring-boot-starter/src/main/java/com/kaikai/starter/annotation/NoShardingMapper.java: -------------------------------------------------------------------------------- 1 | package com.kaikai.starter.annotation; 2 | 3 | import org.springframework.context.annotation.Lazy; 4 | import org.springframework.stereotype.Component; 5 | 6 | import java.lang.annotation.ElementType; 7 | import java.lang.annotation.Retention; 8 | import java.lang.annotation.RetentionPolicy; 9 | import java.lang.annotation.Target; 10 | 11 | /** 12 | * M1Dao 13 | * 14 | * @author pu 15 | * @date 2021/1/11 16 | * time 12:19 17 | */ 18 | @Retention(RetentionPolicy.RUNTIME) 19 | @Target(ElementType.TYPE) 20 | @Component 21 | @Lazy 22 | public @interface NoShardingMapper { 23 | 24 | String value() default ""; 25 | } 26 | -------------------------------------------------------------------------------- /mybatis-plus-sharding-spring-boot-starter/src/main/resources/META-INF/spring.factories: -------------------------------------------------------------------------------- 1 | org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ 2 | com.kaikai.starter.KaiShardingMybatisPlusAutoConfiguration,\ 3 | com.kaikai.starter.KaiShardingOneMybatisPlusAutoConfiguration 4 | 5 | 6 | --------------------------------------------------------------------------------