@Test
  public void testThatFilesAreTheSameAfterMerge() throws Exception {
    DefaultXmlFormatter xmlFormatter = new DefaultXmlFormatter();
    Properties p = new Properties();
    p.setProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_DATE, "true");
    CommentGenerator commentGenerator = new DefaultCommentGenerator();
    commentGenerator.addConfigurationProperties(p);

    Document document =
        new Document(
            XmlConstants.MYBATIS3_MAPPER_PUBLIC_ID, XmlConstants.MYBATIS3_MAPPER_SYSTEM_ID);
    document.setRootElement(getSqlMapElement(commentGenerator));

    GeneratedXmlFile generatedFile1 =
        new GeneratedXmlFile(
            document, "TestMapper.xml", "org.mybatis.test", "src", true, xmlFormatter);
    InputSource is1 = new InputSource(new StringReader(generatedFile1.getFormattedContent()));

    GeneratedXmlFile generatedFile2 =
        new GeneratedXmlFile(
            document, "TestMapper.xml", "org.mybatis.test", "src", true, xmlFormatter);
    InputSource is2 = new InputSource(new StringReader(generatedFile2.getFormattedContent()));

    String mergedSource = XmlFileMergerJaxp.getMergedSource(is1, is2, "TestMapper.xml");

    assertEquals(generatedFile1.getFormattedContent(), mergedSource);
  }
Beispiel #2
0
  @Override
  public List<GeneratedXmlFile> contextGenerateAdditionalXmlFiles() {
    Document document =
        new Document(
            XmlConstants.IBATIS2_SQL_MAP_CONFIG_PUBLIC_ID,
            XmlConstants.IBATIS2_SQL_MAP_CONFIG_SYSTEM_ID);

    XmlElement root = new XmlElement("sqlMapConfig"); // $NON-NLS-1$
    document.setRootElement(root);

    root.addElement(new TextElement("<!--")); // $NON-NLS-1$
    root.addElement(
        new TextElement("  This file is generated by MyBatis Generator.")); // $NON-NLS-1$
    root.addElement(
        new TextElement(
            "  This file is the shell of an SqlMapConfig file - in many cases you will need to add")); //$NON-NLS-1$
    root.addElement(
        new TextElement("    to this file before it is usable by iBATIS.")); // $NON-NLS-1$

    StringBuilder sb = new StringBuilder();
    sb.append("  This file was generated on "); // $NON-NLS-1$
    sb.append(new Date());
    sb.append('.');
    root.addElement(new TextElement(sb.toString()));

    root.addElement(new TextElement("-->")); // $NON-NLS-1$

    XmlElement settings = new XmlElement("settings"); // $NON-NLS-1$
    settings.addAttribute(
        new Attribute("useStatementNamespaces", "true")); // $NON-NLS-1$ //$NON-NLS-2$
    root.addElement(settings);

    XmlElement sqlMap;
    for (String sqlMapFile : sqlMapFiles) {
      sqlMap = new XmlElement("sqlMap"); // $NON-NLS-1$
      sqlMap.addAttribute(new Attribute("resource", sqlMapFile)); // $NON-NLS-1$
      root.addElement(sqlMap);
    }

    GeneratedXmlFile gxf =
        new GeneratedXmlFile(
            document,
            properties.getProperty("fileName", "SqlMapConfig.xml"), // $NON-NLS-1$ //$NON-NLS-2$
            properties.getProperty("targetPackage"), // $NON-NLS-1$
            properties.getProperty("targetProject"), // $NON-NLS-1$
            false,
            context.getXmlFormatter());

    List<GeneratedXmlFile> answer = new ArrayList<GeneratedXmlFile>(1);
    answer.add(gxf);

    return answer;
  }
  @Override
  public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
    XmlElement parentElement = document.getRootElement();

    // 产生分页语句前半部分
    XmlElement paginationPrefixElement = new XmlElement("sql");
    paginationPrefixElement.addAttribute(new Attribute("id", "OracleDialectPrefix"));
    XmlElement pageStart = new XmlElement("if");
    pageStart.addAttribute(new Attribute("test", "page != null"));
    XmlElement pageDialect1 = new XmlElement("if");
    pageDialect1.addAttribute(new Attribute("test", "dialect == 'oracle'"));
    pageStart.addElement(pageDialect1);
    pageDialect1.addElement(
        new TextElement("select * from ( select row_.*, rownum rownum_ from ( "));
    paginationPrefixElement.addElement(pageStart);
    parentElement.addElement(paginationPrefixElement);

    // 产生分页语句后半部分
    XmlElement paginationSuffixElement = new XmlElement("sql");
    paginationSuffixElement.addAttribute(new Attribute("id", "OracleDialectSuffix"));
    XmlElement pageEnd = new XmlElement("if");
    pageEnd.addAttribute(new Attribute("test", "page != null"));
    XmlElement pageDialect2 = new XmlElement("if");
    pageDialect2.addAttribute(new Attribute("test", "dialect == 'oracle'"));
    pageEnd.addElement(pageDialect2);
    pageDialect2.addElement(
        new TextElement(
            "<![CDATA[ ) row_ ) where rownum_ > #{page.start} and rownum_ <= #{page.pageSize} ]]>"));

    // ----- mysql语句。
    XmlElement mysqlDialect = new XmlElement("if");
    mysqlDialect.addAttribute(new Attribute("test", "dialect == 'mysql'"));
    pageEnd.addElement(mysqlDialect);
    mysqlDialect.addElement(new TextElement("limit #{page.start} , #{page.pageSize}"));
    paginationSuffixElement.addElement(pageEnd);

    parentElement.addElement(paginationSuffixElement);

    return super.sqlMapDocumentGenerated(document, introspectedTable);
  }