MyBatsi-Mapper映射文件

Mapper映射文件

  • cache – 给定命名空间的缓存配置。
  • cache-ref – 其他命名空间缓存配置的引用。
  • resultMap – 是最复杂也是最强大的元素,用来描述如何从数据库结果集中来加载对象。
  • parameterMap – 已废弃!老式风格的参数映射。内联参数是首选,这个元素可能在将来被移除,这里不会记录。
  • sql – 可被其他语句引用的可重用语句块。
  • insert – 映射插入语句
  • update – 映射更新语句
  • delete – 映射删除语句
  • select – 映射查询语句

mapper文件与mapper 接口的配置说明

<mapper namespace="vallue " ></mapper>

  • 针对Configuration中不同mapper映射器,这里的名称空间定义不同:

当使用相对于类路径的资源引用和完全限定资源定位符(URL)映射器时,namespace=”valeu” value的值可以任意指定,并且mapper映射文件的位置可以任意指定.

<mapper namespace="nnn" ></mapper>

  • 当使用使用映射器接口实现类的完全限定类名和包内的映射器接口实现全部注册为映射器时, namespace=”valeu” value的值必须指定为mapper接口类型的权限名称且mapper映射文件必须和mapper接口在一个包中

<mapper namespace="com.lifeibai.dao.UserDao" ></mapper>

 

元素说明

<mapper namespace="vallue " > 这里是mapper的元素</mapper>

第一部分insert update delete 

  • insert – 映射插入语句
  • update – 映射更新语句
  • delete – 映射删除语句

 

主键生成策略

 

第二部分select 

 

第三部分sql 与字符串拼接

 

Sql

<!-- 自定义条件查询用户列表 -->

<sql id="sometable">

  ${prefix}Table

</sql>

 

<sql id="someinclude">

  from

    <include refid="${include_target}"/>

</sql>

 

<select id="select" resultType="map">

  select

    field1, field2, field3

  <include refid="someinclude">

    <property name="prefix" value="Some"/>

    <property name="include_target" value="sometable"/>

  </include>

</select>

字符串拼接替换#{}和${}

<!-- 自定义条件查询用户列表 -->

    <select id="findUserByUsername" parameterType="java.lang.String"

           resultType="cn.itcast.mybatis.po.User">

       select * from user where username like '%${value}%'

    </select>

 

 

#{}和${}

#{}表示一个占位符号,通过#{}可以实现preparedStatement向占位符中设置值,自动进行java类型和jdbc类型转换,#{}可以有效防止sql注入。 #{}可以接收简单类型值或pojo属性值。 如果parameterType传输单个简单类型值,#{}括号中可以是value或其它名称。

${}表示拼接sql串,通过${}可以将parameterType 传入的内容拼接在sql中且不进行jdbc类型转换, ${}可以接收简单类型值或pojo属性值,如果parameterType传输单个简单类型值,${}括号中只能是value。

第四部分resultMap 

 

resultMap的解决问题:

1)      po类字段与数据表中的列名不一致

2)      一对一关系

3)      一对多关系

4)      多对多

5)      根据条件封装不同结果

ResultMap的子元素

  • constructor - 用于在实例化类时,注入结果到构造方法中
    • idArg - ID 参数;标记出作为 ID 的结果可以帮助提高整体性能
    • arg - 将被注入到构造方法的一个普通结果
  • id – 一个 ID 结果;标记出作为 ID 的结果可以帮助提高整体性能
  • result – 注入到字段或 JavaBean 属性的普通结果
  • association – 一个复杂类型的关联;许多结果将包装成这种类型
    • 嵌套结果映射 – 关联可以指定为一个 resultMap 元素,或者引用一个
  • collection – 一个复杂类型的集合
    • 嵌套结果映射 – 集合可以指定为一个 resultMap 元素,或者引用一个
  • discriminator – 使用结果值来决定使用哪个 resultMap
    • case – 基于某些值的结果映射
      • 嵌套结果映射 – 一个 case 也是一个映射它本身的结果,因此可以包含很多相 同的元素,或者它可以参照一个外部的 resultMap。
id&result/constructor

 

 

一对一association

 

 

一对多collection

 

鉴别器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>

第五部分cache 

 

第六部分 动态sql

  • if
  • choose (when, otherwise)
  • trim (where, set)
  • foreach

if

做条件判断的,如果我们不使用这个标签,我们肯定会在代码中判断如查询的元素是否为空,传入的元素是否为空,而这时我们直接使用这个标签,就减少了代码的书写

<!-- 传递pojo综合查询用户信息 -->

    <select id="findUserList" parameterType="user" resultType="user">

       select * from user

       where 1=1

       <if test="id!=null">

       and id=#{id}

       </if>

       <if test="username!=null and username!=''">

       and username like '%${username}%'

concat(‘%’,#{username},’%’)

       </if>

    </select>

choose (when, otherwise)

对于这类标签,就是采用多个选项中找一个,就像单项选择题,但是你不会都选择,只会从中选择1个来作为条件。就有点类似于switch。。case。

<select id="findActiveBlogLike" 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 author_name like #{author.name}

    </when>

    <otherwise>

      AND featured = 1

    </otherwise>

  </choose>

</select>

trim (where, set)

如果使用第一个if语句的sql有and的话,就会发现没有写where标签就会报错。而这类标签通常是搭配条件标签使用的。

<select id="findUserList" parameterType="user" resultType="user">

       select * from user

       <where>

         <if test="id!=null and id!=''">

           and id=#{id}

         </if>

         <if test="username!=null and username!=''">

           and username like '%${username}%'

         </if>

       </where>

</select>

foreach

猜你喜欢

转载自www.cnblogs.com/lifeibai/p/9036922.html