MyBatis之select

来源:互联网 发布:for循环鸡兔同笼编程 编辑:程序博客网 时间:2024/05/22 09:05
查询语句是使用 MyBatis 时最常用的元素之一

select元素配置细节如下

属性描述取值默认id在这个模式下唯一的标识符,可被其它语句引用  parameterType传给此语句的参数的完整类名或别名  resultType语句返回值类型的整类名或别名。注意,如果是集合,那么这里填写的是集合的项的整类名或别名,而不是集合本身的类名。(resultType 与resultMap 不能并用)  resultMap引用的外部resultMap 名。结果集映射是MyBatis 中最强大的特性。许多复杂的映射都可以轻松解决。(resultType 与resultMap 不能并用)  flushCache如果设为true,则会在每次语句调用的时候就会清空缓存。select 语句默认设为falsetrue|falsefalseuseCache如果设为true,则语句的结果集将被缓存。select 语句默认设为false true|false false
timeout 设置驱动器在抛出异常前等待回应的最长时间,默认为不设值,由驱动器自己决定true|falsefalsetimeout设置驱动器在抛出异常前等待回应的最长时间,默认为不设值,由驱动器自己决定正整数未设置fetchSize设置一个值后,驱动器会在结果集数目达到此数值后,激发返回,默认为不设值,由驱动器自己决定正整数驱动器决定statementTypestatement,preparedstatement,callablestatement。
预准备语句、可调用语句STATEMENT
PREPARED
CALLABLEPREPAREDresultSetTypeforward_only,scroll_sensitive,scroll_insensitive
只转发,滚动敏感,不区分大小写的滚动FORWARD_ONLY
SCROLL_SENSITIVE
SCROLL_INSENSITIVE驱动器决定


示例:

[html] view plain copy
  1. <resultMap id="detailResultMap" type="com.myapp.domain.Blog">  
  2.     <id column="id" property="id" jdbcType="INTEGER"/>  
  3.     <result column="author_id" property="authorId" jdbcType="INTEGER" />  
  4.   <result column="title" property="title" jdbcType="VARCHAR" />  
  5.     <collection property="posts" ofType="com.myapp.domain.Post">  
  6.     <id column="postId" property="id"></id>  
  7.     <result column="blog_id" property="blogId"></result>  
  8.     <result column="subject" property="subject"/>  
  9.     </collection>  
  10. </resultMap>  
  11. <select id="getBlogs" resultMap="detailResultMap" parameterType="com.myapp.domain.Author">  
  12.         select b.title,b.id,b.author_id,c.id as postId,c.subject from Blog b  
  13.         left join Post c on (b.id=c.blog_id and b.author_id=c.author_id)   
  14.         where  b.author_id=#{id}  
  15. </select>  
0 0
原创粉丝点击