mybatis映射文件mapper詳解


映射文件是mybatis框架中十分重要的文件,可以說,mybatis框架的強大之處就體現在映射文件的編寫上。mapper.xml映射文件主要是用來編寫sql語句的,以及一些結果集的映射關系的編寫,還有就是緩存的一些配置等等。

一、select元素

<select>元素就是sql查詢語句。可以執行一些簡單的查詢操作,也可以是復雜的查詢操作。例如:

<select id="findCustomerById" parameterType="Integer" resultType="com.itheima.po.Customer">
     select * from t_customer where id = #{id}
</select>

屬性 說明
statementType 用於設置mybatis使用哪個JDBC的Statement工作,其值為statement、prepared(默認值)或callable,分別對應JDBC中的Statement、PreparedStatement和CallableStatement

二、insert元素

<insert>元素用於映射插入語句,在執行完元素中定義的SQL語句后,會返回一個表示插入記錄數的整數。

<insert>元素的配置示例如下:

<insert
      id="addCustomer"
      parameterType="com.itheima.po.Customer"
      flushCache="true"
      statementType="PREPARED"
      keyProperty=""
      keyColumn=""
      useGeneratedKeys=""
      timeout="20">

執行插入操作后,很多時候需要返回插入成功的數據生成的主鍵值,此時就可以通過上面講解的3個屬性來實現。

1、對於支持主鍵自助增長的數據庫(如MySQL),可以通過如下配置實現:

<insert id="addCustomer" parameterType="com.itheima.po.Customer" keyProperty="id" useGeneratedKeys="true" >
      insert into t_customer(username,jobs,phone) values(#{username},#{jobs},#{phone})
</insert>

使用以上配置后,執行插入后會返回插入成功的行數,以及插入行的主鍵值。

2、對於不支持主鍵自助增長的數據庫(如Oracle),或者支持增長的數據庫取消了主鍵自動增長的規則時,可以通過如下配置實現自定義生成主鍵:

<insert id="insertCustomer" parameterType="com.itheima.po.Customer">
      <selectKey keyProperty="id" resultType="Integer" order="BEFORE">
          select if(max(id) is null, 1, max(id) +1) as newId from t_customer
      </selectKey>  
        
      insert into t_customer(id,username,jobs,phone)  values(#{id},#{username},#{jobs},#{phone})
</insert>

解析:在執行以上配置時,selectKey元素會首先執行,它會通過自定義的語句來設置數據表中的主鍵(如果t_customer表中沒有記錄,則將id設置為1,否則就將id的最大值加1來作為新的主鍵),然后調用插入語句。

 <selectKey 
      keyProperty="id" 
      resultType="Integer" 
      order="BEFORE"
      statementType="Prepared" (默認的)
>      

其中orde屬性可以設置為before或者after。當設置為before時,那么它會首先執行selectkey元素中的配置來設置主鍵,然后在執行插入語句;如果設置為after,則相反。

三、update和delete元素

<update><delete>元素的使用比較簡單,它們的屬性配置也基本相同。

1、<update><delete>元素的常用屬性如下:

<update
      id="updateCustomer"
      parameterType="com.itheima.po.Customer"
      flushCache="true"
      statementType="PREPARED"
      timeout="20">
    
<delete
      id="deleteCustomer"
      parameterType="com.itheima.po.Customer"
      flushCache="true"
      statementType="PREPARED"
      timeout="20">

2、<update><delete>元素的使用示例如下:

<update id="updateCustomer" parameterType="com.itheima.po.Customer">
       update t_customer 
       set username=#{username},jobs=#{jobs},phone=#{phone}
       where id=#{id}
</update>

<delete id="deleteCustomer" parameterType="Integer">
        delete from t_customer where id=#{id}
</delete>

四、sql片段元素

sql片段,可以在mapper文件中的任何地方引用,主要作用是減少代碼量,復用重復的字段。
例如:

<sql id="customerColumns">id,username,jobs,phone</sql>

定義sql片段,通過<include>元素的refid屬性引用id為customerColumns的代碼片段。
例如:

<select id="findCustomerById" parameterType="Integer" resultType="com.itheima.po.Customer">
    select 
    <include refid="customerColumns"/>
    from tableName
    where id = #{id}
</select>

五、resultMap 結果集映射元素

<resultMap>元素表示結果映射集,是MyBatis中最重要也是最強大的元素。它的主要作用是定義映射規則、級聯的更新以及定義類型轉化器等。<resultMap>元素中包含了一些子元素,它的元素結構如下所示:

<resultMap type="" id="">
       <constructor>    <!-- 類在實例化時,用來注入結果到構造方法中-->
             <idArg/>      <!-- ID參數;標記結果作為ID-->
             <arg/>          <!-- 注入到構造方法的一個普通結果-->
       </constructor>  
       <id/>                 <!-- 用於表示哪個列是主鍵-->
       <result/>           <!-- 注入到字段或JavaBean屬性的普通結果-->
       <association property="" />        <!-- 用於一對一關聯 -->
       <collection property="" />          <!-- 用於一對多關聯 -->
       <discriminator javaType="">      <!-- 使用結果值來決定使用哪個結果映射-->
            <case value="" />                   <!-- 基於某些值的結果映射 -->
       </discriminator> 
</resultMap>

<resultMap>元素的type屬性表示需要映射的POJO,id屬性是這個resultMap的唯一標識。它的子元素<constructor>用於配置構造方法(當一個POJO中未定義無參的構造方法時就可以使用<constructor>元素進行配置)。子元素<id>用於表示那個列是主鍵,而<result>用於表示POJO和數據表中普通列的映射關系。 <association><collection>用於處理多表時的關聯關系,而<discriminator>元素主要用於處理一個單獨的數據庫查詢返回很多不同數據類型結果集的情況。
在默認情況下,mybatis程序在運行時會自動的將查詢到的數據與需要返回的對象的屬性進行匹配賦值(需要表中的列名與對象的屬性名稱完全一致),然而在實際開發中屬性名和列名可能不一致,這個時候就需要使用resultMap元素進行映射處理。

例如:簡單的結果集映射

    <resultMap id="BaseResultMap" type="cn.jason.bootmybatis.model.Tests">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="age" property="age" jdbcType="INTEGER"/>
    </resultMap>

   
    <select id="selectByPrimaryKey" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tests
        where id = #{id,jdbcType=BIGINT}
    </select>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM