MyBatis之select

来源:互联网 发布:喀秋莎录屏软件录微课 编辑:程序博客网 时间:2024/05/22 06:59

查询语句是使用 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驱动器决定


示例:

  <resultMap id="detailResultMap" type="com.myapp.domain.Blog">  <id column="id" property="id" jdbcType="INTEGER"/>  <result column="author_id" property="authorId" jdbcType="INTEGER" />    <result column="title" property="title" jdbcType="VARCHAR" />    <collection property="posts" ofType="com.myapp.domain.Post">    <id column="postId" property="id"></id>    <result column="blog_id" property="blogId"></result>    <result column="subject" property="subject"/>    </collection>  </resultMap>  <select id="getBlogs" resultMap="detailResultMap" parameterType="com.myapp.domain.Author">  select b.title,b.id,b.author_id,c.id as postId,c.subject from Blog b  left join Post c on (b.id=c.blog_id and b.author_id=c.author_id)   where  b.author_id=#{id}  </select>


原创粉丝点击