有没有办法在SpringBoot上检查xml文件是否有公共查询条件?

bsxbgnwa  于 2023-08-02  发布在  Spring
关注(0)|答案(1)|浏览(120)
<select id="findById" resultType="foo">
   SELECT *
   FROM foo_table
   WHERE id = #{id}
   limit 1
</select>

<select id="findByStauts" resultType="foo">
   SELECT *
   FROM foo_table
   WHERE status = #{status}
    limit 1
</select>

字符串
我想确保条件'limit 1'在所有查询中。
有没有办法在Spring Test中进行检查?

mftmpeh8

mftmpeh81#

您可以使用xPath遍历这些XML标记,并检查limit 1是否在其值内:
1.导入库并初始化对象:

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
XPath xPath = XPathFactory.newInstance().newXPath();
Document document;

字符串
1.将文件转换为字符串。有很多方法可以做到这一点。点击此处查看:https://mkyong.com/java/java-convert-file-to-string/的数据。
1.现在,我们将遍历所有的select标签,并检查它们是否包含limit 1。假设步骤2中的字符串名为“myXML”:

document = builder.parse(new InputSource(new StringReader(myXML)));

String xpathSelectTag = "yourXPathToSelectTag";
// Count how many select tags exist
Integer countTags = ((NodeList) xPath.evaluate(xpathSelectTag, document, XPathConstants.NODESET)).getLength();

// loop through tags
String selectValue = "";
for (int i = 1; i <= countTags; i++) {
    selectValue = xPath.evaluate(xpathSelectTag+"["+i+"]", document);
    if (selectValue.contains("limit 1")) {
        // your code
    }
}


在这个代码块xPath.evaluate(xpathSelectTag+"["+i+"]"中,我们取i索引所在的select标签的值。所以,我们遍历所有这些。
请评论任何问题,以便我可以帮助你。

相关问题