Python 官方文档:入门教程 => 点击学习
目录使用xml编写动态sql动态SQL语句的原理入口类:mybatisSqlSessionFactoryBuilderMybatisMapperAnnotationBuilder动态
在Resources文件夹下创建一个Mapper文件夹
比如我们需要在User表中使用增删改查,创建UserMapper.xml,对应MybatisPlus中的UserMapper接口
之后我们在application.yml中配置mapper文件夹的路径
mybatis-plus:
mapper-locations: classpath:mapper
protected final MybatisMapperReGIStry mybatisMapperRegistry = new MybatisMapperRegistry(this);
// ....
public MybatisConfiguration() {
super();
this.mapUnderscoreToCamelCase = true;
languageRegistry.setDefaultDriverClass(MybatisXMLLanguageDriver.class);
}
@Override
public void addMappedStatement(MappedStatement ms) {
// ...
}
// ... 省略若干行
@Override
public <T> void addMapper(Class<T> type) {
mybatisMapperRegistry.addMapper(type);
}
// .... 省略若干行
}
在MybatisMapperRegistry中,MP将mybatis的MapperAnnotationBuilder替换为MP自己的MybatisMapperAnnotationBuilder
public class MybatisMapperRegistry extends MapperRegistry {
@Override
public <T> void addMapper(Class<T> type) {
// ... 省略若干行
MybatisMapperAnnotationBuilder parser = new MybatisMapperAnnotationBuilder(config, type);
parser.parse();
// ... 省略若干行
}
}
在MybatisMapperRegistry类的addMapper方法中,真正进入到MP的核心类MybatisMapperAnnotationBuilder,MybatisMapperAnnotationBuilder这个类是MP实现动态脚本的关键类。
在MP的核心类MybatisMapperAnnotationBuilder的parser方法中,MP逐一遍历要加载的Mapper类,加载的方法包括下面几个
public class MybatisMapperAnnotationBuilder extends MapperAnnotationBuilder {
@Override
public void parse() {
//... 省略若干行
for (Method method : type.getMethods()) {
parseStatement(method);
InterceptorIgnoreHelper.initSqlParserInfoCache(cache, mapperName, method);
SqlParserHelper.initSqlParserInfoCache(mapperName, method);
}
if (GlobalConfigUtils.isSupperMapperChildren(configuration, type)) {
GlobalConfigUtils.getSqlInjector(configuration).inspectInject(assistant, type);
}
//... 省略若干行
}
@Override
public void inspectInject(MapperBuilderAssistant builderAssistant, Class<?> mapperClass) {
Class<?> modelClass = extractModelClass(mapperClass);
//... 省略若干行
List<AbstractMethod> methodList = 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 {
@Override
public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
return Stream.of(
new Insert(),
//... 省略若干行
new SelectPage()
).collect(toList());
}
}
在MybatisMapperAnnotationBuilder中,MP真正将框架自定义的动态SQL语句注册到Mybatis引擎中。而AbstractMethod则履行了具体方法的SQL语句构造。
以 SelectById 这个类为例说明下
public class SelectById extends AbstractMethod {
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
SqlMethod sqlMethod = SqlMethod.SELECT_BY_ID;
SqlSource sqlSource = new RawSqlSource(configuration, String.fORMat(sqlMethod.getSql(),
sqlSelectColumns(tableInfo, false),
tableInfo.getTableName(), tableInfo.geTKEyColumn(), tableInfo.getKeyProperty(),
tableInfo.getLogicDeleteSql(true, true)), Object.class);
return this.addSelectMappedStatementForTable(mapperClass, getMethod(sqlMethod), sqlSource, tableInfo);
}
}
至此,MP完成了在启动时加载自定义的方法xml配置的过程,后面的就是mybatis ${变量} #{变量}的动态替换和预编译,已经进入mybatis自有功能。
MP总共改写和替换了mybatis的十多个类,主要如下图所示:
总体上来说,MP实现mybatis的增强,手段略显繁琐和不够直观,其实根据MybatisMapperAnnotationBuilder构造出自定义方法的xml文件,将其转换为mybatis的Resource资源,可以只继承重写一个Mybatis类:SqlSessionFactoryBean 比如如下:
public class YourSqlSessionFactoryBean extends SqlSessionFactoryBean implements ApplicationContextAware {
private Resource[] mapperLocations;
@Override
public void setMapperLocations(Resource... mapperLocations) {
super.setMapperLocations(mapperLocations);
this.mapperLocations = mapperLocations;
}
@Override
public void afterPropertiesSet() throws Exception {
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
this.setMapperLocations(InjectMapper.getMapperResource(this.dbType, beanFactory, this.mapperLocations));
super.afterPropertiesSet();
}
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: Mybatis Plus使用XML编写动态sql的超简易方法
本文链接: https://lsjlt.com/news/163146.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0