mybatis build下的xml 解析

1.在mybatis中如何获取的对应的sql的方法?
测试代码:

  @Test
  void testShrinkWhitespacesInSqlIsFalse() {
    SqlSource sqlSource = sqlSourceBuilder.parse(sqlFromXml, null, null);
    BoundSql boundSql = sqlSource.getBoundSql(null);
    String actual = boundSql.getSql();
    Assertions.assertEquals(sqlFromXml, actual);
  }

在这里插入图片描述
我们可以看到是sqlSourceBuilder是创建这个类的实现的。

当我们的配置出错了,会出现什么样的结果呢?

 @Test
  void unknownSettings() {
    final String MAPPER_CONFIG = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
            + "<!DOCTYPE configuration PUBLIC \"-//mybatis.org//DTD Config 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-config.dtd\">\n"
            + "<configuration>\n"
            + "  <settings>\n"
            + "    <setting name=\"foo\" value=\"bar\"/>\n"
            + "  </settings>\n"
            + "</configuration>\n";

    XMLConfigBuilder builder = new XMLConfigBuilder(new StringReader(MAPPER_CONFIG));
    when(builder::parse);
    then(caughtException()).isInstanceOf(BuilderException.class)
      .hasMessageContaining("The setting foo is not known.  Make sure you spelled it correctly (case sensitive).");
  }

这里捕捉到了setting 文件为空。
如上是xml 配置的建立的测试案例。

那么一个文件如何正确,mybatis 如何进行解析的呢?

 <resultMap id="complexAuthorId" type="org.apache.ibatis.domain.blog.ComplexImmutableAuthorId">
        <constructor>
            <idArg column="id" javaType="_int" />
            <idArg column="username" javaType="string" />
            <idArg column="password" javaType="string" />
            <idArg column="email" javaType="string" />
        </constructor>
    </resultMap>

    <resultMap id="selectComplexImmutableAuthor" type="org.apache.ibatis.domain.blog.ComplexImmutableAuthor">
        <constructor>
            <idArg javaType="org.apache.ibatis.domain.blog.ComplexImmutableAuthorId"
                resultMap="complexAuthorId" />
            <arg column="bio" javaType="string" />
            <arg column="favourite_section" javaType="org.apache.ibatis.domain.blog.Section" />
        </constructor>
    </resultMap>

    <select id="selectAllAuthors" resultType="org.apache.ibatis.domain.blog.Author">
        select * from author
    </select>

<select id="selectWithOptions" resultType="org.apache.ibatis.domain.blog.Author"
        fetchSize="200" timeout="10" statementType="PREPARED" resultSetType="SCROLL_SENSITIVE" flushCache="false" useCache="false">
        select * from author
    </select>
``
`
这都都是能够解析的。

```java
 @Test
  void shouldSuccessfullyLoadXMLMapperFile() throws Exception {
    Configuration configuration = new Configuration();
    String resource = "org/apache/ibatis/builder/AuthorMapper.xml";
    try (InputStream inputStream = Resources.getResourceAsStream(resource)) {
      XMLMapperBuilder builder = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments());
      builder.parse();
    }
  }

如下我们可以解析对应的文件下的selectWithOptions

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值