mybatis動態sql實現邏輯代碼詳解
mybatis通過將sql配置xml文件中,通過解析xml動態標簽來實現動態sql
如下樣例 xml文件
<?xml version = "1.0" ?> <!DOCTYPE script SYSTEM "script-1.0.dtd"> <script namespace="user"> <common id="commonOrder"> order by id desc </common> <sql id="queryUser"> select * from user <where> <if test='id != null '> id = #{id} </if> <if test="names != null and names.size() >0"> and name in <foreach collection="names" item="item" separator="," open="(" close=")"> ${item} </foreach> </if> </where> <ref id="commonOrder"/> </sql> <sql id="updateUser"> update user set name = ${name} <where> <if test='id != null '> id = #{id} </if> </where> </sql> </script>
1.xml文件讀取
xml標簽編寫規則
<!ELEMENT script (#PCDATA | sql | common)*> <!ATTLIST script namespace CDATA #REQUIRED > <!ELEMENT sql (#PCDATA | trim | where | set | foreach | choose | if | ref)*> <!ATTLIST sql id CDATA #REQUIRED > <!ELEMENT common (#PCDATA | trim | where | set | foreach | choose | if)*> <!ATTLIST common id CDATA #REQUIRED > <!ELEMENT ref (#PCDATA)*> <!ATTLIST ref id CDATA #REQUIRED > <!ELEMENT trim (#PCDATA | trim | where | set | foreach | choose | if)*> <!ATTLIST trim prefix CDATA #IMPLIED prefixOverrides CDATA #IMPLIED suffix CDATA #IMPLIED suffixOverrides CDATA #IMPLIED > <!ELEMENT where (#PCDATA | trim | where | set | foreach | choose | if | ref)*> <!ELEMENT set (#PCDATA | trim | where | set | foreach | choose | if)*> <!ELEMENT foreach (#PCDATA | trim | where | set | foreach | choose | if)*> <!ATTLIST foreach collection CDATA #REQUIRED item CDATA #IMPLIED index CDATA #IMPLIED open CDATA #IMPLIED close CDATA #IMPLIED separator CDATA #IMPLIED > <!ELEMENT choose (when* , otherwise?)> <!ELEMENT when (#PCDATA | trim | where | set | foreach | choose | if)*> <!ATTLIST when test CDATA #REQUIRED > <!ELEMENT otherwise (#PCDATA | trim | where | set | foreach | choose | if)*> <!ELEMENT if (#PCDATA | trim | where | set | foreach | choose | if)*> <!ATTLIST if test CDATA #REQUIRED >
DocumentBuilderFactory 是jdk自帶的解析xml文件操作,位於 javax.xml.parsers 包,如圖其是一個抽象類,因此無法被實例化
需要通過 newInstance 來實例化
實例化對象後的屬性 setValidating(true); 即通過xml的 驗證規則進行xml格式驗證
setNamespaceAware 設置忽略命名空間
對於xml文件的命名空間的定義,詳見
https://www.jb51.net/article/219617.htm
private Document buildXml(InputStream scriptFile) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance(); //默認情況下,解析器不驗證文檔。將這個參數設置為 true 可打開驗證功能。 factory.setValidating(true); //是否設置命名空間 factory.setNamespaceAware(false); //確定是否要忽略文件中的註釋。其默認值為 false。 factory.setIgnoringComments(true); //確定是否要忽略元素內容中的空白(類似於瀏覽器對待 HTML 的方式)。其默認值為 false。 factory.setIgnoringElementContentWhitespace(false); //定解析器是否要將 CDATA 節點轉換為文本,以及是否要和周圍的文本節點合並(如果適用的話)。其默認值為 false。 factory.setCoalescing(false); //確定是否要展開外部實體引用。如果為 true,外部數據將插入文檔。其默認值為 true factory.setExpandEntityReferences(true); DocumentBuilder builder = factory.newDocumentBuilder(); //設置驗證規則文件 dtd文件 builder.setEntityResolver(new EntityResolver() { @Override public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { return new InputSource(new ClassPathResource("script-1.0.dtd").getInputStream()); } }); //設置錯誤解析器 builder.setErrorHandler(new ErrorHandler() { @Override public void error(SAXParseException exception) throws SAXException { throw exception; } @Override public void fatalError(SAXParseException exception) throws SAXException { throw exception; } @Override public void warning(SAXParseException exception) throws SAXException { } }); return builder.parse(scriptFile); }
2.xml 文件解析
到此這篇關於mybatis動態sql實現邏輯代碼詳解的文章就介紹到這瞭,更多相關mybatis動態sql內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!