Ibatis2.0使用說明——配置篇(3)

 
statement中的參數簡介:

1. parameterClass

parameterClass 屬性的值是Java類的全限定名(即包括類的包名)。parameterClass屬性是可選的,目的是限制輸入參數的類型爲指定的Java 類。雖然Parameter-class屬性是可選的,建議你爲每一個SQL都指定parameterClass。如果不指定parameterClass 參數,任何帶有合適屬性(get/set 方法)的Java Bean 都可以作爲輸入參數。如果你使用了parameterMap, 那麼你就不需要再使用parameterClass屬性了。
下面是例子:
例1:
<insert id="insertAuthor2" parameterClass="Author">
       INSERT INTO author (auth_name,auth_age,auth_tel,auth_address) VALUES (#name#,#age#,#telephone#,#address#)
</insert>
 
在上面的語句中,你指定的parameterClass=Author,那麼在你的Author類中要有name,age,telephone和address屬性,並且要有相應的get和set方法
例2:
你可以使用基本類型作爲parameterClass,如:
<delete id="deleteAuthor" parameterClass="int">
       delete from author WHERE auth_id = #id#
</delete>
例3:
你可以使用HashMap作爲parameterClass,如:
<insert id="insertAuthor3" parameterClass="java.util.HashMap">
       INSERT INTO author (auth_name,auth_age,auth_tel,auth_address) VALUES (#name#,#age#,#telephone#,#address#)
</insert>
這時候,在你調用insertAuthor3的時候,你首先應該給傳入的Map對象賦值,調用代碼如下:
       HashMap paramMap = new HashMap();
       paramMap.put("name", "作者三");
       paramMap.put("age",new Integer(31));
       paramMap.put("address","南京");
       paramMap.put("telephone","025-987654321");
       sqlMapClient.insert("insertAuthor3", paramMap);

2. parameterMap

parameterMap 定義一系列有次序的參數用於匹配PreparedStatement 的JDBC值符號。
parameterMap屬性很少使用,parameterMap 屬性的值等於指定的parameterMap元素的name屬性值。通常(和缺省的)的方法是使用inline parameters。
 
注意!動態mapped statement 只支持inline parameter,不支持parameter map。關於動態mapped statement,將在後文中介紹。
 
例如:
<parameterMap id="authorParameter" class="Author">
       <parameter property="name" jdbcType="VARCHAR" javaType="java.lang.String" mode="INOUT"/>
       <parameter property="age" jdbcType="INTEGER" javaType="java.lang.Integer" mode="INOUT"/>
       <parameter property="telephone" jdbcType="VARCHAR" javaType="java.lang.String" mode="INOUT"/>
       <parameter property="address" jdbcType="VARCHAR" javaType="java.lang.String" mode="INOUT"/>
</parameterMap>
<insert id="insertAuthor1" parameterMap="authorParameter">
       INSERT INTO author (auth_name,auth_age,auth_tel,auth_address) VALUES (?,?,?,?)
</insert>
 
上面的例子中,parameterMap的參數按次序匹配SQL語句中的值符號(?)。因此,第一個"?"號將被"name"屬性的值替換,而第二個"?"號將被"age"屬性的值替換,依此類推。
 
記住:使用parameterMap的時候,SQL中的參數用"?"來代替,並且每個"?"的順序要與parameterMap中的定義完全匹配;如果使用parameterClass,那麼SQL中的參數用"#parameterName#"來代替,如果傳入的參數類爲Bean,那麼要有get和set這個參數名的方法。

3. resultClass
resultClass 屬性可以讓您指定一個Java 類,根據ResultSetMetaData 將其自動映射到JDBC ResultSet。只要是JavaBean 的屬性、方法名稱和ResultSet的列名匹配,屬性自動賦值列值。
 
例1:
<select id="getAuthor1" parameterClass="int" resultClass="Author">
       SELECT auth_id as id,auth_name as name,auth_age as age,auth_tel as telephone,auth_address as address FROM author WHERE auth_id = #id#
</select>
在上面的語句中,你指定的resultClass=Author,那麼在你的Author類中要有id,name,age,telephone和address屬性,並且要有相應的get和set方法。
如果你寫成:
<select id="getAuthor1" parameterClass="int" resultClass="Author">
       SELECT auth_id,auth_name,auth_age,auth_tel,auth_address FROM author WHERE auth_id = #id#
</select>
那麼在你的Author類中,要有auth_id,auth_name,auth_age,auth_tel,auth_address屬性,並且要有相應的get和set方法。
例2:
你還可以使用基本類型作爲resultClass,如:
<statement id="getAuthorNumber" resultClass="int">
       <![CDATA[SELECT count(auth_id) as totalAuthor FROM author]]>
</statement>
例3:
你還可以使用HashMap作爲resultClass,如:
<select id="getAuthor4" resultClass="java.util.HashMap">
       SELECT a.auth_id as authorid,a.auth_name as authname,a.auth_age as authage,a.auth_tel as authtel,a.auth_address as authaddress,b.art_title as arttitle FROM author a, article b WHERE a.auth_id=b.art_author
</select>
下面是調用代碼:
List authorList = (List)sqlMapClient.queryForList("getAuthor4",null);
showMethod("getAllAuthor");
for(int i=0;i<authorList.size();i++)
{
       HashMap authMap = (HashMap)authorList.get(i);
       System.out.println("auth_id="+authMap.get("authid")+"; auth_name="+authMap.get("authname")+"; auth_age="+authMap.get("authage")+"; auth_tel="+authMap.get("authtel")+"; auth_address="+authMap.get("authaddress")+";auth_article="+authMap.get("arttitle"));
}
 
但是,使用resultClass 的自動映射存在一些限制,無法指定輸出字段的數據類型,無法自動裝入相關的數據(複雜屬性),並且因爲需要ResultSetMetaData的信息,會對性能有輕微的不利影響。但使用resultMap,這些限制都可以很容易解決。

4. resultMap

使用resultMap 屬性可以控制數據如何從結果集中取出,以及哪一個屬性匹配哪一個字段。不象上面使用resultClass 屬性的自動映射方法,resultMap屬性可以允許指定字段的數據類型,NULL 的替代值。
例如:
<resultMap id="authorResult" class="Author">
       <result property="id" column="auth_id"/>   
       <result property="age" column="auth_age"/>
       <result property="name" column="auth_name"/>
       <result property="telephone" column="auth_tel"/>
       <result property="address" column="auth_address"/>
</resultMap>
 
<select id="getAuthor3" resultMap="authorResult">
       SELECT auth_id,auth_name,auth_age,auth_tel,auth_address FROM author WHERE auth_address like #%address%#
</select>
<statement id="getAllAuthor" resultMap="authorResult">
       SELECT * FROM author
</statement>
 
在上面的語句中,你指定的resultClass=Author,那麼在你的Author類中要有id,name,age,telephone和address屬性,並且要有相應的get和set方法。
 
通過resultMap 的定義,查詢語句得到的ResultSet 被映射成Author對象。resultMap定義"id"屬性值將賦予"auth_id"字段值,而"telephone"屬性值將賦予"auth_tel"字段值,依次類推。
 
注意:在resultMap中所指定的字段必須是下面的select中的子集。
也就是說,你不能寫成
<select id="getAuthor3" resultMap="authorResult">
       SELECT auth_address FROM author WHERE auth_address like #%address%#
</select>
但是你可以在resultMap中去掉<result property="id" column="auth_id"/>這行,仍然可以執行下面的語句:
<select id="getAuthor3" resultMap="authorResult">
       SELECT auth_id,auth_name,auth_age,auth_tel,auth_address FROM author WHERE auth_address like #%address%#
</select>
這樣的話,你就無法取得auth_id的值。 

5. cacheModel

定義查詢mapped statement 的緩存。每一個查詢mapped statement 可以使用不同或相同的cacheModel。
<cacheModel id="author-cache" imlementation="LRU">
       <flushInterval hours="24"/>
       <flushOnExecute statement="insertProduct"/>
       <flushOnExecute statement="updateProduct"/>
       <flushOnExecute statement="deleteProduct"/>
       <property name="size" value="1000" />
</cacheModel>
<select id="getAuthor3" resultMap="authorResult" cacheModel="author-cache">
       SELECT auth_id,auth_name,auth_age,auth_tel,auth_address FROM author WHERE auth_address like #%address%#
</select>
 
上面的配置說明:"getAuthor3"的緩存使用WEAK引用類型,當你通過調用"getAuthor3"的時候,Ibatis將會把結果緩存起來。每24 小時緩存刷新一次,或當更新的操作(即上面配置的insertProduct、updateProduct或deleteProduct)發生時刷新。
 
當你對某些表中的記錄操作頻繁時,可以考慮使用緩衝,但是如果數據量過大的話,最好另想辦法。 

6. xmlResultName

當映射結果指向一個XML文檔的時候,xmlResultName的值是指那個XML文檔的root標籤的名字。例如:
<select id="getAuthor1" parameterClass="int" resultClass="Author" xmlResultName="author">
       SELECT auth_id as id,auth_name as name,auth_age as age,auth_tel as telephone,auth_address as address FROM author WHERE auth_id = #id#
</select>
上面的select將產生如下的XML對象:
<author>
       <id>1</id>
       <name>作者三</name>
       <age>31</age>
       <telephone>025-987654321</telephone>
       <address>南京</address>
</author>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章