MybatisPlusProperties.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (c) 2011-2023, baomidou (jobob@qq.com).
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://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,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.baomidou.mybatisplus.autoconfigure;
  17. import com.baomidou.mybatisplus.core.MybatisConfiguration;
  18. import com.baomidou.mybatisplus.core.config.GlobalConfig;
  19. import com.baomidou.mybatisplus.core.toolkit.Constants;
  20. import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils;
  21. import lombok.Data;
  22. import lombok.experimental.Accessors;
  23. import org.apache.ibatis.scripting.LanguageDriver;
  24. import org.apache.ibatis.session.ExecutorType;
  25. import org.springframework.boot.context.properties.ConfigurationProperties;
  26. import org.springframework.boot.context.properties.NestedConfigurationProperty;
  27. import org.springframework.core.io.Resource;
  28. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
  29. import org.springframework.core.io.support.ResourcePatternResolver;
  30. import java.io.IOException;
  31. import java.util.Optional;
  32. import java.util.Properties;
  33. import java.util.stream.Stream;
  34. /**
  35. * Configuration properties for MyBatis.
  36. *
  37. * @author Eddú Meléndez
  38. * @author Kazuki Shimizu
  39. */
  40. @Data
  41. @Accessors(chain = true)
  42. @ConfigurationProperties(prefix = Constants.MYBATIS_PLUS)
  43. public class MybatisPlusProperties {
  44. private static final ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
  45. /**
  46. * Location of MyBatis xml config file.
  47. */
  48. private String configLocation;
  49. /**
  50. * Locations of MyBatis mapper files.
  51. *
  52. * @since 3.1.2 add default value
  53. */
  54. private String[] mapperLocations = new String[]{"classpath*:/mapper/**/*.xml"};
  55. /**
  56. * Packages to search type aliases. (Package delimiters are ",; \t\n")
  57. */
  58. private String typeAliasesPackage;
  59. /**
  60. * The super class for filtering type alias.
  61. * If this not specifies, the MyBatis deal as type alias all classes that searched from typeAliasesPackage.
  62. */
  63. private Class<?> typeAliasesSuperType;
  64. /**
  65. * Packages to search for type handlers. (Package delimiters are ",; \t\n")
  66. */
  67. private String typeHandlersPackage;
  68. /**
  69. * Indicates whether perform presence check of the MyBatis xml config file.
  70. */
  71. private boolean checkConfigLocation = false;
  72. /**
  73. * Execution mode for {@link org.mybatis.spring.SqlSessionTemplate}.
  74. */
  75. private ExecutorType executorType;
  76. /**
  77. * The default scripting language driver class. (Available when use together with mybatis-spring 2.0.2+)
  78. * <p>
  79. * 如果设置了这个,你会至少失去几乎所有 mp 提供的功能
  80. */
  81. private Class<? extends LanguageDriver> defaultScriptingLanguageDriver;
  82. /**
  83. * Externalized properties for MyBatis configuration.
  84. */
  85. private Properties configurationProperties;
  86. /**
  87. * A Configuration object for customize default settings. If {@link #configLocation}
  88. * is specified, this property is not used.
  89. * TODO 使用 MybatisConfiguration
  90. */
  91. @NestedConfigurationProperty
  92. private MybatisConfiguration configuration;
  93. /**
  94. * 不再需要这个配置,放心删除
  95. *
  96. * @deprecated 2022-03-07
  97. */
  98. @Deprecated
  99. private String typeEnumsPackage;
  100. /**
  101. * TODO 全局配置
  102. */
  103. @NestedConfigurationProperty
  104. private GlobalConfig globalConfig = GlobalConfigUtils.defaults();
  105. public Resource[] resolveMapperLocations() {
  106. return Stream.of(Optional.ofNullable(this.mapperLocations).orElse(new String[0]))
  107. .flatMap(location -> Stream.of(getResources(location))).toArray(Resource[]::new);
  108. }
  109. private Resource[] getResources(String location) {
  110. try {
  111. return resourceResolver.getResources(location);
  112. } catch (IOException e) {
  113. return new Resource[0];
  114. }
  115. }
  116. }