MyBatis 配置的一些小知識點,mybatis配置知識點

來源:互聯網
上載者:User

MyBatis 配置的一些小知識點,mybatis配置知識點
MyBatis別名配置——typeAliases

  類型別名是為 Java 類型設定一個短的名字。它只和 XML 配置有關,存在的意義僅在於用來減少類完全限定名的冗餘。說白了就是預先設定包名

api是這樣寫的

<typeAliases>  <typeAlias alias="Author" type="domain.blog.Author"/>  <typeAlias alias="Blog" type="domain.blog.Blog"/></typeAliases>

也可以指定一個包名,MyBatis 會在包名下面搜尋需要的 Java Bean,比如:

<typeAliases>  <package name="domain.blog"/></typeAliases>

 

這個直接在xml中的<configuration>下設定就可以,再使用的時候就可以直接使用別名了,不過要注意的時候寫在xml下的時候有循序關聯性的 哦!碰到錯誤自己解決,調整位置就好

已經為許多常見的 Java 類型內建了相應的類型別名。它們都是大小寫不敏感的,需要注意的是由基本類型名稱重複導致的特殊處理。

別名 映射的類型
_byte byte
_long long
_short short
_int int
_integer int
_double double
_float float
_boolean boolean
string String
byte Byte
long Long
short Short
int Integer
integer Integer
double Double
float Float
boolean Boolean
date Date
decimal BigDecimal
bigdecimal BigDecimal
object Object
map Map
hashmap HashMap
list List
arraylist ArrayList
collection Collection
iterator Iterator
映射器——mappers

 

既然 MyBatis 的行為已經由上述元素配置完了,我們現在就要定義 SQL 映射語句了。但是首先我們需要告訴 MyBatis 到哪裡去找到這些語句。 Java 在自動尋找這方面沒有提供一個很好的方法,所以最佳的方式是告訴 MyBatis 到哪裡去找對應檔。你可以使用相對於類路徑的資源引用, 或完全限定資源定位器(包括 file:/// 的 URL),或類名和包名等。

這裡有四種方法

第一種類路徑的資源引用<mapper resource="com/entity/UserMapper.xml"/>第二種直接給出本地地址形式的,自己存放的地址,完全限定資源定位器<mapper url="file:///var/mappers/UserMapper.xml"/>第三種以類的形式去尋找<mapper class="com.entity.AuthorMapper"/>第四種包名的形式 <package name="com.entity.builder"/>

 

映射配置細節

 

resultMap和resultType  

  MyBatis中在查詢進行select映射的時候,傳回型別可以用resultType,也可以用resultMap,

  resultType是直接表示傳回型別的,查詢出來的每一個屬性都是放在一個對應的Map裡面的,其中鍵是屬性名稱,值則是其對應的值。當提供的傳回型別屬性是resultType的時候,MyBatis會將Map裡面的索引值對取出賦給resultType所指定的對象對應的屬性。所以其實MyBatis的每一個查詢映射的傳回型別都是ResultMap,只是當我們提供的傳回型別屬性是resultType的時候,MyBatis對自動的給我們把對應的值賦給resultType所指定對象的屬性。

  resultMap則是對外部ResultMap的引用,然後基於尋找出來的屬性名稱進行索引值對封裝,主要用在進行複雜聯集查詢上,結果集映射是MyBatis 中最強大的特性。許多複雜的映射都可以輕鬆解決。

  resultType跟resultMap不能同時存在。

resultMap支援繼承

  

  extends="User"說明繼承了上一個resultMap,要是一個實體類很重的話,先給出一個架構大家都繼承與他,是不錯的選擇

    <!--多對多關聯查詢 -->    <resultMap id="User" type="com.entity.User">        <result property="id" column="id" />        <result property="username" column="username" />        <result property="password" column="password" />    </resultMap>    <resultMap id="user_info" type="User" extends="User">        <collection property="userInfos" ofType="com.entity.UserInfo"            column="uid">            <result property="id" column="id" />            <result property="address" column="address" />        </collection>    </resultMap>    <select id="getUser" resultMap="user_info">        select * from user u,userinfo i        where u.id=i.uid and u.id=#{id}    </select>
動態SQL陳述式完成多條件查詢if

動態 SQL 通常要做的事情是有條件地包含 where 子句的一部分。

<select id="findActiveBlogWithTitleLike"     resultType="Blog">  SELECT * FROM BLOG   WHERE state = ‘ACTIVE’   <if test="title != null">    AND title like #{title}  </if></select>

這條語句提供了一個可選的文本尋找類型的功能。如果沒有傳入“title”,那麼所有處於“ACTIVE”狀態的BLOG都會返回。

choose
<select id="queryEmp"  resultType="cn.test.entity.Emp">          select * from emp where 1=1          <choose>          <when test="deptNo!=null">          and deptno=#{deptNo}          </when>          <when test="deptName!=null">          and deptname=#{deptName}          </when>          <otherwise>          and personnum>#{personNum}          </otherwise>          </choose>

</select>

上面也說了,choose相當於Java中的switch語句;當第一個when滿足時;就只執行第一個when中的條件。當when中的條件都不滿足時;就會執行預設的的;也就是otherwise中的語句。

Where
<select id="getU" resultMap="User" parameterType="com.entity.User">        select*from user        <where>            <if test="username!=null and username!=''">                username like concat('%',#{username},'%')            </if>            <if test="id!=null">                and id=#{id}            </if>        </where>    </select>

 where下面第一個if語句中以and開頭,也可以省略第一個and ,如果第一個if語句中有and;mybatis會將第一個and忽略。

set
<update id="updateEmp" parameterType="cn.test.entity.Emp" flushCache="true">          update emp           <set>          <if test="empName!=null">empname=#{empName},</if>          <if test="job!=null">job=#{job}</if>          </set>          where empno=#{empNo}          </update>

  在mybatis中的SQL語句結尾不能加“;”,這樣會導致mybatis無法識別字元;導致SQL語句的語法錯誤;

foreach

動態 SQL 的另外一個常用的必要操作是需要對一個集合進行遍曆,通常是在構建 IN 條件陳述式的時候

<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 元素的功能是非常強大的,它允許你指定一個集合,聲明可以用在元素體內的集合項目和索引變數。它也允許你指定開閉匹配的字串以及在迭代中間放置分隔字元。這個元素是很智能的,因此它不會偶然地附加多餘的分隔字元。

 

 

   【版本聲明】本文為博主原創文章,轉載請註明出處

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.