zoukankan      html  css  js  c++  java
  • [转]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>

    本篇文章来源于 新技术天空 原文链接: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

  • 相关阅读:
    Spark使用总结与分享【转】
    用实例讲解Spark Sreaming--转
    hbase RowFilter如何根据rowkey查询以及实例实现代码 habase模糊查询【转】
    Android OpenGL ES(十三)通用的矩阵变换指令 .
    Android OpenGL ES(十二):三维坐标系及坐标变换初步 .
    Android OpenGL ES(十一)绘制一个20面体 .
    Android OpenGL ES(十)绘制三角形Triangle .
    Android OpenGL ES(九)绘制线段Line Segment .
    Android OpenGL ES(八)绘制点Point ..
    Android OpenGL ES .介绍
  • 原文地址:https://www.cnblogs.com/redcoatjk/p/3562450.html
Copyright © 2011-2022 走看看