MaBatis之Mapper.xml的配置(多對一關聯關係)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="dao.BookMapper">
  <resultMap id="BaseResultMap" type="domain.Book">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="title" jdbcType="VARCHAR" property="title" />
    <result column="author" jdbcType="VARCHAR" property="author" />
    <result column="isbn" jdbcType="VARCHAR" property="isbn" />
    <result column="category_id" jdbcType="INTEGER" property="categoryId" />
  </resultMap>
  <sql id="Base_Column_List">
    id, title, author, isbn, category_id
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from book
    where id = #{id,jdbcType=INTEGER}
  </select>
  <resultMap id="bookResultMap" type="domain.Book" extends="BaseResultMap">
    <!-- 關聯屬性多對一一般用的是立即加載(分步查詢)
        每次查詢都要通過兩表主鍵和外鍵進行匹配
        也就說有多少個類別就查多少次,性能差,我希望能只查找一次全部都出來
        fetchType是否立即加載eager就是立即加載,lazy就是懶加載
        如果要使用懶加載需要再mybatis.config.xml中
     <setting>
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="aggressiveLazyLoading" value="true"/>
     </setting>
    -->
    <association property="category" javaType="Category" column="category_id"
                 select="dao.CategoryMapper.selectByPrimaryKey" fetchType="eager">
    </association>
  </resultMap>

  <select id="selectAll" resultMap="bookResultMap">
    select id, title, author, isbn, category_id from book
  </select>

  <resultMap id="bookResultMap2" type="domain.Book" extends="BaseResultMap">
    <!-- 關聯屬性多對一一般用的是立即加載
    這裏跟bookResultMap區別:這裏直接把category的Bean類屬性和數據庫的字段直接連接
    -->
    <association property="category" javaType="Category" column="category_id">
      <!-- 這有個坑爲了防止左外連接查詢的時候多表聯查出現mybatis分不清2個表的id所以給其中一個id加上
      一個別名-->
      <id property="id" column="catId"/>
      <result property="name" column="name"/>
    </association>
  </resultMap>
  <select id="selectAll2" resultMap="bookResultMap2">
    <!-- 一次性獲取關聯對象(也就是join fetch)-->
      select  b.id,
              b.title,
              b.author,
              b.isbn,
              b.category_id,
              c.id as catId,
              c.name
      from book b left join category c on b.category_id=c.id
  </select>
<!-- 使用resultMap映射實體類和字段之間的一一對應關係 -->
<resultMap id="bookResultMap3" type="domain.Book" extends="BaseResultMap">
    <association property="category" javaType="Category">
      <id property="id" column="c_id"/>
      <result property="name" column="name"/>
    </association>
</resultMap>
<!-- 兩表聯查  效率沒有左外連接快但是要比分步速度快-->
<select id="selectAll3" resultMap="bookResultMap3">
  select
        b.id,
        b.title,
        b.author,
        b.isbn,
        b.category_id,
        c.id as c_id,
        c.name
   from book b,category c where b.category_id = c.id
</select>

  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from book
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" keyColumn="id" keyProperty="id" parameterType="domain.Book" useGeneratedKeys="true">
    insert into book (title, author, isbn, 
      category_id)
    values (#{title,jdbcType=VARCHAR}, #{author,jdbcType=VARCHAR}, #{isbn,jdbcType=VARCHAR}, 
      #{categoryId,jdbcType=INTEGER})
  </insert>
  <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="domain.Book" useGeneratedKeys="true">
    insert into book
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="title != null">
        title,
      </if>
      <if test="author != null">
        author,
      </if>
      <if test="isbn != null">
        isbn,
      </if>
      <if test="categoryId != null">
        category_id,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="title != null">
        #{title,jdbcType=VARCHAR},
      </if>
      <if test="author != null">
        #{author,jdbcType=VARCHAR},
      </if>
      <if test="isbn != null">
        #{isbn,jdbcType=VARCHAR},
      </if>
      <if test="categoryId != null">
        #{categoryId,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="domain.Book">
    update book
    <set>
      <if test="title != null">
        title = #{title,jdbcType=VARCHAR},
      </if>
      <if test="author != null">
        author = #{author,jdbcType=VARCHAR},
      </if>
      <if test="isbn != null">
        isbn = #{isbn,jdbcType=VARCHAR},
      </if>
      <if test="categoryId != null">
        category_id = #{categoryId,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="domain.Book">
    update book
    set title = #{title,jdbcType=VARCHAR},
      author = #{author,jdbcType=VARCHAR},
      isbn = #{isbn,jdbcType=VARCHAR},
      category_id = #{categoryId,jdbcType=INTEGER}
    where id = #{id,jdbcType=INTEGER}
  </update>
</mapper>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章