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

 

 

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

 


免責聲明!

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



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