国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
Ibatis2.0使用說(shuō)明(二)——配置篇(4)
Parameter Maps and Inline Parameters
 
<parameterMap id="parameterMapName" [class="Author"]>
       <parameter property ="propertyName" [jdbcType="VARCHAR"] [javaType="string"]
       [nullValue="NUMERIC"] [null="-9999999"]/>
       <parameter …… />
       <parameter …… />
</parameterMap>
括號(hào)[]中是可選的屬性。parameterMap 元素的id 屬性作為唯一標(biāo)識(shí),在同一個(gè)SQL Map XML 文件中不能重名。一個(gè)parameterMap 可包含任意多的property 元素。
一、propertyproperty屬性是指?jìng)魅雖apped statement中的JavaBean參數(shù)對(duì)象的屬性名。這個(gè)屬性名可以使用多次,這要看在這個(gè)statement中,這個(gè)屬性名要出現(xiàn)多少次。例如:
<parameterMap id="authorParameter3" class="Author">
       <parameter property="name" jdbcType="VARCHAR" javaType="java.lang.String" mode="INOUT"/>
       <parameter property="name" jdbcType="VARCHAR" javaType="java.lang.String" mode="INOUT"/>
</parameterMap>
<update id="updateAuthor2" parameterMap="authorParameter2">
       UPDATE author set auth_name=? WHERE auth_name = ?
</update>
但是如果使用這樣的方法的話,調(diào)用代碼應(yīng)該是:
Author author = new Author();
author.setName("作者三");
sqlMapClient.update("updateAuthor2", paraMap);
那么它其實(shí)執(zhí)行的是:
UPDATE author set auth_name='作者三' WHERE auth_name = '作者三'
這樣的話,就根本沒(méi)有了意義,因?yàn)?,你只能傳進(jìn)一個(gè)Author對(duì)象,而這個(gè)Author對(duì)象的name屬性值將會(huì)被用在整個(gè)Sql語(yǔ)句中,而一般的情況下是不應(yīng)該相同的,也就是說(shuō),我們的本意可能是想:
UPDATE author set auth_name='作者N' WHERE auth_name = '作者三'
方法倒是有,不過(guò)我覺(jué)得不太好。
<parameterMap id="authorParameter2" class="java.util.HashMap">
       <parameter property="name1" jdbcType="VARCHAR" javaType="java.lang.String" mode="INOUT"/>
       <parameter property="name2" jdbcType="VARCHAR" javaType="java.lang.String" mode="INOUT"/>
</parameterMap>
<update id="updateAuthor2" parameterMap="authorParameter2">
       UPDATE author set auth_name=? WHERE auth_name = ?
</update>
調(diào)用代碼為:
HashMap paraMap = new HashMap();
paraMap.put("name1", "作者N");
paraMap.put("name2", "作者三");
sqlMapClient.update("updateAuthor2", paraMap);
如果你想到更好的方法解決這個(gè)問(wèn)題的話,請(qǐng)不吝賜教。
二、 jdbcTypejdbcType用于指明數(shù)據(jù)庫(kù)的字段類型。如果不說(shuō)明字段類型的話,一些JDBC驅(qū)動(dòng)程序就無(wú)法確定要操作的字段類型。例如:PreparedStatement.setNull(int parameterIndex, int sqlType)方法,要求指定數(shù)據(jù)類型。如果不指定數(shù)據(jù)類型,某些Driver 可能指定為T(mén)ypes.Other 或Types.Null。但是,不能保證所有的Driver 都表現(xiàn)一致。對(duì)于這種情況,SQL Map API 允許使用parameterMap 元素的jdbcType 屬性指定數(shù)據(jù)類型。
正常情況下,只有當(dāng)字段可以為NULL或日期時(shí)間類型時(shí)才需要type 屬性。因?yàn)镴ava 只有一個(gè)Date 類型(java.util.Date),而大多數(shù)SQL 數(shù)據(jù)庫(kù)有多個(gè)-通常至少有3 種。因此,需要指定字段類型是DATE 還是DATETIME。
Type 屬性可以是JDBC Types 類中定義的任意參數(shù)的字符串值。雖然如此,還是有某些類型不支持(即BLOB)。
注意!大多數(shù)JDBC Driver 只有在字段可以為NULL 時(shí)需要指定type 屬性。因此,對(duì)于這些Driver,只是在字段可以為NULL 時(shí)才需要指定type 屬性。
注意!當(dāng)使用Oracle Driver 時(shí),如果沒(méi)有給可以為NULL 的字段指定type 屬性,當(dāng)試圖給這些字段賦值NULL 時(shí),會(huì)出現(xiàn)"Invalid column。 type"錯(cuò)誤。
三、javaType
javaType用于指明作為參數(shù)傳遞的java bean的屬性的類型。通常情況下,這可以通過(guò)反射機(jī)制從java bean中獲取類型,但是一些特定的映射,比如說(shuō)MAP和XML的映射就無(wú)法將類型信息傳遞給框架了。如果java type沒(méi)有設(shè)置而且框架無(wú)法獲知類型的話,那么這個(gè)類型會(huì)被指定為Object。
四、nullValue屬性 nullValue的值可以是對(duì)于property 類型來(lái)說(shuō)合法的任意值,用于指定NULL 的替換值。就是說(shuō),當(dāng)Java Bean的屬性值等于指定值時(shí),相應(yīng)的字段將賦值NULL。這個(gè)特性允許在應(yīng)用中給不支持null的數(shù)據(jù)類型(即int,double,float等)賦值null。當(dāng)這些數(shù)據(jù)類型的屬性值匹配nullValue值(即匹配-9999)時(shí),NULL 將代替nullValue 值寫(xiě)入數(shù)據(jù)庫(kù)。
例如:
<parameterMap id="authorParameter" class="Author">
       <parameter property="name" jdbcType="VARCHAR" javaType="java.lang.String" nullValue="NO_ENTRY" mode="INOUT"/>
       <parameter property="age" jdbcType="INTEGER" javaType="java.lang.Integer" nullValue="-999" mode="INOUT"/>
       <parameter property="telephone" jdbcType="VARCHAR" javaType="java.lang.String" nullValue="NO_ENTRY" mode="INOUT"/>
       <parameter property="address" jdbcType="VARCHAR" javaType="java.lang.String" nullValue="NO_ENTRY" mode="INOUT"/>
</parameterMap>
<insert id="insertAuthor1" parameterMap="authorParameter">
       INSERT INTO author (auth_name,auth_age,auth_tel,auth_address) VALUES (?,?,?,?)
</insert>
您可以在另一個(gè)SQL Map XML 文件中引用parameterMap。例如,要在另一個(gè)文件中引用上面的parameterMap,可以使用名稱"Product.insert-product-param"。
五、Inline Parameter Maps使用Inline Parameter Maps,可以把Java Bean 的屬性名稱嵌在mapped-statement 的定義中(即直接寫(xiě)在SQL 語(yǔ)句中)。
例如:
<insert id="insertAuthor1" parameterClass="Author">
       INSERT INTO author (auth_name,auth_age,auth_tel,auth_address) VALUES (#name#,#age#,#telephone#,#address#)
</insert>
這樣,在你的Author類中,要有name,age,telephone,address的屬性以及相應(yīng)的get和set方法,這樣做可以避免使用另外定義parameterMap的麻煩。
你也可以在內(nèi)嵌參數(shù)中指定數(shù)據(jù)類型和nullValue,例如:
<insert id="insertAuthor1" parameterClass="Author">
       INSERT INTO author (auth_name,auth_age,auth_tel,auth_address) VALUES (#name:VARCHAR:NO_ENTRY#,#age:INTEGER:-999#,#telephone:VARCHAR:NO_ENTRY#,#address:VARCHAR:NO_ENTRY#)
</insert>
注意!在內(nèi)嵌參數(shù)中,要指定NULL 的替代值,必須要先指定數(shù)據(jù)類型。
注意!如需要在查詢時(shí)也使用NULL 替代值,必須同時(shí)在resultMap 中定義。
注意!如果您需要指定很多的數(shù)據(jù)類型和NULL 替代值,可以使用外部的parameterMap元素,這樣會(huì)使代碼更清晰。
六、Result Maps在SQL Map 架構(gòu)中,Result Map 是極其重要的組件。在執(zhí)行查詢Mapped Statement 時(shí),resultMap 負(fù)責(zé)將結(jié)果集的列值映射成Java Bean 的屬性值。resultMap 的結(jié)構(gòu)如下:
<resultMap id="resultMapName" class="some.domain.Class" [extends="parent-resultMap"]>
       <result property="propertyName" column="COLUMN_NAME"
                     [columnIndex="1"] [javaType="int"] [jdbcType="NUMERIC"]
                     [nullValue="-999999"] [select="someOtherStatement"]
                     />
       <result ……/>
       <result ……/>
       <result ……/>
</resultMap>
括號(hào)[]中是可選的屬性resultMap 也有class 屬性,是Java 類的全限定名(即包括包的名稱)或該類的別名。該Java 類初始化并根據(jù)定義填充數(shù)據(jù)。
Extends 是可選的屬性,可以設(shè)定成以為基礎(chǔ)的另外一個(gè)resultMap 的名字。和在Java 中繼承一個(gè)類相似,父resultMap 的屬性將作為子resultMap 的一部分。父resultMap 的屬性總是加到子resultMap 屬性的前面,并且父resultMap 必須要在子resultMap 之前定義。父resultMap 和子resultMap 的class 屬性不一定要一致,它們可以沒(méi)有任何關(guān)系。
resultMap 可以包括任意多的property 映射,將查詢結(jié)果集的列值映射成Java Bean 的屬性。屬性的映射按它們?cè)趓esultMap中定義的順序進(jìn)行。屬性class 必須符合Java Bean 規(guī)范,每一屬性都必須擁有g(shù)et/set 方法。
注意!ResultSet 的列值按它們?cè)趓esultMap 中定義的順序讀取。
(一) property
property屬性是指從mapped statement中返回的JavaBean對(duì)象的屬性名。這個(gè)屬性名也可以使用多次。
(二) column
column屬性值是ResultSet中的列名字,即字段名,取得的這個(gè)字段的值將賦給property所指的bean屬性。
(三) columnIndex
可選屬性,用于改善性能。屬性columnIndex 的值是ResultSet 中用于賦值Java Bean屬性的字段次序號(hào)。在99%的應(yīng)用中,不太可能需要犧牲可讀性來(lái)?yè)Q取性能。使用columnIndex,某些JDBC Driver可以大幅提高性能,某些則沒(méi)有任何效果。
(四) jdbcType
同ParameterMap中的jdbcType
(五) javaType
同ParameterMap中的javaType
(六) nullValue
屬性nullValue指定數(shù)據(jù)庫(kù)中NULL的替代值。因此,如果從ResultSet中讀出NULL值,JavaBean屬性將被賦值為屬性nullValue指定的替代值。
如果數(shù)據(jù)庫(kù)中存在NULLABLE 屬性的字段,但您想在你的應(yīng)用程序中用指定的常量代替NULL,您可以這樣做:
<resultMap id="get-product-result" class="Author">
       <result property="id" column="auth_id"/>  
       <result property="age" column="auth_age"/>
       <result property="name" column="auth_name" nullValue="you have no name"/>
</resultMap>
在上例中,如果取得的記錄中auth_name字段的值為NULL,那么在賦給java bean的時(shí)候,name屬性將被賦為"you have no name"。
七、select 復(fù)雜屬性
如果在一個(gè)類與另一個(gè)類之間是關(guān)聯(lián)關(guān)系的話,那么當(dāng)你用JDBC取得記錄的時(shí)候,這個(gè)關(guān)聯(lián)關(guān)系是如何實(shí)現(xiàn)的呢?例如這樣的關(guān)系:一個(gè)作者可能會(huì)有多個(gè)文章發(fā)表,那么作者與文章之間就是很強(qiáng)的關(guān)聯(lián)關(guān)系,而且是一對(duì)多的關(guān)系,在Author的Bean中是這樣寫(xiě)的:
public class Author
{
    private int id;
.....
    private List articleList;
       public int getId()
    {
        return id;
    }
    public void setId(int id)
    {
        this.id=id;
    }
 
       ... ...
   
       public List getArticleList()
    {
        return articleList;
    }
   
    public void setArticleList(List articleList)
    {
        this.articleList=articleList;
    }
}
當(dāng)你執(zhí)行一條sql語(yǔ)句從數(shù)據(jù)表author中取出相應(yīng)的數(shù)據(jù)的時(shí)候,在上面的java bean中,articleList如何賦值呢?這時(shí)候,就需要使用select屬性。
1.1:1關(guān)系:
我們先假設(shè)在author和article之間使用1:1的關(guān)系,雖然在真實(shí)世界中是不正確的,我們只是做個(gè)例子,那么Author和article的bean代碼如下:
public class Author
{
    private int id;
    private int age;
    private String name;  
    private String address;
    private String telephone;
    private Article article;
      
    public int getId()
    {
        return id;
    }
    public void setId(int id)
    {
        this.id=id;
    }
    public int getAge()
    {
        return age;
    }
    public void setAge(int age)
    {
        this.age=age;
    }
    public String getName()
    {
        return name;
    }
    public void setName(String name)
    {
        this.name=name;
    }
   
    public String getAddress()
    {
        return address;
    }
    public void setAddress(String address)
    {
        this.address=address;
    }
    public String getTelephone()
    {
        return telephone;
    }
    public void setTelephone(String telephone)
    {
        this.telephone=telephone;
    }
 
       public Article getArticle()
    {
        return this.article;
    }
          
    public void setArticle(Article article)
    {
        this.article=article;
    }
}
 
public class Article
{
    private int id;
    private String title;
    private Date createtime;
    private int author;
   
    public int getId()
    {
        return id;
    }
    public void setId(int id)
    {
        this.id=id;
    }
    public String getTitle()
    {
        return title;
    }
    public void setTitle(String title)
    {
        this.title=title;
    }
    public Date getCreatetime()
    {
        return createtime;
    }
    public void setCreatetime(Date createtime)
    {
        this.createtime=createtime;
    }
    public int getAuthor()
    {
        return author;
    }
    public void setAuthor(int author)
    {
        this.author=author;
    }
}
在author.xml的配置如下:
<resultMap id="linkResultMap1" 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"/>
       <result property="article" column="auth_id" select="getLinkArticle1"/>
</resultMap>
<select id="getAuthor5" resultMap="linkResultMap1" parameterClass="int">
       SELECT * FROM author WHERE auth_id = #id#
</select>
<select id="getLinkArticle1" resultClass="com.ibatis.beans.Article" parameterClass="int">
       SELECT art_id as id,art_title as title,art_createtime as createtime,art_author as author FROM article WHERE art_id = #id#
</select>
調(diào)用代碼如下:
Author author = (Author)sqlMapClient.queryForObject("getAuthor5", new Integer(1));
System.out.println(author.getName()+"'s article is :"+author.getArticle().getTitle());
你可以看到,對(duì)于Author類中的article屬性,IBatis是將取得的記錄的auth_id字段值作為參數(shù)傳入到id="getLinkArticle1"的語(yǔ)句中,并將取得的結(jié)果封裝成Article對(duì)象,并賦給Author的article屬性。上面的調(diào)用代碼實(shí)際執(zhí)行的兩條sql語(yǔ)句是:
SELECT * FROM author WHERE auth_id = 1
SELECT art_id as id,art_title as title,art_createtime as createtime,art_author as author FROM article WHERE art_id = 1
在第二條語(yǔ)句中,將取得的記錄封裝成Article對(duì)象,并賦給Author的article屬性,所以,你可以直接使用author.getArticle().getTitle()獲得文章的標(biāo)題。
上面的方法顯示了如何實(shí)現(xiàn)1:1的關(guān)聯(lián)關(guān)系,但是上面的方法并不好,原因是可能會(huì)執(zhí)行很多次查詢!
(1)避免 N+1 Selects (1:1)
如果上面的配置如下:
<resultMap id="linkResultMap1" 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"/>
       <result property="article" column="auth_id" select="getLinkArticle1"/>
</resultMap>
<select id="getAuthor5" resultMap="linkResultMap1" parameterClass="int">
       SELECT * FROM author WHERE auth_id > #id#
</select>
<select id="getLinkArticle1" resultClass="com.ibatis.beans.Article" parameterClass="int">
       SELECT art_id as id,art_title as title,art_createtime as createtime,art_author as author FROM article WHERE art_id = #id#
</select>
調(diào)用代碼如下:
Author author = (Author)sqlMapClient.queryForList("getAuthor5", new Integer(1));
如果SELECT * FROM author WHERE auth_id > 1的記錄有N條,那么將對(duì)id="getLinkArticle1"的語(yǔ)句執(zhí)行N次查詢,這樣所有的查詢總數(shù)將為N+1次,執(zhí)行效率會(huì)很低。
這時(shí),可以使用下面的聯(lián)合查詢的方法來(lái)解決:
<resultMap id="linkResultMap2" 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"/>
       <result property="article.title" column="art_title"/>
</resultMap>
<select id="getAuthor6" resultMap="linkResultMap2" parameterClass="int">
       <![CDATA[ SELECT a.auth_id,a.auth_age,a.auth_name,a.auth_tel,a.auth_address,b.art_title FROM author a,article b WHERE a.auth_id > #id# and a.auth_id = b.art_id]]>
</select>
調(diào)用代碼為:
Author author = (Author)sqlMapClient.queryForList("getAuthor6", new Integer(1));
這樣只用一條Sql語(yǔ)句就可以解決。
2.1:M與M:N關(guān)系:
下面我們討論1:M的關(guān)系,一個(gè)author可能有多個(gè)article,所以,author與article之間是一對(duì)多的關(guān)系。那么我們?cè)贏uthor類中加入如下代碼(在省略號(hào)間的是要加入的代碼):
public class Author
{
... ...
       private List articleList;
    public List getArticleList()
    {
        return articleList;
    }
   
    public void setArticleList(List articleList)
    {
        this.articleList=articleList;
    }
... ...
}
配置如下:
<resultMap id="linkResultMap3" 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"/>
       <result property="articleList" column="auth_id" select="getLinkArticle3"/>
</resultMap>
<select id="getAuthor7" resultMap="linkResultMap3" parameterClass="int">
       SELECT * FROM author WHERE auth_id = #id#
</select>
<select id="getLinkArticle3" resultClass="com.ibatis.beans.Article" parameterClass="int">
       SELECT art_id as id,art_title as title,art_createtime as createtime,art_author as author FROM article WHERE art_author = #id#
</select>
調(diào)用代碼為:
Author author = (Author)sqlMapClient.queryForObject("getAuthor7", new Integer(1));
System.out.println(author.getName()+"的文章有:");
for(int i=0;i<author.getArticleList().size();i++)
{
       int num=i+1;
       Article art = (Article)author.getArticleList().get(i);
       System.out.println(num+". "+art.getTitle());
}
從上面的實(shí)現(xiàn)可以看出,你只需要在bean中加入一個(gè)java.util.List(或java.util.Collection)類型的articleList來(lái)表示所有的文章列表即可,調(diào)用部分沒(méi)有什么變化,IBaits會(huì)自動(dòng)從Article中取得的記錄封裝成Article對(duì)象并加入到一個(gè)List對(duì)象中,然后將這個(gè)List對(duì)象賦值給Author類的articleList屬性。
(1)避免 N+1 Selects (1:M and M:N)
1:M和M:N的情況與1:1的情況相似,也會(huì)出現(xiàn)N+1 Selects 的情況,但是到目前位置,還沒(méi)有其他的方法來(lái)解決這個(gè)問(wèn)題,希望在不久的將來(lái)能夠解決。
3.多個(gè)復(fù)雜參數(shù)屬性
你可能已經(jīng)注意到了,上面的例子中,在resultMap中只指明了一個(gè)column屬性用于id=”getLinkArticle”的statement關(guān)聯(lián)。其實(shí),Ibatis允許你指明多個(gè)column屬性與id=”getLinkArticle”的statement關(guān)聯(lián),語(yǔ)法很簡(jiǎn)單, {param1=column1, param2=column2, …, paramN=columnN}。下面是一個(gè)例子:
<resultMap id="linkResultMap4" 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"/>
    <result property="articleList" column="{id=auth_id,address=auth_address}" select="getLinkArticle4"/>
</resultMap>
<select id="getAuthor8" resultMap="linkResultMap4" parameterClass="int">
    SELECT * FROM author WHERE auth_id = #id#
</select>
<select id="getLinkArticle4" resultClass="com.ibatis.beans.Article" parameterClass="int">
    SELECT art_id as id,art_title as title,art_createtime as createtime,art_author as author FROM article WHERE art_author = #id# and art_publish_add=#address#
</select>
你也可以只寫(xiě)字段的名稱,只要按照所關(guān)聯(lián)的statement中所對(duì)應(yīng)的字段順序即可,象這樣:
{auth_id,auth_address}
上面的這個(gè)例子我在Mysql的環(huán)境中運(yùn)行沒(méi)有通過(guò),報(bào)出的錯(cuò)誤是:Column'{auth_id,auth_address}' not found.
這個(gè)錯(cuò)誤是與JDBC驅(qū)動(dòng)無(wú)關(guān)的,而是在做XML解析的時(shí)候發(fā)生的錯(cuò)誤,不知道是什么原因,如果您有這樣的成功經(jīng)歷的話,希望能共同分享,我的Email是:tenwang1977@163.com
注意:有些JDBC驅(qū)動(dòng)不支持同時(shí)打開(kāi)多個(gè)ResultSets(單個(gè)連接)。所以說(shuō),這樣的驅(qū)動(dòng)不能完成復(fù)雜對(duì)象的映射,因?yàn)镴DBC驅(qū)動(dòng)需要多個(gè)ResultSets的連接,這時(shí)候,只能使用一個(gè)關(guān)聯(lián)查詢解決問(wèn)題。
如果你使用Microsoft SQL Server 2000 的JDBC驅(qū)動(dòng)的話,你需要在配置url的時(shí)候,在url后加上SelectMethod=Cursor。
4.在Parameter Maps and Result Maps中支持的參數(shù)
Java Type JavaBean/Map
Property Mapping Result Class /
Parameter Class*** Type Alias**
boolean YES NO boolean
java.lang.Boolean YES YES boolean
byte YES NO byte
java.lang.Byte YES YES byte
short YES NO short
java.lang.Short YES YES short
int YES NO Int/ Integer
java.lang.Integer YES YES Int/ Integer
long YES NO long
java.lang.Long YES YES long
float YES NO float
java.lang.Float YES YES float
double YES NO double
java.lang.Double YES YES double
java.lang.String YES YES string
java.util.Date YES YES date
java.math.BigDecimal YES YES decimal
* java.sql.Date YES YES N/A
* java.sql.Time YES YES N/A
* java.sql.Timestamp YES YES N/A
七、緩存Mapped Statement Result<cacheModel id="product-cache" type ="LRU" readOnly=”true” serialize=”false”>
<flushInterval hours="24"/>
<flushOnExecute statement="insertProduct"/>
<flushOnExecute statement="updateProduct"/>
<flushOnExecute statement="deleteProduct"/>
<property name=”cache-size” value=”1000” />
</cacheModel>
上面的cache model 創(chuàng)建了一個(gè)名為“product-cache”的緩存,使用“最近最少使用”(LRU)實(shí)現(xiàn),每24小時(shí),緩沖區(qū)將刷新一次,而且在執(zhí)行insertProduct、updateProduct和deleteProduct的statement時(shí),緩沖區(qū)也將刷新,設(shè)定的時(shí)間可以設(shè)定為hours, minutes, seconds或 milliseconds。一些Cache的實(shí)現(xiàn)需要附加的屬性,比如說(shuō)上例中的cache-size
 屬性,cache的大小指明了可以存放在cache中的實(shí)體的個(gè)數(shù)。type屬性的名稱要么是全限定的類名,要么是緩存實(shí)現(xiàn)的別名。Cache Model 使用插件的形式來(lái)支持不同的緩存算法。它的實(shí)現(xiàn)在cache-model 元素的type屬性中指定(如上所示)。
(一)Read-Only 與 Read/Write
Ibatis支持只讀和可讀寫(xiě)的Cache,只讀的Cache可以在所有的用戶間共享,所以它可以提供更大的操作空間。但是從只讀緩沖中讀取的對(duì)象不能夠被修改。如果你要對(duì)你取得的對(duì)象進(jìn)行修改的話,那么你只能用可讀寫(xiě)的緩沖。readOnly=”true”為只讀緩沖;readOnly=”false”為可讀寫(xiě)緩沖。
(二)Serializable Read/Write Caches
要使用Serializable Read/Write Caches,設(shè)置readOnly=”false”,serialize=”true”。默認(rèn)情況下,采用的是readOnly=” true”,serialize=”false”。
(三)緩沖類型
目前包括以下的4 個(gè)緩沖類型實(shí)現(xiàn):
1. “MEMORY” (com.ibatis.db.sqlmap.cache.memory.MemoryCacheController)
MEMORY cache 實(shí)現(xiàn)使用reference 類型來(lái)管理cache 的行為。垃圾收集器可以根據(jù)reference 類型判斷是否要回收cache 中的數(shù)據(jù)。MEMORY 實(shí)現(xiàn)適用于沒(méi)有統(tǒng)一的對(duì)象重用模式的應(yīng)用,或內(nèi)存不足的應(yīng)用。
MEMORY 實(shí)現(xiàn)可以這樣配置:
<cache-model name="product-cache" implementation ="MEMORY">
       <flush-interval hours="24"/>
       <flush-on-execute statement="insertProduct"/>
       <flush-on-execute statement="updateProduct"/>
       <flush-on-execute statement="deleteProduct"/>
       <cache-property name=”reference-type” value=”WEAK” />
</cache-model>
MEMORY cache 實(shí)現(xiàn)只認(rèn)識(shí)一個(gè)<cache-property>元素。這個(gè)名為“reference-type”屬性的值必須是STRONG,SOFT 和WEAK 三者其一。這三個(gè)值分別對(duì)應(yīng)于JVM 不同的內(nèi)存reference 類型。
(1) WEAK(缺?。?
大多數(shù)情況下,WEAK類型是最佳選擇。如果不指定類型,缺省類型就是WEAK。它能大大提高常用查詢的性能。但是對(duì)于當(dāng)前不被使用的查詢結(jié)果數(shù)據(jù),將被清除以釋放內(nèi)存用來(lái)分配其他對(duì)象。
(2) SOFT
在查詢結(jié)果對(duì)象數(shù)據(jù)不被使用,同時(shí)需要內(nèi)存分配其他對(duì)象的情況下,SOFT類型將減少內(nèi)存不足的可能性。然而,這不是最具侵入性的reference類型,結(jié)果數(shù)據(jù)依然可能被清除。
(3) STRONG
確保查詢結(jié)果數(shù)據(jù)一直保留在內(nèi)存中,除非Cache被刷新(例如,到了刷新的時(shí)間或執(zhí)行了更新數(shù)據(jù)的操作)。
對(duì)于下面的情況,這是理想的選擇:
1” 結(jié)果內(nèi)容數(shù)據(jù)很少
2” 完全靜態(tài)的數(shù)據(jù)
3” 頻繁使用的數(shù)據(jù)
優(yōu)點(diǎn)是對(duì)于這類查詢性能非常好。缺點(diǎn)是,如果需要分配其他對(duì)象,內(nèi)存無(wú)法釋放(可能是更重要的數(shù)據(jù)對(duì)象)。
2. “LRU” (com.ibatis.db.sqlmap.cache.lru.LruCacheController)
LRU Cache 實(shí)現(xiàn)用“最近最少使用”原則來(lái)確定如何從Cache 中清除對(duì)象。當(dāng)Cache溢出時(shí),最近最少使用的對(duì)象將被從Cache 中清除。
<cache-model name="product-cache" implementation ="LRU">
       <flush-interval hours="24"/>
       <flush-on-execute statement="insertProduct"/>
       <flush-on-execute statement="updateProduct"/>
       <flush-on-execute statement="deleteProduct"/>
       <cache-property name=”cache-size” value=”1000” />
</cache-model>
值得注意的是,這里指的對(duì)象可以是任意的,從單一的String 對(duì)象到Java Bean 的ArrayList 對(duì)象都可以。因此,不要Cache 太多的對(duì)象,以免內(nèi)存不足。
3. “FIFO” (com.ibatis.db.sqlmap.cache.fifo.FifoCacheController)
FIFO Cache 實(shí)現(xiàn)用“先進(jìn)先出”原則來(lái)確定如何從Cache 中清除對(duì)象。對(duì)于短時(shí)間內(nèi)持續(xù)引用特定的查詢而后很可能不再使用的情況,F(xiàn)IFO Cache 是很好的選擇。
<cache-model name="product-cache" implementation ="FIFO">
       <flush-interval hours="24"/>
       <flush-on-execute statement="insertProduct"/>
       <flush-on-execute statement="updateProduct"/>
       <flush-on-execute statement="deleteProduct"/>
       <cache-property name=”cache-size” value=”1000” />
</cache-model>
值得注意的是,這里指的對(duì)象可以是任意的,從單一的String 對(duì)象到Java Bean 的ArrayList 對(duì)象都可以。因此,不要Cache 太多的對(duì)象,以免內(nèi)存不足。
4. “OSCACHE” (com.ibatis.db.sqlmap.cache.oscache.OSCacheController)
OSCACHE Cache 實(shí)現(xiàn)是OSCache2.0 緩存引擎的一個(gè)Plugin。它具有高度的可配置性,分布式,高度的靈活性。
<cache-model name="product-cache" implementation ="OSCACHE">
       <flush-interval hours="24"/>
       <flush-on-execute statement="insertProduct"/>
       <flush-on-execute statement="updateProduct"/>
       <flush-on-execute statement="deleteProduct"/>
</cache-model>
OSCACHE 實(shí)現(xiàn)不使用cache-property 元素。而是在類路徑的根路徑中使用標(biāo)準(zhǔn)的oscache.properties 文件進(jìn)行配置。在oscache.properties 文件中,您可以配置Cache 的算法(和上面討論的算法很類似),Cache 的大小,持久化方法(內(nèi)存,文件等)和集群方法。
要獲得更詳細(xì)的信息,請(qǐng)參考OSCache 文檔。OSCache 及其文檔可以從OpenSymphony網(wǎng)站上獲?。?a >http://www.opensymphony.com/oscache/
本篇文章來(lái)源于 新技術(shù)天空 原文鏈接:http://www.ntsky.com/tech/java/opensource/ibatis/2007-06-29/876df513e86bdbf0.html
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Mybatis XML 文件的學(xué)習(xí)詳解
Mybatis框架Result Maps解析
Mybatis的ResultMap的使用(轉(zhuǎn))
深入淺出MyBatis
MyBatis筆記
ibatis如何支持clob 和blob
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服