Insight mybatis xml 动态sql解析
来源:互联网 发布:部门信息表sql 编辑:程序博客网 时间:2024/06/03 18:47
一. mybatis 动态sql疑问
1. 占位符${foo},#{foo}怎样解析的?
2. xml 动态sql是怎样翻译的?
二. Insight
首先需要明确runtime ,执行的过程:
// select查询执行过程publicList selectList(String statement, Object parameter, RowBounds rowBounds) { try { // 1. 根据mapper 方法名称,从configuration取出对应的statement 主要就是动态sql xml片段 MappedStatement ms = configuration.getMappedStatement(statement); // 2. 根据配置的statement,执行查询,返回结果集 List result = executor.query(ms, wrapCollection(parameter), rowBounds, Executor.NO_RESULT_HANDLER); return result; } // ...}// 解析动态sql,执行sqlpublic List query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException { // 动态sql就是在这个阶段完成解析,形成标准的sql 语句, eg: select * from dual where id = ? BoundSql boundSql = ms.getBoundSql(parameter); CacheKey key = createCacheKey(ms, parameter, rowBounds, boundSql); return query(ms, parameter, rowBounds, resultHandler, key, boundSql);}
// xml 动态sql核心实现,更具体的类参考SqlNode、SqlSource其他实现类public BoundSql getBoundSql(Object parameterObject) { DynamicContext context = new DynamicContext(configuration, parameterObject); //1. 纯文本解析,包括 TextSqlNode, ForEachSqlNode, IfSqlNode, VarDeclSqlNode, TrimSqlNode, WhereSqlNode, SetSqlNode, ChooseSqlNode // 其中${foo} 表达式就是在TextSqlNode 实现,@see new GenericTokenParser("${", "}", new BindingTokenParser(context)) rootSqlNode.apply(context); SqlSourceBuilder sqlSourceParser = new SqlSourceBuilder(configuration); Class> parameterType = parameterObject == null ? Object.class : parameterObject.getClass(); //2. 带有关联参数解析,prepareStatement 中用到的parameterMappings // 其中#{foo} 表达式就是在此处实现,@see new GenericTokenParser("#{", "}", handler); SqlSource sqlSource = sqlSourceParser.parse(context.getSql(), parameterType, context.getBindings()); BoundSql boundSql = sqlSource.getBoundSql(parameterObject); return boundSql;}
解疑完毕。
三.参考
各个动态sql节点的解析器
put("trim", new TrimHandler());put("where", new WhereHandler());put("set", new SetHandler());put("foreach", new ForEachHandler());put("if", new IfHandler());put("choose", new ChooseHandler());put("when", new IfHandler());put("otherwise", new OtherwiseHandler());put("bind", new BindHandler());
阅读全文
0 0
- Insight mybatis xml 动态sql解析
- MyBatis(三) xml文件解析流程 动态SQL解析
- Mybatis的动态sql-----xml
- mybatis动态SQL解析备忘录
- Mybatis解析动态sql原理分析
- Mybatis解析动态sql原理分析
- Insight MyBatis-Spring 解析过程,调用执行
- mybatis的mapper.xml文件的动态sql编辑
- mybatis的xml文件l,动态sql编写语法
- 如何解析Mybatis xml文件中配置的sql
- Mybatis 动态SQL之<trim>,<where>,<set>源码解析
- mybatis动态SQL语句
- MyBatis动态SQL
- MyBatis 动态SQL
- Mybatis 动态SQL
- MyBatis动态SQL
- MyBatis动态SQL完整版
- mybatis动态sql
- JAVA历史版本
- 几种数据库设计思想
- VS2017 发布到远程存储库时遇到错误:Git failed with a fatal error.
- Jquery操作Input之Select和Radio选中
- h5 7
- Insight mybatis xml 动态sql解析
- h5 8
- Oracle数据库中,如何给已有很多数据的表填充自增序列字段
- C语言-数据结构-链表合并无需输入结点
- Android Notification自定义样式
- h5 8
- 树形DP
- 51Nod1240 莫比乌斯函数
- 任务7 正则表达式