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>
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值