mybatis 的一些常用功能

1. association 查询结果的一对一关联:

<resultMap id="blogResult" type="Blog">
  <id property=”blog_id” column="id" />
  <result property="title" column="blog_title"/>
  <association property="author" column="blog_author_id" javaType="Author">
    <id property="id" column="author_id"/>
    <result property="username" column="author_username"/>
    <result property="password" column="author_password"/>
    <result property="email" column="author_email"/>
    <result property="bio" column="author_bio"/>
  </association>
</resultMap>

2. collection查询结果的一对多关联:

<resultMap id="blogResult" type="Blog">
  <id property=”id” column="blog_id" />
  <result property="title" column="blog_title"/>
  <collection property="posts" ofType="Post">
    <id property="id" column="post_id"/>
    <result property="subject" column="post_subject"/>
    <result property="body" column="post_body"/>
  </collection>
</resultMap>

3. discriminator鉴别器(一):

    有时一个单独的数据库查询也许返回很多不同(但是希望有些关联)数据类型的结果集。
鉴别器元素就是被设计来处理这个情况的,还有包括类的继承层次结构。鉴别器非常容易理
解,因为它的表现很像 Java 语言中的 switch 语句。
    定义鉴别器指定了 column 和 javaType 属性。列是 MyBatis 查找比较值的地方。 JavaType
是需要被用来保证等价测试的合适类型(尽管字符串在很多情形下都会有用)。比如:

<resultMap id="vehicleResult" type="Vehicle">
  <id property=”id” column="id" />
  <result property="vin" column="vin"/>
  <result property="year" column="year"/>
  <result property="make" column="make"/>
  <result property="model" column="model"/>
  <result property="color" column="color"/>
  <discriminator javaType="int" column="vehicle_type">
    <case value="1" resultMap="carResult"/>
    <case value="2" resultMap="truckResult"/>
    <case value="3" resultMap="vanResult"/>
    <case value="4" resultMap="suvResult"/>
  </discriminator>
</resultMap>

<resultMap id="carResult" type="Car">
  <result property="doorCount" column="door_count" />
</resultMap>

     这个例子中,如果vehicle_type的值是1,则会使用carResult,即只加载doorCount属性,如果需要加载vehicleResult中的属性,则需要让carResult继承vehicleResult,使用如下:

<resultMap id="carResult" type="Car" extends=”vehicleResult”>
  <result property=”doorCount” column="door_count" />
</resultMap>

4. discriminator鉴别器(二):

    还有一种方式可以实现上面的效果:

<resultMap id="vehicleResult" type="Vehicle">
  <id property=”id” column="id" />
  <result property="vin" column="vin"/>
  <result property="year" column="year"/>
  <result property="make" column="make"/>
  <result property="model" column="model"/>
  <result property="color" column="color"/>
  <discriminator javaType="int" column="vehicle_type">
    <case value="1" resultType="carResult">
      <result property=”doorCount” column="door_count" />
    </case>
    <case value="2" resultType="truckResult">
      <result property=”boxSize” column="box_size" />
      <result property=”extendedCab” column="extended_cab" />
    </case>
    <case value="3" resultType="vanResult">
      <result property=”powerSlidingDoor” column="power_sliding_door" />
    </case>
    <case value="4" resultType="suvResult">
      <result property=”allWheelDrive” column="all_wheel_drive" />
    </case>
  </discriminator>
</resultMap>

5. 缓存:

    同hibernate一样,一级缓存(session级)是默认开启的,如果需要开启二级缓存,就需要加以下配置:

<!-- 在核心配置文件中加入配置 -->
<setting name="cacheEnabled" value="true"/>

<!-- 在mapper文件中加入配置 -->
<cache />

    <cache /> 语句的效果如下:

  •     映射语句文件中的所有 select 语句将会被缓存。
  •     映射语句文件中的所有 insert, update 和 delete 语句会刷新缓存。
  •     缓存会使用 Least Recently Used( LRU,最近最少使用的)算法来收回。
  •     根据时间表(比如 no Flush Interval,没有刷新间隔),缓存不会以任何时间顺序来刷新。
  •     缓存会存储列表集合或对象(无论查询方法返回什么)的 1024 个引用。
  •     缓存会被视为是 read/write(可读/可写)的缓存,意味着对象检索不是共享的,而且可以安全地被调用者修改,而不干扰其他调用者或线程所做的潜在修改。

    所有的这些属性都可以通过缓存元素的属性来修改。比如:

<cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true"/>

    这个更高级的配置创建了一个 FIFO 缓存,并每隔 60 秒刷新, 存数结果对象或列表的512 个引用,而且返回的对象被认为是只读的,因此在不同线程中的调用者之间修改它们会导致冲突。
    可用的收回策略有:
 LRU – 最近最少使用的:移除最长时间不被使用的对象。
 FIFO – 先进先出:按对象进入缓存的顺序来移除它们。
 SOFT – 软引用:移除基于垃圾回收器状态和软引用规则的对象。
 WEAK – 弱引用:更积极地移除基于垃圾收集器状态和弱引用规则的对象。
默认的是 LRU。
    flushInterval(刷新间隔)可以被设置为任意的正整数,而且它们代表一个合理的毫秒形式的时间段。默认情况是不设置,也就是没有刷新间隔,缓存仅仅调用语句时刷新。
    size(引用数目)可以被设置为任意正整数,要记住你缓存的对象数目和你运行环境的可用内存资源数目。默认值是 1024。
    readOnly(只读)属性可以被设置为 true 或 false。只读的缓存会给所有调用者返回缓存对象的相同实例。因此这些对象不能被修改。这提供了很重要的性能优势。可读写的缓存会返回缓存对象的拷贝(通过序列化)。这会慢一些,但是安全,因此默认是 false。

6. 动态sql:

  1)条件

      if 和 choose when otherwise

<select id=”findActiveBlogLike” parameterType=”Blog” resultType=”Blog”>
  SELECT * FROM BLOG WHERE state = „ACTIVE‟
  <choose>
    <when test=”title != null”>
      AND title like #{title}
    </when>
    <when test=”author != null and author.name != null”>
      AND title like #{author.name}
    </when>
    <otherwise>
      AND featured = 1
    </otherwise>
  </choose>
</select>

  2)关键字

      where 和 set

<!-- 取代where关键字,当所有if条件不成立时就不会出现where关键字 -->
<select id=”findActiveBlogLike” parameterType=”Blog” resultType=”Blog”>
  SELECT * FROM BLOG
  <where>
    <if test=”state != null”>
      state = #{state}
    </if>
    <if test=”title != null”>
      AND title like #{title}
    </if>
    <if test=”author != null and author.name != null”>
      AND title like #{author.name}
    </if>
  </where>
</select>


<update id="updateAuthorIfNecessary" parameterType="domain.blog.Author">
  update Author
  <set>
    <if test="username != null">username=#{username},</if>
    <if test="password != null">password=#{password},</if>
    <if test="email != null">email=#{email},</if>
    <if test="bio != null">bio=#{bio}</if>
  </set>
  where id=#{id}
</update>

  3)foreach

<select id="selectPostIn" resultType="domain.blog.Post">
  SELECT * FROM POST P WHERE ID in
  <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
    #{item}
  </foreach>
</select>

foreach详细用法: http://www.cnblogs.com/zzgno1/p/4184601.html

转载于:https://my.oschina.net/silenceyawen/blog/750448

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值