|
Ibatis2.0使用說明(二)——配置篇(3)
作者: 來源: 標(biāo)簽:ibatis orm sql (English) 【大 中 小】 statement中的參數(shù)簡介: 1. parameterClass parameterClass 屬性的值是Java類的全限定名(即包括類的包名)。parameterClass屬性是可選的,目的是限制輸入?yún)?shù)的類型為指定的Java 類。雖然Parameter-class屬性是可選的,建議你為每一個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> 在上面的語句中,你指定的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> 這時候,在你調(diào)用insertAuthor3的時候,你首先應(yīng)該給傳入的Map對象賦值,調(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值符號。 parameterMap屬性很少使用,parameterMap 屬性的值等于指定的parameterMap元素的name屬性值。通常(和缺省的)的方法是使用inline parameters。 注意!動態(tài)mapped statement 只支持inline parameter,不支持parameter map。關(guān)于動態(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語句中的值符號(?)。因此,第一個"?"號將被"name"屬性的值替換,而第二個"?"號將被"age"屬性的值替換,依此類推。 記?。菏褂胮arameterMap的時候,SQL中的參數(shù)用"?"來代替,并且每個"?"的順序要與parameterMap中的定義完全匹配;如果使用parameterClass,那么SQL中的參數(shù)用"#parameterName#"來代替,如果傳入的參數(shù)類為Bean,那么要有g(shù)et和set這個參數(shù)名的方法。 3. resultClass resultClass 屬性可以讓您指定一個Java 類,根據(jù)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屬性,并且要有相應(yīng)的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屬性,并且要有相應(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 的自動映射存在一些限制,無法指定輸出字段的數(shù)據(jù)類型,無法自動裝入相關(guān)的數(shù)據(jù)(復(fù)雜屬性),并且因為需要ResultSetMetaData的信息,會對性能有輕微的不利影響。但使用resultMap,這些限制都可以很容易解決。 4. resultMap 使用resultMap 屬性可以控制數(shù)據(jù)如何從結(jié)果集中取出,以及哪一個屬性匹配哪一個字段。不象上面使用resultClass 屬性的自動映射方法,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> 在上面的語句中,你指定的resultClass=Author,那么在你的Author類中要有id,name,age,telephone和address屬性,并且要有相應(yīng)的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"/>這行,仍然可以執(zhí)行下面的語句: <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引用類型,當(dāng)你通過調(diào)用"getAuthor3"的時候,Ibatis將會把結(jié)果緩存起來。每24 小時緩存刷新一次,或當(dāng)更新的操作(即上面配置的insertProduct、updateProduct或deleteProduct)發(fā)生時刷新。 當(dāng)你對某些表中的記錄操作頻繁時,可以考慮使用緩沖,但是如果數(shù)據(jù)量過大的話,最好另想辦法。 6. xmlResultName 當(dāng)映射結(jié)果指向一個XML文檔的時候,xmlResultName的值是指那個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對象: <author> <id>1</id> <name>作者三</name> <age>31</age> <telephone>025-987654321</telephone> <address>南京</address> </author> 本篇文章來源于 新技術(shù)天空 原文鏈接:http://www./tech/java/opensource/ibatis/2007-06-29/7acdecd3b19824d6.html |
|
|