MyBatis源码的学习(13)---Mybatis是如何构建sql的?

统一的一个接口:sqlSource

俩个实现类: 

1.RawSqlSource 
解析:select * from user where name = #{name}
属性字段 sqlSource --StaticSqlSource类型的
StaticSqlSource进行将'#{}'替换为 ?占位符
StaticSqlSource中的属性字段sql用于存储解析后的sql 
select * from user where name = ?

2.DynamicSqlSource
解析含有 '${}'或 <where>,<if>等标签的sql
 select * from ${tablename} where name = #{name}
    insert into u_user(usercode,username,createtime,usertype,mobile)
    values
    <foreach collection="list" item="user" separator=",">
      (#{user.userCode},#{user.userName}
      ,#{user.createTime},#{user.userType},#{user.mobile})
    </foreach>
啥时候解析生成sql,在getBoundSql()方法中,解析动态sql,最终
解析后结果
select * from user where name = ?
insert into u_user(usercode,username,createtime,usertype,mobile)
    values
      (?,?
      ,?,?,?)
     , 
      (?,?
      ,?,?,?)
  
//XMLStatementBuilder类,解析某个sql 如:<select>....</select>
public void parseStatementNode() {
     //省略

    //我们主要看这一行代码
    SqlSource sqlSource = langDriver.createSqlSource(configuration, context, parameterTypeClass);
   // 省略
  }

 先说结论:什么时候我们的SqlSource用DynamicSqlSource?只要我们含有 元素节点(比如:<if>,<where>)或者我们的文本中含有 '${}'时,我们就使用动态的sqlSource。

//默认的用这个驱动,当然这儿我们可以通过配置,使用别的驱动来解析xml
// XMLLanguageDriver类
@Override
  public SqlSource createSqlSource(Configuration configuration, XNode script, Class<?> parameterType) {
    XMLScriptBuilder builder = new XMLScriptBuilder(configuration, script, parameterType);
    return builder.parseScriptNode();
  }
/**
   context的内容如下:
  <select resultType="learn.User" parameterType="String" id="selectUserByIdFromTable">
    select * from ${tableName} where usercode = #{id}
  </select>

*/
public SqlSource parseScriptNode() {
    //重点看这一行代码,里面负责给isDynamic赋值
    MixedSqlNode rootSqlNode = parseDynamicTags(context);
    SqlSource sqlSource;
    if (isDynamic) {
      sqlSource = new DynamicSqlSource(configuration, rootSqlNode);
    } else {
      sqlSource = new RawSqlSource(configuration, rootSqlNode, parameterType);
    }
    return sqlSource;
  }
 protected MixedSqlNode parseDynamicTags(XNode node) {
    //用于解析某一个sql语句 如: <select>....</select>
    List<SqlNode> contents = new ArrayList<>();
    NodeList children = node.getNode().getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
      XNode child = node.newXNode(children.item(i));
      if (child.getNode().getNodeType() == Node.CDATA_SECTION_NODE || child.getNode().getNodeType() == Node.TEXT_NODE) {
// 这是解析xml文档中的文本。比如 我们的
// select * from ${tableName} where usercode = #{id}
        String data = child.getStringBody("");
        TextSqlNode textSqlNode = new TextSqlNode(data);
        if (textSqlNode.isDynamic()) {//这个方法用于判断我们的sql文本是否是动态的sql,
//  只要有 ‘${}’存在,就是动态sql
          conte
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值