ibatis动态查询条件
ibatis的調試相對困難,出錯的時候主要依據是log4生成的log文件和出錯提示,這方面要能比較熟練的看懂.
下面這個配置基本上包含了最復雜的功能:分頁\搜索\排序\緩存\傳值Hash表\返回hash表\動態sql
如果對下面這段配置能信手粘來的話,那開發速度將會大大的提升.
<statement id="XinxiTable_SelectAll" listClass="ArrayList"?
?????????????? resultMap="SimpleXinxi" parameterClass="Hashtable" cacheModel="xinxi-cache" >
????? SELECT
??????? <dynamic prepend="top">
????????? <isNotEqual prepend="top" property="TopNum" compareValue = "0">
??????????? $TopNum$
????????? </isNotEqual>
??????? </dynamic>
????? *
????? FROM
????? (select a.[iXinxiID],a.[sXinxiTitle],a.[iXinxiClassId],b.[sClassName],
????? a.[dXinxiDate],a.[dXinxiYxq],a.[iXinxiHits],a.[sXinxiUser],a.[sRedirectUrl],
????? ROW_NUMBER() OVER(
????? <dynamic prepend="order by">
????? <isEqual prepend="order by" property="Sort" compareValue = "0">
??????? a.iXinxiID desc
????? </isEqual>
????? <isEqual prepend="order by" property="Sort" compareValue = "1">
??????? a.iXinxiID asc
????? </isEqual>
????? <isEqual prepend="order by" property="Sort" compareValue = "2">
??????? a.iXinxiHits desc
????? </isEqual>
????? <isEqual prepend="order by" property="Sort" compareValue = "3">
??????? a.iXinxiHits asc
????? </isEqual>
??? </dynamic>
????? ) as row
????? FROM
????? [dbo].[XinxiTable] as a,[dbo].[XinxiClass] as b
????? <dynamic prepend="where">
??????? <isParameterPresent>
????????? <isNotEmpty prepend="and" property="XinxiType" >
??????????? a.[iXinxiState]= $XinxiType$
????????? </isNotEmpty>
????????? <isNotEqual prepend="and" property="XinxiClass" compareValue = "0">
??????????? a.[iXinxiClassID]= $XinxiClass$
????????? </isNotEqual>
????????? <isEqual prepend="and" property="SearchType" compareValue = "1">
??????????? a.[sXinxiTitle] LIKE '%$Keyword$%'
????????? </isEqual>
????????? <isEqual prepend="and" property="SearchType" compareValue = "2">
??????????? (a.[sXinxiTitle] LIKE '%$Keyword$%' or a.[sXinxiContent] LIKE '%$Keyword$%')
????????? </isEqual>
??????? </isParameterPresent>
????? </dynamic>
????? and a.iXinxiClassId=b.iClassId
????? )a
????? <dynamic prepend="where">
??????? <isParameterPresent>
????????? <isEqual prepend="and" property="IsPage" compareValue = "1">
??????????? row between $PageLower$ and $PageUpper$
????????? </isEqual>
??????? </isParameterPresent>
????? </dynamic>
??? </statement>
ibatis動態查詢條件:
<select id="SelectEemployee" parameterClass="string" resultMap = "employee-result">
??????????? select * from employee
//動態SQL語句
??????????? ?<dynamic prepend="WHERE">
??????????????? ?<isParameterPresent>
??????????????????? ?? emp_id = #value#
??????????????? ?</isParameterPresent>
??????????? ? </dynamic>
??????? </select>
??? </statements>
</sqlMap>
/*
動態SQL的寫法:
開始 <dynamic
條件成立時前面要加的字符串 prepend ="字符串">
<屬性關鍵字? (見下表)
prepend="字符串"
判斷條件的對象屬性名 property="字符串"
如果是屬性關鍵字是比較條件時,字符串存放要比較的值compareValue="字符串">
要顯示的條件名
</屬性關鍵字>
結束</dynamic>
*/
/*
動態SQL的參數有
| 屬性關鍵字 | 含義 |
| <isEqual> | 如果參數相等于值則查詢條件有效。 |
| <isNotEqual> | 如果參數不等于值則查詢條件有效。 |
| <isGreaterThan> | 如果參數大于值則查詢條件有效。 |
| <isGreaterEqual> | 如果參數等于值則查詢條件有效。 |
| <isLessEqual> | 如果參數小于值則查詢條件有效。如下所示: <isLessEqual prepend = ”AND” property = ”age” compareValue = ”18” > ADOLESCENT = ‘TRUE’ </isLessEqual> |
| <isPropertyAvailable> | 如果參數有使用則查詢條件有效。 |
| <isNotPropertyAvailable> | 如果參數沒有使用則查詢條件有效。 |
| <isNull> | 如果參數為NULL則查詢條件有效。 |
| <isNotNull> | 如果參數不為NULL則查詢條件有效。 |
| <isEmpty> | 如果參數為空則查詢條件有效。 |
| <isNotEmpty> | 如果參數不為空則查詢條件有效。參數的數據類型為Collection、String 時參數不為NULL或“”。如下所示: <isNotEmpty prepend=”AND” property=”firstName” > FIRST_NAME=#firstName# </isNotEmpty> |
| <isParameterPresent> | 如果參數類不為NULL則查詢條件有效。 |
| <isNotParameterPresent> | Checks to see if the parameter object is not present (null). Example Usage: <isNotParameterPresent prepend=”AND”> EMPLOYEE_TYPE = ‘DEFAULT’ </isNotParameterPresent> |
ibatis如何出入動態傳入指定表和指定列查詢對應數據?
ibatis 的配置如下:
<select id="selectDataOfTable" resultClass="java.util.Hashtable" parameterClass="java.util.Map">
??? <![CDATA[
??? SELECT $column$ FROM $tableName$
??? ]]>
??? </select>
當傳出:tableName: CC_RPT_DF01
column?? :
TO_CHAR(MONTH_CODE,'yyyy-MM-dd hh:mi:ss') MONTH_CODE,decode(BUSINESS_PLACE_CODE,NULL,'',BUSINESS_PLACE_CODE)BUSINESS_PLACE_CODE,decode(PRICE_ID,NULL,'',PRICE_ID)PRICE_ID,decode(TRADE_CODE,NULL,'',TRADE_CODE)TRADE_CODE,decode(INTERVAL,NULL,'',INTERVAL)INTERVAL,decode(TOTAL_POWER,NULL,0,TOTAL_POWER)TOTAL_POWER,decode(TOTAL_POWER_YG,NULL,0,TOTAL_POWER_YG)TOTAL_POWER_YG,decode(TOTAL_POWER_WG,NULL,0,TOTAL_POWER_WG)TOTAL_POWER_WG,decode(FAVOR_POWER,NULL,0,FAVOR_POWER)FAVOR_POWER,decode(ADD_POWER,NULL,0,ADD_POWER)ADD_POWER,decode(TOTAL_FEE,NULL,0,TOTAL_FEE)TOTAL_FEE,decode(CONTENT_FEE,NULL,0,CONTENT_FEE)CONTENT_FEE,decode(CAPA,NULL,0,CAPA)CAPA,decode(CAPA_FEE,NULL,0,CAPA_FEE)CAPA_FEE,decode(NEED,NULL,0,NEED)NEED,decode(NEED_FEE,NULL,0,NEED_FEE)NEED_FEE,decode(BASE_FEE,NULL,0,BASE_FEE)BASE_FEE,decode(COS_ADD_FEE,NULL,0,COS_ADD_FEE)COS_ADD_FEE,decode(COS_REDUCE_FEE,NULL,0,COS_REDUCE_FEE)COS_REDUCE_FEE,decode(FAVOR_FEE,NULL,0,FAVOR_FEE)FAVOR_FEE,decode(ADD_FEE,NULL,0,ADD_FEE)ADD_FEE,decode(SANXIA_FEE,NULL,0,SANXIA_FEE)SANXIA_FEE,decode(CHENGSHI_FEE,NULL,0,CHENGSHI_FEE)CHENGSHI_FEE,decode(PROVINCE_FEE,NULL,0,PROVINCE_FEE)PROVINCE_FEE,decode(STATE_FEE,NULL,0,STATE_FEE)STATE_FEE,decode(NONGWANG_FEE,NULL,0,NONGWANG_FEE)NONGWANG_FEE,decode(ENERGY_FEE,NULL,0,ENERGY_FEE)ENERGY_FEE,decode(OTHER_FEE,NULL,0,OTHER_FEE)OTHER_FEE,decode(USER_COUNT,NULL,0,USER_COUNT)USER_COUNT
能查出CC_RPT_DF01 表對應 列? 的數據 。
當第二次傳入? tableName:CC_RPT_DF11
column?? :
TO_CHAR(MONTH_CODE,'yyyy-MM-dd hh:mi:ss') MONTH_CODE,decode(BUSINESS_PLACE_CODE,NULL,'',BUSINESS_PLACE_CODE)BUSINESS_PLACE_CODE,decode(VOLTAGE_LEVEL,NULL,'',VOLTAGE_LEVEL)VOLTAGE_LEVEL,decode(SALE_ID,NULL,'',SALE_ID)SALE_ID,decode(USER_NUM,NULL,0,USER_NUM)USER_NUM,decode(SUM_CAPA,NULL,0,SUM_CAPA)SUM_CAPA
Check the selectDataOfTable-AutoResultMap.?
--- Check the result mapping for the 'PRICE_ID' property.?
--- Cause: java.sql.SQLException: 列名無效; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:??
--- The error occurred in com/szp/eai/dataobject/Common.xml.?
--- The error occurred while applying a result map.?
第二次傳入的列名和數據庫表中一致。
我感覺好像 SELECT $column$ FROM $tableName$ 這中語句,ibatis記住了第一次出入的列,第二次傳入的新列沒有用。
要實現這中功能,ibatis該如何做?
答案:
這個問題是因為你查詢的sql的列是變化的,但是ibatis默認的會緩存RS中的meta信息,如果你第一次查詢的列和第二次查詢的列不一樣的話,那么第二次ibatis還會以第一次查詢的列為key從RS里面獲取數據,但是你的列是變化的,所以第二次取數據的時候,RS里面已經沒有了你第一次的那個列了,所以會出錯。 幸好ibatis 可以設置來改變這種緩存引起的問題,就是這個remapResults=true
<select id="" parameterClass="" resultClass="" remapResults="true">
</select>
iBATIS的select?標簽?#于?$區別------模糊查詢
?? 在IbatiS的select中有兩個占位符 # 和 $
?? 我們先看一個 一個查詢語句:
??? select * from user where Name = #userName#;
?? 我們用#這個占位符可以查詢到 我們想要的結果,可是當我們需要模糊查詢的時候該怎么辦呢?
?? select * from user where Name = ‘%#userName#%’(錯誤的寫法);如果我們這樣寫,程序在編譯的時候會報錯,因為 # 這個占位符前面還有別的 符號,#這個占位符是不允許這樣寫的,這個時候我們就可以用 $ 這個占位符:
?? select * from user where Name = ‘%$userName$%’(正確的模糊查詢寫法),這樣寫編譯器不會報錯,也能得到我們想要的結果。
??? 歸根結底的原因就是 # 占位符會把我們的SQL 語句翻譯成
??? select * from user where Name = ? 這樣的語句,然后在填充參數。
??? $ 占位符會把我們的SQL語句 翻譯成
??? select * from user where Name = '想要查詢的東西' 這個樣標準的SQL語句
總結
以上是生活随笔為你收集整理的ibatis动态查询条件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 遥知兄弟登高处(说一说遥知兄弟登高处的简
- 下一篇: 什么狗最可爱又黏人(什么狗最可爱)