View Javadoc
1   /**
2    *    Copyright 2015-2019 the original author or authors.
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 org.mybatis.spring.boot.autoconfigure;
17  
18  import java.io.IOException;
19  import java.util.Optional;
20  import java.util.Properties;
21  import java.util.stream.Stream;
22  
23  import org.apache.ibatis.scripting.LanguageDriver;
24  import org.apache.ibatis.session.Configuration;
25  import org.apache.ibatis.session.ExecutorType;
26  
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  /**
34   * Configuration properties for MyBatis.
35   *
36   * @author EddĂș MelĂ©ndez
37   * @author Kazuki Shimizu
38   */
39  @ConfigurationProperties(prefix = MybatisProperties.MYBATIS_PREFIX)
40  public class MybatisProperties {
41  
42    public static final String MYBATIS_PREFIX = "mybatis";
43  
44    private static final ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
45  
46    /**
47     * Location of MyBatis xml config file.
48     */
49    private String configLocation;
50  
51    /**
52     * Locations of MyBatis mapper files.
53     */
54    private String[] mapperLocations;
55  
56    /**
57     * Packages to search type aliases. (Package delimiters are ",; \t\n")
58     */
59    private String typeAliasesPackage;
60  
61    /**
62     * The super class for filtering type alias. If this not specifies, the MyBatis deal as type alias all classes that
63     * searched from typeAliasesPackage.
64     */
65    private Class<?> typeAliasesSuperType;
66  
67    /**
68     * Packages to search for type handlers. (Package delimiters are ",; \t\n")
69     */
70    private String typeHandlersPackage;
71  
72    /**
73     * Indicates whether perform presence check of the MyBatis xml config file.
74     */
75    private boolean checkConfigLocation = false;
76  
77    /**
78     * Execution mode for {@link org.mybatis.spring.SqlSessionTemplate}.
79     */
80    private ExecutorType executorType;
81  
82    /**
83     * The default scripting language driver class. (Available when use together with mybatis-spring 2.0.2+)
84     */
85    private Class<? extends LanguageDriver> defaultScriptingLanguageDriver;
86  
87    /**
88     * Externalized properties for MyBatis configuration.
89     */
90    private Properties configurationProperties;
91  
92    /**
93     * A Configuration object for customize default settings. If {@link #configLocation} is specified, this property is
94     * not used.
95     */
96    @NestedConfigurationProperty
97    private Configuration configuration;
98  
99    /**
100    * @since 1.1.0
101    */
102   public String getConfigLocation() {
103     return this.configLocation;
104   }
105 
106   /**
107    * @since 1.1.0
108    */
109   public void setConfigLocation(String configLocation) {
110     this.configLocation = configLocation;
111   }
112 
113   public String[] getMapperLocations() {
114     return this.mapperLocations;
115   }
116 
117   public void setMapperLocations(String[] mapperLocations) {
118     this.mapperLocations = mapperLocations;
119   }
120 
121   public String getTypeHandlersPackage() {
122     return this.typeHandlersPackage;
123   }
124 
125   public void setTypeHandlersPackage(String typeHandlersPackage) {
126     this.typeHandlersPackage = typeHandlersPackage;
127   }
128 
129   public String getTypeAliasesPackage() {
130     return this.typeAliasesPackage;
131   }
132 
133   public void setTypeAliasesPackage(String typeAliasesPackage) {
134     this.typeAliasesPackage = typeAliasesPackage;
135   }
136 
137   /**
138    * @since 1.3.3
139    */
140   public Class<?> getTypeAliasesSuperType() {
141     return typeAliasesSuperType;
142   }
143 
144   /**
145    * @since 1.3.3
146    */
147   public void setTypeAliasesSuperType(Class<?> typeAliasesSuperType) {
148     this.typeAliasesSuperType = typeAliasesSuperType;
149   }
150 
151   public boolean isCheckConfigLocation() {
152     return this.checkConfigLocation;
153   }
154 
155   public void setCheckConfigLocation(boolean checkConfigLocation) {
156     this.checkConfigLocation = checkConfigLocation;
157   }
158 
159   public ExecutorType getExecutorType() {
160     return this.executorType;
161   }
162 
163   public void setExecutorType(ExecutorType executorType) {
164     this.executorType = executorType;
165   }
166 
167   /**
168    * @since 2.1.0
169    */
170   public Class<? extends LanguageDriver> getDefaultScriptingLanguageDriver() {
171     return defaultScriptingLanguageDriver;
172   }
173 
174   /**
175    * @since 2.1.0
176    */
177   public void setDefaultScriptingLanguageDriver(Class<? extends LanguageDriver> defaultScriptingLanguageDriver) {
178     this.defaultScriptingLanguageDriver = defaultScriptingLanguageDriver;
179   }
180 
181   /**
182    * @since 1.2.0
183    */
184   public Properties getConfigurationProperties() {
185     return configurationProperties;
186   }
187 
188   /**
189    * @since 1.2.0
190    */
191   public void setConfigurationProperties(Properties configurationProperties) {
192     this.configurationProperties = configurationProperties;
193   }
194 
195   public Configuration getConfiguration() {
196     return configuration;
197   }
198 
199   public void setConfiguration(Configuration configuration) {
200     this.configuration = configuration;
201   }
202 
203   public Resource[] resolveMapperLocations() {
204     return Stream.of(Optional.ofNullable(this.mapperLocations).orElse(new String[0]))
205         .flatMap(location -> Stream.of(getResources(location))).toArray(Resource[]::new);
206   }
207 
208   private Resource[] getResources(String location) {
209     try {
210       return resourceResolver.getResources(location);
211     } catch (IOException e) {
212       return new Resource[0];
213     }
214   }
215 
216 }