面试官问:Mybatis Plus 是如何实现动态 SQL 语句的?原理你懂吗?
阅读本文大概需要 4 分钟。
来源:juejin.cn/post/6883081187103866894

public class MybatisSqlSessionFactoryBuilder extends SqlSessionFactoryBuilder {public SqlSessionFactory build(Configuration configuration) {// ... 省略若干行if (globalConfig.isEnableSqlRunner()) {new SqlRunnerInjector().inject(configuration);}// ... 省略若干行return sqlSessionFactory;}}
扩展继承自Mybatis的MybatisConfiguration类: MP动态脚本构建,注册,及其它逻辑判断。
SqlRunnerInjector: MP默认插入一些动态方法的xml 脚本方法。
public class MybatisConfiguration extends Configuration {/*** Mapper 注册*/protected final MybatisMapperRegistry mybatisMapperRegistry = new MybatisMapperRegistry(this);// ..../*** 初始化调用*/public MybatisConfiguration() {super();this.mapUnderscoreToCamelCase = true;languageRegistry.setDefaultDriverClass(MybatisXMLLanguageDriver.class);}/*** MybatisPlus 加载 SQL 顺序:*1、加载 XML中的 SQL
*2、加载 SqlProvider 中的 SQL
*3、XmlSql 与 SqlProvider不能包含相同的 SQL
*调整后的 SQL优先级:XmlSql > sqlProvider > CurdSql
*/public void addMappedStatement(MappedStatement ms) {// ...}// ... 省略若干行/*** 使用自己的 MybatisMapperRegistry*/publicvoid addMapper(Class type) { mybatisMapperRegistry.addMapper(type);}// .... 省略若干行}
public class MybatisMapperRegistry extends MapperRegistry {publicvoid addMapper(Class type) {// ... 省略若干行MybatisMapperAnnotationBuilder parser = new MybatisMapperAnnotationBuilder(config, type);parser.parse();// ... 省略若干行}}
public class MybatisMapperAnnotationBuilder extends MapperAnnotationBuilder {public void parse() {//... 省略若干行for (Method method : type.getMethods()) {/** for循环代码, MP判断method方法是否是@Select @Insert等mybatis注解方法**/parseStatement(method);InterceptorIgnoreHelper.initSqlParserInfoCache(cache, mapperName, method);SqlParserHelper.initSqlParserInfoCache(mapperName, method);}/** 这2行代码, MP注入默认的方法列表**/if (GlobalConfigUtils.isSupperMapperChildren(configuration, type)) {GlobalConfigUtils.getSqlInjector(configuration).inspectInject(assistant, type);}//... 省略若干行}public void inspectInject(MapperBuilderAssistant builderAssistant, Class> mapperClass) {Class> modelClass = extractModelClass(mapperClass);//... 省略若干行ListmethodList = this.getMethodList(mapperClass); TableInfo tableInfo = TableInfoHelper.initTableInfo(builderAssistant, modelClass);// 循环注入自定义方法methodList.forEach(m -> m.inject(builderAssistant, mapperClass, modelClass, tableInfo));mapperRegistryCache.add(className);}}public class DefaultSqlInjector extends AbstractSqlInjector {public ListgetMethodList(Class> mapperClass) {return Stream.of(new Insert(),//... 省略若干行new SelectPage()).collect(toList());}}
/*** 根据ID 查询一条数据*/public class SelectById extends AbstractMethod {public MappedStatement injectMappedStatement(Class> mapperClass, Class> modelClass, TableInfo tableInfo) {/** 定义 mybatis xml method id, 对应**/ SqlMethod sqlMethod = SqlMethod.SELECT_BY_ID;/** 构造id对应的具体xml片段 **/SqlSource sqlSource = new RawSqlSource(configuration, String.format(sqlMethod.getSql(),sqlSelectColumns(tableInfo, false),tableInfo.getTableName(), tableInfo.getKeyColumn(), tableInfo.getKeyProperty(),tableInfo.getLogicDeleteSql(true, true)), Object.class);/** 将xml method方法添加到mybatis的MappedStatement中 **/return this.addSelectMappedStatementForTable(mapperClass, getMethod(sqlMethod), sqlSource, tableInfo);}}

public class YourSqlSessionFactoryBean extends SqlSessionFactoryBean implements ApplicationContextAware {private Resource[] mapperLocations;public void setMapperLocations(Resource... mapperLocations) {super.setMapperLocations(mapperLocations);/** 存使用mybatis原生定义的mapper xml文件路径**/this.mapperLocations = mapperLocations;}/*** {@inheritDoc}*/public void afterPropertiesSet() throws Exception {ConfigurableListableBeanFactory beanFactory = getBeanFactory();/** 只需要通过将自定义的方法构造成xml resource和原生定义的Resource一起注入到mybatis中即可, 这样就可以实现MP的自定义动态SQL和原生SQL的共生关系**/this.setMapperLocations(InjectMapper.getMapperResource(this.dbType, beanFactory, this.mapperLocations));super.afterPropertiesSet();}}
最近面试BAT,整理一份面试资料《Java面试BATJ通关手册》,覆盖了Java核心技术、JVM、Java并发、SSM、微服务、数据库、数据结构等等。
朕已阅 
评论

