ibatis select 标签

select的输入和输出

例如 select id,name,age from user where name like '%王%' and age>20

这里既有输入也有输出

select的属性
id,parameterClass,resultClass,parameterMap,resultMap,cacheModel
resultSetType,fetchSize,xmlResultName,remapResults,timeout
输入 parameterClass,ParameterMap
resultSetTYpe为结果集类型,主要是说游标,具有方向性,可以取
forward_only,scoll_insensitive,scoll_sensitive,具体情况具体配置

fetchSize 预取回数据量的大小

xmlResultName,返回result为xml的名称

如果返回的结果可以修改则吧remapResults设为true,否则设为false

输入,用参数带进来
可以是#,$做为占位符

不同占位符使用

Xml代码 复制代码
  1. <SPAN style="FONT-SIZE: small"><select id="getAllUsers1" resultClass="com.mypackage.User"  
  2.   parameterClass="string">  
  3.   SELECT * FROM USER WHERE DEPARTMENT=#department#   
  4.  </select>  
  5. <select id="getAllUsers1" resultClass="com.mypackage.User"  
  6.   parameterClass="string">  
  7.   SELECT * FROM USER WHERE DEPARTMENT like '$department$'   
  8.  </select>  
  9. </SPAN>  
<select id="getAllUsers1" resultClass="com.mypackage.User" parameterClass="string"> SELECT * FROM USER WHERE DEPARTMENT=#department# </select> <select id="getAllUsers1" resultClass="com.mypackage.User" parameterClass="string"> SELECT * FROM USER WHERE DEPARTMENT like '$department$' </select> 

 

#不能放在‘’之内


resultMap

 

Xml代码 复制代码
  1. <SPAN style="FONT-SIZE: small"><!-- 无映射  -->  
  2.  <select id="getAllUsers1" resultClass="User"  
  3.   parameterClass="string">  
  4.   SELECT * FROM USER WHERE DEPARTMENT=#department#   
  5.  </select>  
  6.   
  7.  <!-- 内联映射->实体类 -->  
  8.  <select id="getAllUsers2" resultClass="User"  
  9.   parameterClass="string">  
  10.   SELECT USERID as userid, USERNAME as username, PASSWORD as   
  11.   password, DEPARTMENT as department FROM USER WHERE   
  12.   DEPARTMENT=#department#   
  13.  </select>  
  14.   
  15.  <!-- 内联映射->MAP类 -->  
  16.  <select id="getAllUsers3" resultClass="hashmap"  
  17.   parameterClass="string">  
  18.   SELECT * FROM USER WHERE DEPARTMENT=#department#   
  19.  </select>  
  20.   
  21.  <!-- 显示映射->实体类 -->  
  22.  <resultMap id="accoutResult" class="User">  
  23.   <result property="userid" column="USERID" />  
  24.   <result property="username" column="USERNAME" />  
  25.   <result property="password" column="PASSWORD" />  
  26.   <result property="department" column="DEPARTMENT" />  
  27.  </resultMap>  
  28.  <select id="getAllUsers4" resultMap="accoutResult"  
  29.   parameterClass="string">  
  30.   SELECT * FROM USER WHERE DEPARTMENT=#department#   
  31.  </select>  
  32.   
  33.  <!-- 显示映射->MAP类 -->  
  34.  <resultMap id="mapResult" class="hashmap">  
  35.   <result property="userid" column="USERID" />  
  36.   <result property="username" column="USERNAME" />  
  37.   <result property="password" column="PASSWORD" />  
  38.   <result property="department" column="DEPARTMENT" />  
  39.  </resultMap>  
  40.  <select id="getAllUsers5" resultMap="mapResult"  
  41.   parameterClass="string">  
  42.   SELECT * FROM USER WHERE DEPARTMENT=#department#   
  43.  </select>  
  44.   
  45. </SPAN>  
<!-- 无映射 --> <select id="getAllUsers1" resultClass="User" parameterClass="string"> SELECT * FROM USER WHERE DEPARTMENT=#department# </select> <!-- 内联映射->实体类 --> <select id="getAllUsers2" resultClass="User" parameterClass="string"> SELECT USERID as userid, USERNAME as username, PASSWORD as password, DEPARTMENT as department FROM USER WHERE DEPARTMENT=#department# </select> <!-- 内联映射->MAP类 --> <select id="getAllUsers3" resultClass="hashmap" parameterClass="string"> SELECT * FROM USER WHERE DEPARTMENT=#department# </select> <!-- 显示映射->实体类 --> <resultMap id="accoutResult" class="User"> <result property="userid" column="USERID" /> <result property="username" column="USERNAME" /> <result property="password" column="PASSWORD" /> <result property="department" column="DEPARTMENT" /> </resultMap> <select id="getAllUsers4" resultMap="accoutResult" parameterClass="string"> SELECT * FROM USER WHERE DEPARTMENT=#department# </select> <!-- 显示映射->MAP类 --> <resultMap id="mapResult" class="hashmap"> <result property="userid" column="USERID" /> <result property="username" column="USERNAME" /> <result property="password" column="PASSWORD" /> <result property="department" column="DEPARTMENT" /> </resultMap> <select id="getAllUsers5" resultMap="mapResult" parameterClass="string"> SELECT * FROM USER WHERE DEPARTMENT=#department# </select> 

 

 map映射 ,返回的list里的每一项都是一个map然后有键值对

xml映射

Xml代码 复制代码
  1. <SPAN style="FONT-SIZE: small"><!-- XML -->  
  2.  <select id="selectXML" parameterClass="string" resultClass="xml"  
  3.   xmlResultName="log">  
  4.   SELECT * FROM USER_ACCOUNT WHERE GROUPNAME=#groupName#   
  5.  </select>  
  6. </SPAN>  
<!-- XML --> <select id="selectXML" parameterClass="string" resultClass="xml" xmlResultName="log"> SELECT * FROM USER_ACCOUNT WHERE GROUPNAME=#groupName# </select> 

  

resultClass="xml"这个xml也是预定义好的类

在测试类里调用后返回的是xml格式的字符串,指定的xmlResultName相当于xml的
根元素

 

返回结果有三类,1 返回直接指定的类 2 返回map类型 3 返回xml

你可能感兴趣的:(xml,ibatis)