在ibatis框架中 拼接sql語句,動態查詢 .
阿新 • • 發佈:2019-02-15
在ibatis中使用安全的拼接語句,動態查詢
ibatis比JDBC的優勢之一,安全高效
說明文字在註釋中
<select id="selectAllProducts" parameterClass="Product" resultMap="ProductResult">
select id,note from Product
<dynamic prepend="WHERE"><!-- isNotNull判斷引數是否存在,Integer型別 --><isNotNull property="id"><!-- isGreaterThan判斷引數是否大於compareValue,isGreaterEquals是大於等於 --><isGreaterThan prepend=" and " property="id" compareValue="0">
id = #id#
</isGreaterThan></isNotNull><!-- isNotEmpty判斷字串不為空,isEmpty可以判斷字串為空 --><isNotEmpty prepend=" and " property="note"><!-- 模糊查詢不能用#,#在是用prepareStatement的?插入引數,$是文字替換 -->
note like '%$note$%'
</isNotEmpty></dynamic></select>
用Map傳引數
<select id="selectAllProducts" parameterClass="java.util.HashMap" resultMap="ProductResult">
select id,note from Product
<dynamic prepend="WHERE"><!-- isPropertyAvailable判斷屬性是否有效 --><isPropertyAvailable property="id"><isNotNull property="id"><!-- isLessThan判斷引數是否小於compareValue,isLessEquals是小於等於 --><isLessThan prepend=" and " property="id" compareValue="10">
id = #id#
</isLessThan></isNotNull></isPropertyAvailable></dynamic></select>
ibatis比JDBC的優勢之一,安全高效
說明文字在註釋中
<select id="selectAllProducts" parameterClass="Product" resultMap="ProductResult">
select id,note from Product
<dynamic prepend="WHERE"><!-- isNotNull判斷引數是否存在,Integer型別 --><isNotNull property="id"><!-- isGreaterThan判斷引數是否大於compareValue,isGreaterEquals是大於等於
id = #id#
</isGreaterThan></isNotNull><!-- isNotEmpty判斷字串不為空,isEmpty可以判斷字串為空 --><isNotEmpty prepend=" and " property="note"><!-- 模糊查詢不能用#,#在是用prepareStatement的?插入引數,$是文字替換
note like '%$note$%'
</isNotEmpty></dynamic></select>
用Map傳引數
<select id="selectAllProducts" parameterClass="java.util.HashMap" resultMap="ProductResult">
select id,note from Product
<dynamic prepend="WHERE"><!-- isPropertyAvailable判斷屬性是否有效
id = #id#
</isLessThan></isNotNull></isPropertyAvailable></dynamic></select>