五月综合激情婷婷六月,日韩欧美国产一区不卡,他扒开我内裤强吻我下面视频 ,无套内射无矿码免费看黄,天天躁,日日躁,狠狠躁

新聞動態(tài)

mybatis動態(tài)sql實現(xiàn)邏輯代碼詳解

發(fā)布日期:2022-01-31 11:18 | 文章來源:CSDN

mybatis通過將sql配置xml文件中,通過解析xml動態(tài)標簽來實現(xiàn)動態(tài)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標簽編寫規(guī)則

<!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的 驗證規(guī)則進行xml格式驗證

setNamespaceAware 設(shè)置忽略命名空間
對于xml文件的命名空間的定義,詳見
https://www.jb51.net/article/219617.htm

private Document buildXml(InputStream scriptFile)
				throws ParserConfigurationException, SAXException, IOException {
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();
			//默認情況下,解析器不驗證文檔。將這個參數(shù)設(shè)置為 true 可打開驗證功能。
			factory.setValidating(true);
			//是否設(shè)置命名空間
			factory.setNamespaceAware(false);
			//確定是否要忽略文件中的注釋。其默認值為 false。
			factory.setIgnoringComments(true);
			//確定是否要忽略元素內(nèi)容中的空白(類似于瀏覽器對待 HTML 的方式)。其默認值為 false。
			factory.setIgnoringElementContentWhitespace(false);
			//定解析器是否要將 CDATA 節(jié)點轉(zhuǎn)換為文本,以及是否要和周圍的文本節(jié)點合并(如果適用的話)。其默認值為 false。
			factory.setCoalescing(false);
			//確定是否要展開外部實體引用。如果為 true,外部數(shù)據(jù)將插入文檔。其默認值為 true
			factory.setExpandEntityReferences(true);
			DocumentBuilder builder = factory.newDocumentBuilder();
			//設(shè)置驗證規(guī)則文件 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());
				}
			});
			//設(shè)置錯誤解析器
			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 文件解析

到此這篇關(guān)于mybatis動態(tài)sql實現(xiàn)邏輯代碼詳解的文章就介紹到這了,更多相關(guān)mybatis動態(tài)sql內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!

美國服務(wù)器租用

版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部