org.mybatis.generator.api.dom.xml.Document Java Examples

The following examples show how to use org.mybatis.generator.api.dom.xml.Document. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SelectOneByExamplePlugin.java    From mybatis-generator-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * SQL Map Methods 生成
 * 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
 * @param document
 * @param introspectedTable
 * @return
 */
@Override
public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
    if (selectOneByExampleEle != null) {
        // hook
        if (PluginTools.getHook(ISelectOneByExamplePluginHook.class).sqlMapSelectOneByExampleWithoutBLOBsElementGenerated(document, selectOneByExampleEle, introspectedTable)) {
            // 添加到根节点
            FormatTools.addElementWithBestPosition(document.getRootElement(), selectOneByExampleEle);
            logger.debug("itfsw(查询单条数据插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加selectOneByExample方法。");
        }
    }

    if (selectOneByExampleWithBLOBsEle != null) {
        // hook
        if (PluginTools.getHook(ISelectOneByExamplePluginHook.class).sqlMapSelectOneByExampleWithBLOBsElementGenerated(document, selectOneByExampleWithBLOBsEle, introspectedTable)) {
            // 添加到根节点
            FormatTools.addElementWithBestPosition(document.getRootElement(), selectOneByExampleWithBLOBsEle);
            logger.debug("itfsw(查询单条数据插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加selectOneByExampleWithBLOBs方法。");
        }
    }

    return true;
}
 
Example #2
Source File: Configuration.java    From mybatis-generator-core-fix with Apache License 2.0 6 votes vote down vote up
/**
 * Builds an XML representation of this configuration. This can be used to
 * persist a programmatically generated configuration.
 * 
 * @return the XML representation of this configuration
 */
public Document toDocument() {
    // note that this method will not reconstruct a properties
    // element - that element is only used in XML parsing

    Document document = new Document(
            XmlConstants.MYBATIS_GENERATOR_CONFIG_PUBLIC_ID,
            XmlConstants.MYBATIS_GENERATOR_CONFIG_SYSTEM_ID);
    XmlElement rootElement = new XmlElement("generatorConfiguration"); //$NON-NLS-1$
    document.setRootElement(rootElement);

    for (String classPathEntry : classPathEntries) {
        XmlElement cpeElement = new XmlElement("classPathEntry"); //$NON-NLS-1$
        cpeElement.addAttribute(new Attribute("location", classPathEntry)); //$NON-NLS-1$
        rootElement.addElement(cpeElement);
    }

    for (Context context : contexts) {
        rootElement.addElement(context.toXmlElement());
    }

    return document;
}
 
Example #3
Source File: XMLMergePlugin.java    From dolphin with Apache License 2.0 6 votes vote down vote up
/**
 * 从MBG生成的DOM文档结构中找到与element代表同一节点的元素对象
 *
 * @param document generate xml dom tree
 * @param element  The dom4j element
 * @return The xml element correspond to dom4j element
 */
protected XmlElement findMatchedElementIn(Document document, org.dom4j.Element element) {
  org.dom4j.Attribute id = element.attribute("id");
  String idName = id.getName();
  String idValue = id.getValue();
  for (Element me : document.getRootElement().getElements()) {
    if (me instanceof XmlElement) {
      XmlElement xe = (XmlElement) me;
      for (Attribute ab : xe.getAttributes()) {
        if (StringUtils.equals(idName, ab.getName()) && StringUtils.equals(idValue, ab.getValue())) {
          return xe;
        }
      }
    }
  }
  return null;
}
 
Example #4
Source File: IntrospectedTableMyBatis3Impl.java    From mybatis-generator-core-fix with Apache License 2.0 6 votes vote down vote up
@Override
public List<GeneratedXmlFile> getGeneratedXmlFiles() {
    List<GeneratedXmlFile> answer = new ArrayList<GeneratedXmlFile>();

    if (xmlMapperGenerator != null) {
        Document document = xmlMapperGenerator.getDocument();
        GeneratedXmlFile gxf = new GeneratedXmlFile(document,
            getMyBatis3XmlMapperFileName(), getMyBatis3XmlMapperPackage(),
            context.getSqlMapGeneratorConfiguration().getTargetProject(),
            true, context.getXmlFormatter());
        if (context.getPlugins().sqlMapGenerated(gxf, this)) {
            answer.add(gxf);
        }
    }

    return answer;
}
 
Example #5
Source File: IntrospectedTableIbatis2Java2Impl.java    From mybatis-generator-plus with Apache License 2.0 6 votes vote down vote up
@Override
public List<GeneratedXmlFile> getGeneratedXmlFiles() {
	// 让isMergeable属性可配置 add by wushuai begin
	String isMergeableStr = context.getSqlMapGeneratorConfiguration().getProperty("isMergeable");
	boolean isMergeable = false;
	if ("true".equalsIgnoreCase(isMergeableStr)) {
		isMergeable = true;
	}
	// 让isMergeable属性可配置 add by wushuai end

	List<GeneratedXmlFile> answer = new ArrayList<GeneratedXmlFile>();

	Document document = sqlMapGenerator.getDocument();
	GeneratedXmlFile gxf = new GeneratedXmlFile(document, getIbatis2SqlMapFileName(), getIbatis2SqlMapPackage(), context.getSqlMapGeneratorConfiguration().getTargetProject(), isMergeable,context.getXmlFormatter());
	if (context.getPlugins().sqlMapGenerated(gxf, this)) {
		answer.add(gxf);
	}

	return answer;
}
 
Example #6
Source File: IntrospectedTableMyBatis3Impl.java    From mybatis-generator-plus with Apache License 2.0 6 votes vote down vote up
@Override
public List<GeneratedXmlFile> getGeneratedXmlFiles() {
	// 让isMergeable属性可配置 add by wushuai begin
	String isMergeableStr = context.getSqlMapGeneratorConfiguration().getProperty("isMergeable");
	boolean isMergeable = false;
	if ("true".equalsIgnoreCase(isMergeableStr)) {
		isMergeable = true;
	}
	// 让isMergeable属性可配置 add by wushuai end
	List<GeneratedXmlFile> answer = new ArrayList<GeneratedXmlFile>();

	if (xmlMapperGenerator != null) {
		Document document = xmlMapperGenerator.getDocument();
		GeneratedXmlFile gxf = new GeneratedXmlFile(document, getMyBatis3XmlMapperFileName(), getMyBatis3XmlMapperPackage(), context.getSqlMapGeneratorConfiguration().getTargetProject(),isMergeable, context.getXmlFormatter());
		if (context.getPlugins().sqlMapGenerated(gxf, this)) {
			answer.add(gxf);
		}
	}

	return answer;
}
 
Example #7
Source File: SqlServerSupport.java    From mybatis-generator-plus with Apache License 2.0 6 votes vote down vote up
/**
 * 向&lt;mapper&gt;的子节点中添加内容支持批量和分页查询的sql代码块
 *
 * @author 吴帅
 * @parameter @param document
 * @parameter @param introspectedTable
 * @createDate 2015年9月29日 上午10:20:11
 */
@Override
public void sqlDialect(Document document, IntrospectedTable introspectedTable) {
    XmlElement parentElement = document.getRootElement();
    // 1.1 产生sqlserver分页的前缀语句块
    XmlElement paginationPrefixElement = new XmlElement("sql");
    paginationPrefixElement.addAttribute(new Attribute("id", "SqlServerDialectPrefix"));
    XmlElement prefixIf = new XmlElement("if");
    prefixIf.addAttribute(new Attribute("test", "offset != null and end != null"));
    prefixIf.addElement(new TextElement("SELECT * FROM ("));
    paginationPrefixElement.addElement(prefixIf);
    parentElement.addElement(paginationPrefixElement);

    // 1.2 产生sqlserver分页的后缀语句块
    XmlElement paginationSuffixElement = new XmlElement("sql");
    paginationSuffixElement.addAttribute(new Attribute("id", "SqlServerDialectSuffix"));
    XmlElement suffixIf = new XmlElement("if");
    suffixIf.addAttribute(new Attribute("test", "offset != null and end != null"));
    suffixIf.addElement(new TextElement("<![CDATA[) _pagination_tab WHERE _pagination_rownumber >=#{offset} and _pagination_rownumber < #{end}]]>"));
    paginationSuffixElement.addElement(suffixIf);
    parentElement.addElement(paginationSuffixElement);

    // 2.增加批量插入的xml配置
    addBatchInsertXml(document, introspectedTable);
}
 
Example #8
Source File: IntrospectedTableMyBatis3Impl.java    From mapper-generator-javafx with Apache License 2.0 6 votes vote down vote up
@Override
public List<GeneratedXmlFile> getGeneratedXmlFiles() {
    List<GeneratedXmlFile> answer = new ArrayList<>();

    if (xmlMapperGenerator != null) {
        Document document = xmlMapperGenerator.getDocument();
        GeneratedXmlFile gxf = new GeneratedXmlFile(document,
                getMyBatis3XmlMapperFileName(), getMyBatis3XmlMapperPackage(),
                context.getSqlMapGeneratorConfiguration().getTargetProject(),
                true, context.getXmlFormatter());
        if (context.getPlugins().sqlMapGenerated(gxf, this)) {
            answer.add(gxf);
        }
    }

    return answer;
}
 
Example #9
Source File: CommentsWavePlugin.java    From dolphin with Apache License 2.0 6 votes vote down vote up
@Override
public boolean sqlMapGenerated(GeneratedXmlFile sqlMap, IntrospectedTable introspectedTable) {
  try {
    // use reflect to fix the root comment
    Document document = (Document) FieldUtils.readDeclaredField(sqlMap, "document", true);
    ExtendedDocument extendedDocument = new ExtendedDocument(document);
    FieldUtils.writeDeclaredField(sqlMap, "document", extendedDocument, true);
    if (context.getCommentGenerator() instanceof CommentGenerator) {
      CommentGenerator cg = (CommentGenerator) context.getCommentGenerator();
      cg.addSqlMapFileComment(extendedDocument);
    }
  } catch (IllegalAccessException e) {
    e.printStackTrace();
  }
  return true;
}
 
Example #10
Source File: PostgreBatchUpdatePlugin.java    From hui-mybatis-generator-plugins with Apache License 2.0 5 votes vote down vote up
@Override
public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
    if (introspectedTable.getTargetRuntime().equals(IntrospectedTable.TargetRuntime.MYBATIS3)) {
        addSqlMapper(document, introspectedTable);
    }
    return super.sqlMapDocumentGenerated(document, introspectedTable);
}
 
Example #11
Source File: GeneratedXmlFile.java    From mybatis-generator-plus with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param document
 * @param fileName
 * @param targetPackage
 * @param targetProject
 * @param isMergeable
 *            true if the file can be merged by the built in XML file
 *            merger.
 */
public GeneratedXmlFile(Document document, String fileName,
        String targetPackage, String targetProject, boolean isMergeable,
        XmlFormatter xmlFormatter) {
    super(targetProject);
    this.document = document;
    this.fileName = fileName;
    this.targetPackage = targetPackage;
    this.isMergeable = isMergeable;
    this.xmlFormatter = xmlFormatter;
}
 
Example #12
Source File: BaseBatchOperatorPlugin.java    From hui-mybatis-generator-plugins with Apache License 2.0 5 votes vote down vote up
@Override
public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
    if (BaseGenTool.isMybatisMode(introspectedTable)) {
        addSqlMapper(document, introspectedTable);
    }
    return super.sqlMapDocumentGenerated(document, introspectedTable);
}
 
Example #13
Source File: PluginAggregator.java    From mybatis-generator-plus with Apache License 2.0 5 votes vote down vote up
public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
	boolean rc = true;

	for (Plugin plugin : plugins) {
		if (!plugin.sqlMapDocumentGenerated(document, introspectedTable)) {
			rc = false;
			break;
		}
	}

	return rc;
}
 
Example #14
Source File: RowBoundsPlugin.java    From mybatis-generator-plus with Apache License 2.0 5 votes vote down vote up
/**
 * We'll override this method and add any new elements generated by
 * previous calls
 */
@Override
public boolean sqlMapDocumentGenerated(Document document,
        IntrospectedTable introspectedTable) {
    List<XmlElement> elements = elementsToAdd.get(introspectedTable.getFullyQualifiedTable());
    if (elements != null) {
        for (XmlElement element : elements) {
            document.getRootElement().addElement(element);
        }
    }

    return true;
}
 
Example #15
Source File: OracleBatchInsertPlugin.java    From hui-mybatis-generator-plugins with Apache License 2.0 5 votes vote down vote up
@Override
public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
    if (introspectedTable.getTargetRuntime().equals(IntrospectedTable.TargetRuntime.MYBATIS3)) {
        addSqlMapper(document, introspectedTable);
    }
    return super.sqlMapDocumentGenerated(document, introspectedTable);
}
 
Example #16
Source File: GeneratedXmlFile.java    From mapper-generator-javafx with Apache License 2.0 5 votes vote down vote up
public GeneratedXmlFile(Document document, String fileName,
        String targetPackage, String targetProject, boolean isMergeable,
        XmlFormatter xmlFormatter) {
    super(targetProject);
    this.document = document;
    this.fileName = fileName;
    this.targetPackage = targetPackage;
    this.isMergeable = isMergeable;
    this.xmlFormatter = xmlFormatter;
}
 
Example #17
Source File: PluginAggregator.java    From mapper-generator-javafx with Apache License 2.0 5 votes vote down vote up
@Override
public boolean sqlMapDocumentGenerated(Document document,
        IntrospectedTable introspectedTable) {
    boolean rc = true;

    for (Plugin plugin : plugins) {
        if (!plugin.sqlMapDocumentGenerated(document, introspectedTable)) {
            rc = false;
            break;
        }
    }

    return rc;
}
 
Example #18
Source File: SimpleXMLMapperGenerator.java    From mapper-generator-javafx with Apache License 2.0 5 votes vote down vote up
@Override
public Document getDocument() {
    Document document = new Document(
            XmlConstants.MYBATIS3_MAPPER_PUBLIC_ID,
            XmlConstants.MYBATIS3_MAPPER_SYSTEM_ID);
    document.setRootElement(getSqlMapElement());

    if (!context.getPlugins().sqlMapDocumentGenerated(document,
            introspectedTable)) {
        document = null;
    }

    return document;
}
 
Example #19
Source File: BatchDeletePlugin.java    From hui-mybatis-generator-plugins with Apache License 2.0 5 votes vote down vote up
@Override
public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
    if (introspectedTable.getTargetRuntime().equals(IntrospectedTable.TargetRuntime.MYBATIS3)){
        addSqlMapper(document, introspectedTable);
    }
    return super.sqlMapDocumentGenerated(document, introspectedTable);
}
 
Example #20
Source File: CachePlugin.java    From mybatis-generator-core-fix with Apache License 2.0 5 votes vote down vote up
@Override
public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {

    XmlElement element = new XmlElement("cache"); //$NON-NLS-1$
    context.getCommentGenerator().addComment(element);

    for (CacheProperty cacheProperty : CacheProperty.values()) {
        addAttributeIfExists(element, introspectedTable, cacheProperty);
    }
    
    document.getRootElement().addElement(element);

    return true;
}
 
Example #21
Source File: HookAggregator.java    From mybatis-generator-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public boolean sqlMapSelectOneByExampleWithoutBLOBsElementGenerated(Document document, XmlElement element, IntrospectedTable introspectedTable) {
    for (ISelectOneByExamplePluginHook plugin : this.getPlugins(ISelectOneByExamplePluginHook.class)) {
        if (!plugin.sqlMapSelectOneByExampleWithoutBLOBsElementGenerated(document, element, introspectedTable)) {
            return false;
        }
    }
    return true;
}
 
Example #22
Source File: HookAggregator.java    From mybatis-generator-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public boolean sqlMapSelectOneByExampleWithBLOBsElementGenerated(Document document, XmlElement element, IntrospectedTable introspectedTable) {
    for (ISelectOneByExamplePluginHook plugin : this.getPlugins(ISelectOneByExamplePluginHook.class)) {
        if (!plugin.sqlMapSelectOneByExampleWithBLOBsElementGenerated(document, element, introspectedTable)) {
            return false;
        }
    }
    return true;
}
 
Example #23
Source File: HookAggregator.java    From mybatis-generator-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public boolean sqlMapLogicalDeleteByExampleElementGenerated(Document document, XmlElement element, IntrospectedColumn logicalDeleteColumn, String logicalDeleteValue, IntrospectedTable introspectedTable) {
    for (ILogicalDeletePluginHook plugin : this.getPlugins(ILogicalDeletePluginHook.class)) {
        if (!plugin.sqlMapLogicalDeleteByExampleElementGenerated(document, element, logicalDeleteColumn, logicalDeleteValue, introspectedTable)) {
            return false;
        }
    }
    return true;
}
 
Example #24
Source File: HookAggregator.java    From mybatis-generator-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public boolean sqlMapSelectByExampleSelectiveElementGenerated(Document document, XmlElement element, IntrospectedTable introspectedTable) {
    for (ISelectSelectivePluginHook plugin : this.getPlugins(ISelectSelectivePluginHook.class)) {
        if (!plugin.sqlMapSelectByExampleSelectiveElementGenerated(document, element, introspectedTable)) {
            return false;
        }
    }
    return true;
}
 
Example #25
Source File: XMLMapperGenerator.java    From mybatis-generator-core-fix with Apache License 2.0 5 votes vote down vote up
@Override
public Document getDocument() {
    Document document = new Document(
            XmlConstants.MYBATIS3_MAPPER_PUBLIC_ID,
            XmlConstants.MYBATIS3_MAPPER_SYSTEM_ID);
    document.setRootElement(getSqlMapElement());

    if (!context.getPlugins().sqlMapDocumentGenerated(document,
            introspectedTable)) {
        document = null;
    }

    return document;
}
 
Example #26
Source File: SimpleXMLMapperGenerator.java    From mybatis-generator-core-fix with Apache License 2.0 5 votes vote down vote up
@Override
public Document getDocument() {
    Document document = new Document(
            XmlConstants.MYBATIS3_MAPPER_PUBLIC_ID,
            XmlConstants.MYBATIS3_MAPPER_SYSTEM_ID);
    document.setRootElement(getSqlMapElement());

    if (!context.getPlugins().sqlMapDocumentGenerated(document,
            introspectedTable)) {
        document = null;
    }

    return document;
}
 
Example #27
Source File: SqlMapGenerator.java    From mybatis-generator-core-fix with Apache License 2.0 5 votes vote down vote up
@Override
public Document getDocument() {
    Document document = new Document(
            XmlConstants.IBATIS2_SQL_MAP_PUBLIC_ID,
            XmlConstants.IBATIS2_SQL_MAP_SYSTEM_ID);
    document.setRootElement(getSqlMapElement());

    if (!context.getPlugins().sqlMapDocumentGenerated(document,
            introspectedTable)) {
        document = null;
    }

    return document;
}
 
Example #28
Source File: CommentPluginTest.java    From mybatis-generator-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * 测试配置了模板参数转换
 */
@Test
public void testGenerateWithTemplate() throws Exception {
    MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/CommentPlugin/mybatis-generator.xml");
    MyBatisGenerator myBatisGenerator = tool.generate();

    // java中的注释
    for (GeneratedJavaFile file : myBatisGenerator.getGeneratedJavaFiles()) {
        if (file.getFileName().equals("Tb.java")) {
            TopLevelClass topLevelClass = (TopLevelClass) file.getCompilationUnit();
            // addJavaFileComment
            Assert.assertEquals(topLevelClass.getFileCommentLines().get(0), "TestAddJavaFileComment:Tb:" + new SimpleDateFormat("yyyy-MM").format(new Date()));
            // addFieldComment 同时测试 if 判断和 mbg
            Field id = topLevelClass.getFields().get(0);
            Assert.assertEquals(id.getJavaDocLines().get(0), "注释1");
            Assert.assertEquals(id.getJavaDocLines().get(1), MergeConstants.NEW_ELEMENT_TAG);
            // addGeneralMethodComment
            Method cons = topLevelClass.getMethods().get(0);
            Assert.assertEquals(cons.getJavaDocLines().get(0), "addGeneralMethodComment:Tb:tb");
            // addSetterComment
            Method setter = topLevelClass.getMethods().get(5);
            Assert.assertEquals(setter.getJavaDocLines().get(0), "addSetterComment:field1:field1");
        }
    }

    // xml注释
    ObjectUtil xml = new ObjectUtil(myBatisGenerator.getGeneratedXmlFiles().get(0));
    Document doc = (Document) xml.get("document");
    List<Element> els = ((XmlElement) (doc.getRootElement().getElements().get(0))).getElements();
    String comment = ((TextElement) els.get(0)).getContent();
    Assert.assertEquals(comment, "addComment:BaseResultMap");
}
 
Example #29
Source File: IntrospectedTableIbatis2Java2Impl.java    From mybatis-generator-core-fix with Apache License 2.0 5 votes vote down vote up
@Override
public List<GeneratedXmlFile> getGeneratedXmlFiles() {
    List<GeneratedXmlFile> answer = new ArrayList<GeneratedXmlFile>();

    Document document = sqlMapGenerator.getDocument();
    GeneratedXmlFile gxf = new GeneratedXmlFile(document,
            getIbatis2SqlMapFileName(), getIbatis2SqlMapPackage(), context
                    .getSqlMapGeneratorConfiguration().getTargetProject(),
            true, context.getXmlFormatter());
    if (context.getPlugins().sqlMapGenerated(gxf, this)) {
        answer.add(gxf);
    }

    return answer;
}
 
Example #30
Source File: PluginAggregator.java    From mybatis-generator-core-fix with Apache License 2.0 5 votes vote down vote up
public boolean sqlMapDocumentGenerated(Document document,
        IntrospectedTable introspectedTable) {
    boolean rc = true;

    for (Plugin plugin : plugins) {
        if (!plugin.sqlMapDocumentGenerated(document, introspectedTable)) {
            rc = false;
            break;
        }
    }

    return rc;
}