statement中的參數(shù)簡(jiǎn)介:
1. parameterClass
parameterClass 屬性的值是Java類的全限定名(即包括類的包名)。parameterClass屬性是可選的,目的是限制輸入?yún)?shù)的類型為指定的Java 類。雖然Parameter-class屬性是可選的,建議你為每一個(gè)SQL都指定parameterClass。如果不指定parameterClass 參數(shù),任何帶有合適屬性(get/set 方法)的Java Bean 都可以作為輸入?yún)?shù)。如果你使用了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>
在上面的語(yǔ)句中,你指定的parameterClass=Author,那么在你的Author類中要有name,age,telephone和address屬性,并且要有相應(yīng)的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>
這時(shí)候,在你調(diào)用insertAuthor3的時(shí)候,你首先應(yīng)該給傳入的Map對(duì)象賦值,調(diào)用代碼如下:
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 定義一系列有次序的參數(shù)用于匹配PreparedStatement 的JDBC值符號(hào)。
parameterMap屬性很少使用,parameterMap 屬性的值等于指定的parameterMap元素的name屬性值。通常(和缺省的)的方法是使用inline parameters。
注意!動(dòng)態(tài)mapped statement 只支持inline parameter,不支持parameter map。關(guān)于動(dòng)態(tài)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的參數(shù)按次序匹配SQL語(yǔ)句中的值符號(hào)(?)。因此,第一個(gè)"?"號(hào)將被"name"屬性的值替換,而第二個(gè)"?"號(hào)將被"age"屬性的值替換,依此類推。
記住:使用parameterMap的時(shí)候,SQL中的參數(shù)用"?"來(lái)代替,并且每個(gè)"?"的順序要與parameterMap中的定義完全匹配;如果使用parameterClass,那么SQL中的參數(shù)用"#parameterName#"來(lái)代替,如果傳入的參數(shù)類為Bean,那么要有g(shù)et和set這個(gè)參數(shù)名的方法。
3. resultClass
resultClass 屬性可以讓您指定一個(gè)Java 類,根據(jù)ResultSetMetaData 將其自動(dòng)映射到JDBC ResultSet。只要是JavaBean 的屬性、方法名稱和ResultSet的列名匹配,屬性自動(dòng)賦值列值。
例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>
在上面的語(yǔ)句中,你指定的resultClass=Author,那么在你的Author類中要有id,name,age,telephone和address屬性,并且要有相應(yīng)的get和set方法。
如果你寫(xiě)成:
<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屬性,并且要有相應(yīng)的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>
下面是調(diào)用代碼:
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 的自動(dòng)映射存在一些限制,無(wú)法指定輸出字段的數(shù)據(jù)類型,無(wú)法自動(dòng)裝入相關(guān)的數(shù)據(jù)(復(fù)雜屬性),并且因?yàn)樾枰猂esultSetMetaData的信息,會(huì)對(duì)性能有輕微的不利影響。但使用resultMap,這些限制都可以很容易解決。
4. resultMap
使用resultMap 屬性可以控制數(shù)據(jù)如何從結(jié)果集中取出,以及哪一個(gè)屬性匹配哪一個(gè)字段。不象上面使用resultClass 屬性的自動(dòng)映射方法,resultMap屬性可以允許指定字段的數(shù)據(jù)類型,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>
在上面的語(yǔ)句中,你指定的resultClass=Author,那么在你的Author類中要有id,name,age,telephone和address屬性,并且要有相應(yīng)的get和set方法。
通過(guò)resultMap 的定義,查詢語(yǔ)句得到的ResultSet 被映射成Author對(duì)象。resultMap定義"id"屬性值將賦予"auth_id"字段值,而"telephone"屬性值將賦予"auth_tel"字段值,依次類推。
注意:在resultMap中所指定的字段必須是下面的select中的子集。
也就是說(shuō),你不能寫(xiě)成
<select id="getAuthor3" resultMap="authorResult">
SELECT auth_address FROM author WHERE auth_address like #%address%#
</select>
但是你可以在resultMap中去掉<result property="id" column="auth_id"/>這行,仍然可以執(zhí)行下面的語(yǔ)句:
<select id="getAuthor3" resultMap="authorResult">
SELECT auth_id,auth_name,auth_age,auth_tel,auth_address FROM author WHERE auth_address like #%address%#
</select>
這樣的話,你就無(wú)法取得auth_id的值。
5. cacheModel
定義查詢mapped statement 的緩存。每一個(gè)查詢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>
上面的配置說(shuō)明:"getAuthor3"的緩存使用WEAK引用類型,當(dāng)你通過(guò)調(diào)用"getAuthor3"的時(shí)候,Ibatis將會(huì)把結(jié)果緩存起來(lái)。每24 小時(shí)緩存刷新一次,或當(dāng)更新的操作(即上面配置的insertProduct、updateProduct或deleteProduct)發(fā)生時(shí)刷新。
當(dāng)你對(duì)某些表中的記錄操作頻繁時(shí),可以考慮使用緩沖,但是如果數(shù)據(jù)量過(guò)大的話,最好另想辦法。
6. xmlResultName
當(dāng)映射結(jié)果指向一個(gè)XML文檔的時(shí)候,xmlResultName的值是指那個(gè)XML文檔的root標(biāo)簽的名字。例如:
<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將產(chǎn)生如下的XML對(duì)象:
<author>
<id>1</id>
<name>作者三</name>
<age>31</age>
<telephone>025-987654321</telephone>
<address>南京</address>
</author>
本篇文章來(lái)源于 新技術(shù)天空 原文鏈接:http://203.208.35.101/search?q=cache:VGS9aOGcGUkJ:www.ntsky.com/tech/java/opensource/ibatis/2007-06-29/7acdecd3b19824d6.html+parameterMap+%E7%94%A8%E6%B3%95&hl=zh-CN&ct=clnk&cd=5&gl=cn&lr=lang_zh-CN|lang_zh-TW&st_usg=ALhdy28HV28RUvZvcFSoHJtrbuRTGrXRLg