之前简单研究了Mybatis 的源码,现在简单研究下MybatisPlus 的源码。大体分析其执行过程。Mybatisplus 执行逻辑大体和mybatis一样,只是在启动过程中会生成一些默认的SQL下面研究其生成默认SQL的过程。
1. 自动配置
查看源码按自动配置的套路,先查看AutoConfiguration和Properties文件。
1. com.baomidou.mybatisplus.spring.boot.starter.MybatisPlusProperties 属性文件
1 package com.baomidou.mybatisplus.spring.boot.starter; 2 3 import java.io.IOException; 4 import java.util.ArrayList; 5 import java.util.Arrays; 6 import java.util.List; 7 import java.util.Properties; 8 9 import org.apache.ibatis.session.ExecutorType; 10 import org.springframework.boot.context.properties.ConfigurationProperties; 11 import org.springframework.boot.context.properties.NestedConfigurationProperty; 12 import org.springframework.core.io.Resource; 13 import org.springframework.core.io.support.PathMatchingResourcePatternResolver; 14 import org.springframework.core.io.support.ResourcePatternResolver; 15 16 import com.baomidou.mybatisplus.MybatisConfiguration; 17 18 /** 19 * Configuration properties for MyBatis. 20 * 21 * @author Eddú Meléndez 22 * @author Kazuki Shimizu 23 */ 24 @ConfigurationProperties(prefix = MybatisPlusProperties.MYBATIS_PLUS_PREFIX) 25 public class MybatisPlusProperties { 26 27 public static final String MYBATIS_PLUS_PREFIX = "mybatis-plus"; 28 29 /** 30 * Location of MyBatis xml config file. 31 */ 32 private String configLocation; 33 34 /** 35 * Locations of MyBatis mapper files. 36 */ 37 private String[] mapperLocations; 38 39 /** 40 * Packages to search type aliases. (Package delimiters are ",; \t\n") 41 */ 42 private String typeAliasesPackage; 43 44 // TODO 自定义枚举包 45 private String typeEnumsPackage; 46 47 /** 48 * Packages to search for type handlers. (Package delimiters are ",; \t\n") 49 */ 50 private String typeHandlersPackage; 51 52 /** 53 * Indicates whether perform presence check of the MyBatis xml config file. 54 */ 55 private boolean checkConfigLocation = false; 56 57 /** 58 * Execution mode for {@link org.mybatis.spring.SqlSessionTemplate}. 59 */ 60 private ExecutorType executorType; 61 62 /** 63 * Externalized properties for MyBatis configuration. 64 */ 65 private Properties configurationProperties; 66 /** 67 * Externalized properties for MyBatis configuration. 68 */ 69 @NestedConfigurationProperty 70 private GlobalConfig globalConfig; 71 72 /** 73 * A Configuration object for customize default settings. If {@link #configLocation} 74 * is specified, this property is not used. 75 */ 76 @NestedConfigurationProperty 77 private MybatisConfiguration configuration; 78 79 /** 80 * @since 1.1.0 81 */ 82 public String getConfigLocation() { 83 return this.configLocation; 84 } 85 86 /** 87 * @since 1.1.0 88 */ 89 public void setConfigLocation(String configLocation) { 90 this.configLocation = configLocation; 91 } 92 93 public String[] getMapperLocations() { 94 return this.mapperLocations; 95 } 96 97 public void setMapperLocations(String[] mapperLocations) { 98 this.mapperLocations = mapperLocations; 99 } 100 101 public String getTypeHandlersPackage() { 102 return this.typeHandlersPackage; 103 } 104 105 public void setTypeHandlersPackage(String typeHandlersPackage) { 106 this.typeHandlersPackage = typeHandlersPackage; 107 } 108 109 public String getTypeAliasesPackage() { 110 return this.typeAliasesPackage; 111 } 112 113 public void setTypeAliasesPackage(String typeAliasesPackage) { 114 this.typeAliasesPackage = typeAliasesPackage; 115 } 116 117 public String getTypeEnumsPackage() { 118 return typeEnumsPackage; 119 } 120 121 public void setTypeEnumsPackage(String typeEnumsPackage) { 122 this.typeEnumsPackage = typeEnumsPackage; 123 } 124 125 public boolean isCheckConfigLocation() { 126 return this.checkConfigLocation; 127 } 128 129 public void setCheckConfigLocation(boolean checkConfigLocation) { 130 this.checkConfigLocation = checkConfigLocation; 131 } 132 133 public ExecutorType getExecutorType() { 134 return this.executorType; 135 } 136 137 public void setExecutorType(ExecutorType executorType) { 138 this.executorType = executorType; 139 } 140 141 /** 142 * @since 1.2.0 143 */ 144 public Properties getConfigurationProperties() { 145 return configurationProperties; 146 } 147 148 /** 149 * @since 1.2.0 150 */ 151 public void setConfigurationProperties(Properties configurationProperties) { 152 this.configurationProperties = configurationProperties; 153 } 154 155 public MybatisConfiguration getConfiguration() { 156 return configuration; 157 } 158 159 public void setConfiguration(MybatisConfiguration configuration) { 160 this.configuration = configuration; 161 } 162 163 public Resource[] resolveMapperLocations() { 164 ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver(); 165 List<Resource> resources = new ArrayList<Resource>(); 166 if (this.mapperLocations != null) { 167 for (String mapperLocation : this.mapperLocations) { 168 try { 169 Resource[] mappers = resourceResolver.getResources(mapperLocation); 170 resources.addAll(Arrays.asList(mappers)); 171 } catch (IOException e) { 172 // ignore 173 } 174 } 175 } 176 return resources.toArray(new Resource[resources.size()]); 177 } 178 179 public GlobalConfig getGlobalConfig() { 180 return globalConfig; 181 } 182 183 public void setGlobalConfig(GlobalConfig globalConfig) { 184 this.globalConfig = globalConfig; 185 } 186 }